Created
November 24, 2021 14:53
-
-
Save msdigital/4a0d97bb7be823595c92263bbfed3ec1 to your computer and use it in GitHub Desktop.
Simple NodeJS Winston Logger with different Log Levels
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 logLevels = { | |
levels: { | |
emerg: 0, | |
alert: 1, | |
crit: 2, | |
error: 3, | |
warning: 4, | |
notice: 5, | |
info: 6, | |
debug: 7 | |
}, | |
colors: { | |
emerg: 'red', | |
alert: 'red', | |
crit: 'red', | |
error: 'red', | |
warning: 'yellow', | |
notice: 'blue', | |
info: 'green', | |
debug: 'green' | |
} | |
} | |
winston.addColors(logLevels) | |
const logger = winston.createLogger({ | |
levels: logLevels.levels, | |
transports: [ | |
new winston.transports.Console({ | |
colorize: true, | |
prettyPrint: true | |
}) | |
], | |
format: winston.format.printf(log => `[${log.level.toUpperCase()}] - ${log.message}`), | |
}); | |
logger.JSON = function(value){ | |
this.info(JSON.stringify(value)); | |
} | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment