Winston: Timestamp is not printed in the file when you set timestamp format directly to the logger transport.

Created on 28 Jun 2018  路  1Comment  路  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_

    • [ ] winston@2

    • [X] winston@3

  • _node -v outputs:_ v8.10.0
  • _Operating System?_ Linux
  • _Language?_ ES6

What is the problem?

The message timestamp is not printed in the file when you set timestamp() format directly to the logger transport.

javascript const fileLogger = winston.createLogger({ level: 'info', transports: [ new winston.transports.Console({ format: winston.format.combine( winston.format.colorize(), winston.format.simple(), ), }), new winston.transports.File({ format: winston.format.combine( winston.format.json(), winston.format.timestamp(), ), filename: path.resolve('project.log') }), ] });
Output:
json {"message":"Using development environment settings","level":"info"} {"message":"Debug mode is ON","level":"info"} {"message":"Configuring routes","level":"info"} {"message":"Server is listening on port 3000","level":"info"}

What do you expect to happen instead?

Timestamp should be printed in the file:
json {"message":"Using development environment settings","level":"info","timestamp":"2018-06-28T16:40:28.944Z"} {"message":"Debug mode is ON","level":"info","timestamp":"2018-06-28T16:40:28.948Z"} {"message":"Configuring routes","level":"info","timestamp":"2018-06-28T16:40:28.949Z"} {"message":"Intercambio Backend server is listening on port 3000","level":"info","timestamp":"2018-06-28T16:40:28.957Z"}
This only works if I add the timestamp format in the logger level.

Most helpful comment

Formats are applied in order, apply the timestamp format before the json format like this:

winston.format.combine(
  winston.format.timestamp(),
  winston.format.json()
)

>All comments

Formats are applied in order, apply the timestamp format before the json format like this:

winston.format.combine(
  winston.format.timestamp(),
  winston.format.json()
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nepoxx picture Nepoxx  路  4Comments

KingRial picture KingRial  路  3Comments

sinai-doron picture sinai-doron  路  3Comments

mohanen picture mohanen  路  4Comments

pocesar picture pocesar  路  3Comments