本文介绍了使用Winston-MongoDB,Express-Winston和Winston进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using winston, expressWinston, winston-mongoDB to manage the logs in my NodeJs application.

将winston-mongodb传输与express-winston一起使用时;我可以按照以下格式将日志存储到我的mongoDB集合中.(meta:true)

While using winston-mongodb transport with express-winston; i can store the logs to my mongoDB collection in the following format. (meta:true)

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

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

I can get the request/response info in the meta.

这些细节是否有可能直接成为馆藏的一部分?,就像这样:

Is it possible that these details be a part of the collection directly ? , something like this:

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

}

推荐答案

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

you can override the default log function implementation to save whatever you want exactly

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

希望它将帮助您实现这一想法

Hope it will help you the idea

这篇关于使用Winston-MongoDB,Express-Winston和Winston进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:23