4

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

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

}

nevosial
  • 1,034
  • 2
  • 13
  • 20
  • I'm having some trouble with the same implementation, can you please check my question here https://stackoverflow.com/questions/48202738/logging-with-winston-mongodb-and-express-winston ? Would appreciate a little bit help on this. – josh Jan 11 '18 at 08:47

1 Answers1

0

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

M.elsayed
  • 96
  • 6