we use morgan
为了注销快速转换:
var morgan = require('morgan');
morgan('combined');
// a format string
morgan(':remote-addr :method :url :uuid');
// a custom function
morgan(function (req, res) {
return req.method + ' ' + req.url + ' ' + req.uuid;
})
另外,我们使用winston
为了记录我们的其他日志记录:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level: 'info' }),
new (winston.transports.File)({ filename: '/var/log/log-file.log' })
]
});
有什么办法可以将两个记录器结合在一起吗?现在的情况是morgan
写入我的标准输出,当winston
写信给/var/log/log-file.log
.
我希望记录器文件能够结合快速转换信息和我想要的其他信息(logger.info()
)..
这篇文章非常适合您想做的事情。
http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
对于您的特定代码,您可能需要这样的东西:
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: './logs/all-logs.log',
handleExceptions: true,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.info(message);
}
};
app.use(require("morgan")("combined", { "stream": logger.stream }));
这将设置 Winston 将日志写入控制台和文件。然后,您可以使用最后一个表达式将 morgan 中间件的输出传递到 Winston。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)