我對小膠質答案,https://gitter.im/balderdashy/sails#
張貼了同在這裏:
var winston = require('winston');
var getCaller = function()
{
var stack = getStack();
stack.shift();
var obj = stack[3];
// Return caller's caller
return obj.getFileName();
};
var getStack = function()
{
// Save original Error.prepareStackTrace
var origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack)
{
return stack;
};
// Create a new `Error`, which automatically gets `stack`
var err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
var stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack
};
var customLogger = new winston.Logger({
transports: [
new(winston.transports.File)({
level: 'debug',
filename: './logs/application.log'
}),
new (winston.transports.Console)({
level: 'silly',
colorize: true,
timestamp: true
})
]
});
customLogger.log = function(){
var args = arguments;
args[1] = getCaller()+" - "+args[1];
winston.Logger.prototype.log.apply(this,args);
};
module.exports.log = {
colors: false,
custom: customLogger
};
這也可以讓你從堆棧中的函數名 –