Created
January 7, 2019 03:05
-
-
Save ezy/5d21049c700ec646921df224e49a1ca6 to your computer and use it in GitHub Desktop.
Logger setup for winston in node express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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