We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
winston
winston@2
winston@3
node -v
Winston does not log errors, nor catch exceptions.
Winston to catch and log errors and exceptions
Here's my Winston setup:
const setupLogger = () => { try { const logger = require('winston'); const path = require('path'); const logPath = path.normalize(`${__dirname}/logs/`); console.log(`Log path ${logPath}`); const formatMessage = (theLog) => { if (theLog instanceof Error) { return `${theLog.timestamp} ${theLog.level}: ${theLog.message} - StackTrace: ${theLog.stack}`; } else { return `${theLog.timestamp} ${theLog.level}: ${theLog.message}`; } }; const transportDefaults = { handleExceptions: true, exitOnError: false }; const loggerInstance = logger.createLogger({ level: 'info', format: logger.format.combine( logger.format.printf((theLog) => `${theLog.timestamp} ${theLog.level}: ${theLog.message}`), logger.format.timestamp(), logger.format.json() ), transports: [ new logger.transports.File({ filename: `${logPath}combined.log`, ...transportDefaults }), new logger.transports.File({ filename: `${logPath}error.log`, level: 'error', ...transportDefaults }), new logger.transports.File({ filename: `${logPath}warn.log`, level: 'warn', ...transportDefaults }), new logger.transports.File({ filename: `${logPath}info.log`, level: 'info', ...transportDefaults }), new logger.transports.File({ filename: `${logPath}debug.log`, level: 'debug', ...transportDefaults }) ], exceptionHandlers: [ new logger.transports.File({ filename: `${logPath}exceptions.log`, ...transportDefaults }) ] }); if (process.env.NODE_ENV !== 'production') { loggerInstance.add(new logger.transports.Console({ handleExceptions: true, exitOnError: false, format: logger.format.combine( logger.format.errors({ stack: true }), logger.format.colorize(), logger.format.align(), logger.format.printf((theLog) => formatMessage(theLog)), ) })); console.log('Logging to console'); } return loggerInstance; } catch (err) { console.log("Error setting up logger: ", err); process.exit(5); } };
If an exception happens, nothing is saved to any file, nor is it printed to console. It's as if everything is fine and nothing erred.
If I Logger.error(err) in an exception block, then this is printed to file:
Logger.error(err)
{"level":"error","timestamp":"2020-02-02T06:56:07.973Z"}
And this is printed to console:
2020-02-02T06:56:07.973Z error: undefined
The text was updated successfully, but these errors were encountered:
Thanks to #1771 (comment), I found that you need to add the errors formatter to the logger itself. It does not work on the transport.
errors
So, instead of this:
const log = winston.createLogger({ transports: [new winston.transports.Console({ format: combine( errors(), stringify(), cli(), ), })], })
This is necessary:
const log = winston.createLogger({ format: errors(), transports: [new winston.transports.Console({ format: combine( stringify(), cli(), ), })], })
Sorry, something went wrong.
No branches or pull requests
Please tell us about your environment:
winston
version?winston@2
winston@3
node -v
outputs: v13.1.0What is the problem?
Winston does not log errors, nor catch exceptions.
What do you expect to happen instead?
Winston to catch and log errors and exceptions
Other information
Here's my Winston setup:
If an exception happens, nothing is saved to any file, nor is it printed to console. It's as if everything is fine and nothing erred.
If I
Logger.error(err)
in an exception block, then this is printed to file:And this is printed to console:
The text was updated successfully, but these errors were encountered: