winston version?_winston@2winston@3 node -v outputs:_ v11.13.0 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?
It should log to all the three transports, console and 2 files.
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
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