I'm new to Winston, and I'm trying to use it to log uncaught exceptions and then exit. It is logging to the console, but doesn't seem to have time to log to a file before exiting
Here is an example:
index.js
const express = require('express');
const app = express();
const logger = require('./startup/logging');
process.on('uncaughtException', (ex) => {
logger.error("Won't be logged in the File");
process.exit(1);
});
throw new Error("Uncaught");
logging.js
const { createLogger, format, transports } = require('winston');
const logger = createLogger({
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD hh:mm:ss |'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
new transports.Console({
format: format.combine(
format.colorize({
// all: true
}),
// this is repeated because for some reason colorize() won't work otherwise
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
)
}),
new transports.File({ filename: './exceptions.log' })
]
});
module.exports = logger;
As mentioned, the uncaught exception is logged to the console, but not to the file. Removing process.exit(1) solves the problem of logging, but I do want the program to exit.
Adding handleExceptions: true to the File transport causes the same issue - it exits prior to logging the exception. I'm assuming this is the cause, as adding exitOnError: false then logs the exception.
Is this the intended behaviour? It seems strange to me, even as a beginner.
EDIT:
process.on('uncaughtException', (ex) => {
logger.log('error', 'Will be logged');
setTimeout(function(){process.exit(1)}, 1000);
});
This works, but surely isn't a fix, just shows the problem i'm having
What version are you using? In v2 you could use a callback that executes after the log entry has been written.
process.on('uncaughtException', (ex) => {
logger.error("Will be logged in the File", function() {
process.exit(1);
});
});
In v3 you're supposed to do:
logger.log('info', 'some message');
logger.on('finish', () => process.exit());
logger.end();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With