2

From the below post i got the idea to use moment with winston to change date format.

winston:how to change timestamp format

but this is working for winston.transports.Console and its not working for winston.transports.File

PFB my code :

var logger = new winston.Logger({
    level: 'debug',
    timestamp: function () {
      return moment().format('YYYY-MM-DD hh:mm:ss')
    },
    transports: [
          new(winston.transports.MongoDB)({
                  db : 'dbpath',
                  collection : 'Logs',
                    level : 'error',
                    capped : true,
                    timestamp: function () {
                      return moment().format('YYYY-MM-DD hh:mm:ss')
                    }

          }),
          new winston.Logger({
            level: 'debug',
            transports: [
              new(winston.transports.File)
              ({ filename: 'filename' })
            ],
            timestamp: function () {
              return moment().format('YYYY-MM-DD hh:mm:ss')
            }
          }),
          new winston.transports.Console({

            timestamp: function () {
              return moment().format('YYYY-MM-DD hh:mm:ss')
            }

          })
    ]
})

I am getting this in my output file:

{"level":"info","message":"info Hello","timestamp":"2018-04-16T06:42:12.819Z"}
{"level":"error","message":"error Hello","timestamp":"2018-04-16T06:42:12.847Z"}
{"level":"debug","message":"debug Hello","timestamp":"2018-04-16T06:42:12.861Z"}
{"level":"warn","message":"debug Hello","timestamp":"2018-04-16T06:42:12.900Z"}

it's neither working for mongodb transport

Prabhat Mishra
  • 951
  • 2
  • 12
  • 33
  • Because you are inserting the "logs" into MongoDB. As soon as you do that, whatever format the "string" you supply as input, it's simply being parsed into the stored `BSON Date`. The other logs should be doing whatever you ask them, but the data in MongoDB is going to comply to what it wants. – Neil Lunn Apr 16 '18 at 06:48
  • ok agreed with mongodb but data in file is also going in same format i have posted the same in question..please check.. – Prabhat Mishra Apr 16 '18 at 06:53
  • That would seem to indicate that `winston.transports.MongoDB` is actually overriding the other specified loggers, since standard log formats are not serialized JSON. This might be an artifact of using that logger, or the other configs might be incorrect. What happens if you comment out the MongoDB logger? – Neil Lunn Apr 16 '18 at 06:56
  • same no change the `console` output is coming as desired o/p but the file o/p is still the same as mentioned in the question. – Prabhat Mishra Apr 16 '18 at 06:59
  • Umm. If you actually "commented out" the whole logger config section like I asked then you should be seeing a drastic change. If you "don't see a change" then you are picking up a "different" logging configuration to what you think you are editing. – Neil Lunn Apr 16 '18 at 07:01
  • as you said i only commented out `MongoDB logger` so its not logging in to mongodb now .. – Prabhat Mishra Apr 16 '18 at 07:06

0 Answers0