Last active
April 19, 2021 16:01
-
-
Save amirelemam/5865647f1ea26f181d3e19f448f0f7ae to your computer and use it in GitHub Desktop.
Logging with winston
This file contains 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 customLevels = { | |
levels: { | |
error: 0, | |
warn: 1, | |
info: 2, | |
http: 3, | |
verbose: 4, | |
debug: 5, | |
silly: 6, | |
}, | |
colors: { | |
error: 'red', | |
warn: 'yellow', | |
info: 'green', | |
http: 'blue', | |
verbose: 'white', | |
debug: 'white', | |
silly: 'white', | |
}, | |
}; | |
winston.addColors(customLevels.colors); | |
const logger = winston.createLogger({ | |
transports: [ | |
new winston.transports.Console({ | |
format: winston.format.combine( | |
winston.format.colorize(), | |
winston.format.timestamp({ | |
format: 'YYYY-MM-DDHH:mm:ss', | |
}), | |
winston.format.printf( | |
(info) => `${info.timestamp} ${info.level}: ${info.message}` | |
) | |
), | |
level: process.env.LOGGING_LEVEL || 'info', | |
}), | |
], | |
}); | |
module.exports = logger; | |
module.exports.stream = { | |
write: function (message, encoding) { | |
logger.http(message); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment