首页 文章

使用winston-mongodb,express-winston和winston登录

提问于
浏览
3

我正在使用winston,expressWinston,winston-mongoDB来管理我的NodeJs应用程序中的日志 .

使用winston-mongodb运输时使用express-winston;我可以按以下格式将日志存储到我的mongoDB集合中 . (元:真)

{
"_id" : ObjectId("5a124f9781d911337ebeb98d"),
"timestamp" : ISODate("2017-11-20T03:44:23.336Z"),
"level" : "info",
"message" : "GET /stylesheets/bootstrap.css 304 2ms",
"meta" : {
    "res" : {
        "statusCode" : 304
    },
    "req" : {
        "url" : "/stylesheets/bootstrap.css",
        "headers" : {
            "host" : "localhost:3000",
            "connection" : "keep-alive",
        },
        "method" : "GET",
        "httpVersion" : "1.1",
        "originalUrl" : "/stylesheets/bootstrap.css",
        "query" : {}
    },
    "responseTime" : 2
}

我可以在meta中获取请求/响应信息 .

这些细节是否可能直接成为该集合的一部分? ,像这样:

{
"_id" : ObjectId("5a12537d81b6b634cb7d4696"),
"timestamp" : ISODate("2017-11-20T04:01:01.229Z"),
"level" : "info",
"message" : "GET /stylesheets/bootstrap.css 304 11ms",
"status": 200,
"requestUrl": '/',
"requestMethod": 'GET',
"remoteIp": '::1'
"meta" : {}

}

1 回答

  • 0

    您可以覆盖默认的日志功能实现,以保存您想要的任何内容

    let logger = new winston.Logger(options);
    logger.log = function () {
        var args = arguments;
        // here you can transform your meta obj
        winston.Logger.prototype.log.apply(this, args);
    };
    

    希望它会帮助你的想法

相关问题