I'm making a Node.js app and I am using Winston for most of my logging purposes. I also aware of the Connect/Express logger function and know it has a stream option... Is it at all possible to output the stuff from Connect/Express's logger function to Winston? ...then I can have all the useful logging I need?
我正在做一个节点。js应用程序和我使用Winston进行大部分日志记录。我还知道Connect/Express记录器函数,并知道它有一个流选项……是否有可能将Connect/Express的logger函数输出到Winston?…那么我就可以有我需要的所有有用的日志了?
I find the logging that Connect/Express useful, but at the moment the two are sort of separate... I would must prefer to have it all running through Winston and it's transports.
我发现连接/表示的日志记录很有用,但是现在两者有点不同……我一定要让它跑过温斯顿和它的运输。
How is that possible? Thanks, James
这怎么可能?谢谢你,詹姆斯
2 个解决方案
#1
68
Here is what i did to solve this very issue. Basically use the stream option in the connect/express logger module to pipe the messages through to winston. I chose to use winston.info logging level, use whatever level makes sense for you.
这就是我解决这个问题的方法。基本上,使用connect/express logger模块中的stream选项将消息传输到winston。我选择使用winstone .info日志级别,使用任何对您有意义的级别。
var winston = require('winston');
var express = require('express');
var app = express.createServer();
// enable web server logging; pipe those log messages through winston
var winstonStream = {
write: function(message, encoding){
winston.info(message);
}
};
app.use(express.logger({stream:winstonStream}));
// now do the rest of your express configuration...
#2
3
I had the same problem, I looked into the internals of the logger module and pretty much replicated what is there, in this custom middleware (warning, coffeescript).
我遇到了同样的问题,我研究了日志记录器模块的内部结构,并在这个自定义中间件(警告、coffeescript)中几乎复制了其中的内容。
As a bonus, it logs data using metadata fields as well.
此外,它还使用元数据字段记录数据。
(req, res, next) ->
sock = req.socket
req._startTime = new Date
req._remoteAddress = sock.socket && sock.socket.remoteAddress || sock.remoteAddress;
_url = () -> req.originalUrl || req.url
_method = () -> req.method
_respTime = () -> String(Date.now() - req._startTime)
_status = () -> res.headerSent && res.statusCode || null
_remoteAddr = () -> req.ip || req._remoteAddress || (req.socket?.socket? && req.socket.socket.remoteAddress) || req.socket.remoteAddress
_usrAgent = () -> req.headers['user-agent']
logRequest = () ->
res.removeListener 'finish', logRequest
res.removeListener 'close', logRequest
winston.info "#{_method()} #{_url()} #{_status()} #{_remoteAddr()} #{_usrAgent()} #{_respTime()}",
http_access:
method: _method()
url: _url()
status: _status()
remote_address: _remoteAddr()
user_agent: _usrAgent()
res.on 'finish', logRequest
res.on 'close', logRequest
next()
#1
68
Here is what i did to solve this very issue. Basically use the stream option in the connect/express logger module to pipe the messages through to winston. I chose to use winston.info logging level, use whatever level makes sense for you.
这就是我解决这个问题的方法。基本上,使用connect/express logger模块中的stream选项将消息传输到winston。我选择使用winstone .info日志级别,使用任何对您有意义的级别。
var winston = require('winston');
var express = require('express');
var app = express.createServer();
// enable web server logging; pipe those log messages through winston
var winstonStream = {
write: function(message, encoding){
winston.info(message);
}
};
app.use(express.logger({stream:winstonStream}));
// now do the rest of your express configuration...
#2
3
I had the same problem, I looked into the internals of the logger module and pretty much replicated what is there, in this custom middleware (warning, coffeescript).
我遇到了同样的问题,我研究了日志记录器模块的内部结构,并在这个自定义中间件(警告、coffeescript)中几乎复制了其中的内容。
As a bonus, it logs data using metadata fields as well.
此外,它还使用元数据字段记录数据。
(req, res, next) ->
sock = req.socket
req._startTime = new Date
req._remoteAddress = sock.socket && sock.socket.remoteAddress || sock.remoteAddress;
_url = () -> req.originalUrl || req.url
_method = () -> req.method
_respTime = () -> String(Date.now() - req._startTime)
_status = () -> res.headerSent && res.statusCode || null
_remoteAddr = () -> req.ip || req._remoteAddress || (req.socket?.socket? && req.socket.socket.remoteAddress) || req.socket.remoteAddress
_usrAgent = () -> req.headers['user-agent']
logRequest = () ->
res.removeListener 'finish', logRequest
res.removeListener 'close', logRequest
winston.info "#{_method()} #{_url()} #{_status()} #{_remoteAddr()} #{_usrAgent()} #{_respTime()}",
http_access:
method: _method()
url: _url()
status: _status()
remote_address: _remoteAddr()
user_agent: _usrAgent()
res.on 'finish', logRequest
res.on 'close', logRequest
next()