【问题标题】:Logging with winston-mongodb, express-winston and winston使用 winston-mongodb、express-winston 和 winston 进行日志记录
【发布时间】:2018-01-15 18:51:06
【问题描述】:

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

在使用带有 express-winston 的 winston-mongodb 传输时;我可以按以下格式将日志存储到我的 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
}

我可以在元数据中获取请求/响应信息。

这些细节有可能直接成为收藏的一部分吗? ,类似这样:

{
"_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" : {}

}

【问题讨论】:

标签: node.js mongodb express winston


【解决方案1】:

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

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);
};

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2018-06-21
    • 2019-09-29
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多