winston exceptions, unhandled exceptions, winston weird exception log etc.
The problem
I'm using winston to log everything in my NestJS app and it works perfectly except for one problem:
When an unhandled exception occurs (for example if I throw one on purpose) and passed onto node, winston logs it but in a very weird format:
{"date":"Mon Sep 04 2023 18:59:25 GMT+0300 (שעון ישראל (קיץ))","error":{},"exception":true,"level":"error","message":"unhandledRejection: djdjd\nError: djdjd\n at bootstrap (file:///C:/proj/integration/test/src/main.ts:17:9)\n at file:///C:/proj/integration/test/src/main.ts:23:1\n at ModuleJob.run (node:internal/modules/esm/module_job:194:25)","os":{"loadavg":[0,0,0],"uptime":377372.531},"process":{"argv":["C:\\Program Files\\nodejs\\node.exe","C:\\proj\\integration\\test\\dist\\main"],"cwd":"C:\\proj\\integration\\test","execPath":"C:\\Program Files\\nodejs\\node.exe","gid":null,"memoryUsage":{"arrayBuffers":503050,"external":2631243,"heapTotal":40247296,"heapUsed":27436640,"rss":82460672},"pid":18272,"uid":null,"version":"v18.16.1"},"stack":"Error: djdjd\n at bootstrap (file:///C:/proj/integration/test/src/main.ts:17:9)\n at file:///C:/proj/integration/test/src/main.ts:23:1\n at ModuleJob.run (node:internal/modules/esm/module_job:194:25)","trace":[{"column":9,"file":"file:///C:/proj/integration/test/src/main.ts","function":"bootstrap","line":17,"method":null,"native":false},{"column":1,"file":"file:///C:/proj/integration/test/src/main.ts","function":null,"line":23,"method":null,"native":false},{"column":25,"file":"node:internal/modules/esm/module_job","function":"ModuleJob.run","line":194,"method":"run","native":false}]}
Sometimes it even logs the same thing 2 or even 3 times in a row.
For the life of me I can't figure out how to format this any differently or at least take off some (most) of the properties there are here.
This is my logger config:
// Define the levels that can be used for logging
const levels = {
error: 0,
warn: 1,
info: 2,
debug: 3,
};
const maxLevelToLog = process.env.NODE_ENV === 'dev' ? 'debug' : 'info';
const customFormat = winstonFormat.printf(({ level, message, exception }) => {
return `${level}: ${message}${exception ? '\n' + exception.stack : ''}`;
});
const format = winstonFormat.combine(winstonFormat.timestamp(), winstonFormat.json());
// Tell winston to log to console, file and datadog
const customTransports = [
new transports.Console({ format }),
new transports.DailyRotateFile({
frequency: '24h',
datePattern: 'YYYY-MM-DD',
dirname: logsFolderName,
extension: '.log',
filename: logsFileNameFormat,
handleExceptions: true,
maxSize: '5m',
maxFiles: '30d',
format,
}),
];
return {
level: maxLevelToLog,
levels,
transports: customTransports,
exceptionHandlers: new transports.Console({format: customFormat}),
};
What version of Winston presents the issue?
v3.10.0
What version of Node are you using?
v18.16.1
If this worked in a previous version of Winston, which was it?
🔎 Search Terms
winston exceptions, unhandled exceptions, winston weird exception log etc.
The problem
I'm using winston to log everything in my NestJS app and it works perfectly except for one problem: When an unhandled exception occurs (for example if I throw one on purpose) and passed onto node, winston logs it but in a very weird format:
Sometimes it even logs the same thing 2 or even 3 times in a row. For the life of me I can't figure out how to format this any differently or at least take off some (most) of the properties there are here. This is my logger config:
What version of Winston presents the issue?
v3.10.0
What version of Node are you using?
v18.16.1
If this worked in a previous version of Winston, which was it?
No response
Minimum Working Example
No response
Additional information
No response