My logger is set up like:
const myFormat = printf(info => {
return `${info.timestamp}: ${info.level}: ${info.message}: ${info.err}`;
});
const logger =
winston.createLogger({
level: "info",
format: combine(timestamp(), myFormat),
transports: [
new winston.transports.File({
filename:
"./logger/error.log",
level: "error"
}),
new winston.transports.File({
filename:
"./logger/info.log",
level: "info"
})
]
})
Then I am logging out some error like this:
logger.error(`GET on /history`, { err });
How is it possible to log the full stack trace for errors to via the error transport? I tried passing in the err.stack and it came out as undefined.
Thanks !
Here is another take for Winston 3.2.
Nowadays Winston comes with a built-in stacktrace formatter, but it does not seem to trigger if the same formatter has
winston.format.simple()
combined. Thus, you need usewinston.format.printf
instead as by the answer from Kirai Mali. I could not figure out how to configure bothwinston.format.errors()
andwinston.format.simple()
in the same configuration.Based on the current Winston README example and answers above, here is my configuration that uses JSON format logfiles, but for the local development console it still gives colored log lines and good stack traces.
Here is my logger configuration. Added
errors({ stack: true })
, thanks to Murli Prajapati ans and small trick in printf function. My winston version is3.2.1
.I am using this same configuration in
express-winston
and for general log also.@Ming's answer got me partly there, but to have a string description with the error, this is how I got full stack tracing working on ours:
You can write a formatter to pass
error.stack
to log.(the output will depend on your configuration)
For winston version
3.2.0+
, following will add stacktrace to log output:Ref: https://github.com/winstonjs/winston/issues/1338#issuecomment-482784056
Here is my
logger.js
withwinston": "^3.1.0