Winston: process.exit() just after logging, doesn't log to the files.

Created on 3 Apr 2019  路  1Comment  路  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_

    • [ ] winston@2

    • [x] winston@3

  • _node -v outputs:_ v11.13.0
  • _Operating System?_ WSL
  • _Language?_ ES6/7

What is the problem?

Here is how to reproduce the problem.
~~~~
const logger = winston.createLogger({
transports: [
new winston.transports.File({ filename: "src/logs/access.log", level: "info" }),
new winston.transports.File({ filename: "src/logs/errors.log", level: "error" })
],
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json()
)
});

logger.add(
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple())
})
);

logger.info({
level: "info",
message: "Incoming Request"
});
process.exit()
~~~~
The outcome is, it logs to the console, but doesn't log to the files because it takes some time to log to files.

So the solution can be making the logger.log function async
Or
Can someone suggest a workaround?

What do you expect to happen instead?

It should log to all the three transports, console and 2 files.

Most helpful comment

You should be able to wait for the "finish" event, and exit then. See this section in the README: (https://github.com/winstonjs/winston#awaiting-logs-to-be-written-in-winston)

However, this behavior seems to be broken atm, at least for File transports - they don't flush on end: #1504

>All comments

You should be able to wait for the "finish" event, and exit then. See this section in the README: (https://github.com/winstonjs/winston#awaiting-logs-to-be-written-in-winston)

However, this behavior seems to be broken atm, at least for File transports - they don't flush on end: #1504

Was this page helpful?
0 / 5 - 0 ratings