Node.js - 日志记录/使用摩根和温斯顿
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27906551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js - logging / Use morgan and winston
提问by Or Smith
we use morganin order to log our express transformation:
我们使用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;
})
Also, we use winstonin order to log our other logging:
此外,我们使用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' })
]
});
Is there any way to combine the two loggers together? the situation now is that morganis write to my standard output, when winstonwrites to /var/log/log-file.log.
有没有办法将两个记录器结合在一起?现在的情况morgan是写入我的标准输出,当winston写入/var/log/log-file.log.
I wish that the logger file will combine from the express transformation information, and from the other information I want (logger.info())..
我希望记录器文件将结合快速转换信息和我想要的其他信息 ( logger.info()..
回答by lindsaymacvean
This article does an excellent job for what you want to do.
这篇文章非常适合您想要做的事情。
http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
For your specific code you probably need something like this:
对于您的特定代码,您可能需要这样的东西:
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 }));
This will set up Winston to write a log to the console as well as a file. Then you can use the last expression to pass output from the morgan middleware into winston.
这将设置 Winston 将日志和文件写入控制台。然后您可以使用最后一个表达式将摩根中间件的输出传递到 winston。
回答by Mahyar SEPEHR
In Typescript:
在打字稿中:
let logger = new (winston.Logger)({
exitOnError: false,
level: 'info',
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'app.log'})
]
})
class MyStream {
write(text: string) {
logger.info(text)
}
}
let myStream = new MyStream()
app.use(morgan('tiny', { stream: myStream }));
回答by Johnny
Update the last line to remove warning
更新最后一行以删除警告
app.use(require("morgan")("combined", { stream: logger.stream }));
回答by Cris Shaki
for Typescript another way to go about it, without needing to create a class is
对于打字稿,另一种无需创建类的方法是
let logger = new (winston.Logger)({
exitOnError: false,
level: 'info',
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'app.log'})
]
})
const myStream = {
write: (text: string) => {
logger.info(text)
}
}
app.use(morgan('combined', { stream: myStream }));
This solution was hived from this Github page https://github.com/winstonjs/winston/issues/1385. However, it is important to note that there is a slight difference between our codes. Instead of:
此解决方案来自此 Github 页面https://github.com/winstonjs/winston/issues/1385。但是,需要注意的是,我们的代码之间存在细微差别。代替:
app.use(morgan('combined', { myStream }));
I use:
我用:
app.use(morgan('combined', { stream: myStream }));
This helped me out as am not too big on creating classes.
这帮助我解决了创建类的问题。
回答by user566245
Morgan has the bad habit of ending the message with a \nso to make things orderly you may want to remove that before writing it to winston.
Morgan 有一个坏习惯,以 结束消息,\n因此为了使事情井然有序,您可能希望在将其写入 winston 之前将其删除。
This can be done in many different ways like on the format side in winston, or by updating your stream to not write the \n
这可以通过许多不同的方式完成,例如在 winston 的格式方面,或者通过更新您的流以不写入 \n
class MyStream {
write(text: string) {
logger.info(text.replace(/\n$/, ''));
}
}
let myStream = new MyStream()
app.use(morgan('tiny', { stream: myStream }));

