Skip to content

Instantly share code, notes, and snippets.

@ezy
Created January 7, 2019 03:05
Show Gist options
  • Select an option

  • Save ezy/5d21049c700ec646921df224e49a1ca6 to your computer and use it in GitHub Desktop.

Select an option

Save ezy/5d21049c700ec646921df224e49a1ca6 to your computer and use it in GitHub Desktop.
Logger setup for winston in node express
const winston = require('winston');
const transports = {
console: new winston.transports.Console({
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug',
handleExceptions: true,
format: winston.format.simple(),
colorize: true,
}),
file: new winston.transports.File({
filename: 'error.log',
level: 'error',
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
}),
};
const logger = winston.createLogger({
transports: [
transports.console,
transports.file,
],
exitOnError: false,
});
if (process.env.NODE_ENV !== 'production') {
logger.debug('Logging initialized at debug level');
}
logger.stream = {
write(message, encoding) {
logger.info(message);
},
};
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment