winston version?_winston@2winston@3 node -v outputs:_ v8.10.0The 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"}
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.
Formats are applied in order, apply the timestamp format before the json format like this:
winston.format.combine(
winston.format.timestamp(),
winston.format.json()
)
Most helpful comment
Formats are applied in order, apply the
timestampformat before thejsonformat like this: