I am new to Winston, when there is an error on the code it gets logged to the console but not to the log file. Infos get logged to another file correctly but errors don't.
Here's my logger.js file:
const winston = require('winston')
let logger
module.exports.init = () => {
logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: './logs/error.log', level: 'error' }),
new winston.transports.File({ filename: './logs/info.log' })
]
})
process.on('uncaughtException', ex => {
logger.error(ex.message)
process.exit(1)
})
process.on('unhandledRejection', ex => {
logger.error(ex.message)
process.exit(1)
})
}
module.exports.use = () => logger
I've used the code here, but it won't work. Any help would be really appreciated.
I suspect your process is exiting before getting a chance to write to the logs. I'd suggest waiting until all messages are logged before exiting, see Awaiting logs to be written in winston
process.on('uncaughtException', ex => {
logger.error(ex.message)
logger.on('finish', () => {
process.exit(1);
});
})
process.on('unhandledRejection', reason => {
logger.error("unhandledRejection: " + reason)
logger.on('finish', () => {
process.exit(1);
});
})