Last active
April 26, 2022 08:41
-
-
Save emg110/1ab0584ff59b6f216304ec68b0977f7c to your computer and use it in GitHub Desktop.
Winston 3 customization by chalk
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 { createLogger, format, transports } = require('winston'); | |
const chalk = require('chalk'); | |
const { combine, timestamp, label, printf, simple, splat } = format; | |
const consoleFormat = printf(({ level, message, label, timestamp }) => { | |
var levelUpper = level.toUpperCase(); | |
switch (levelUpper) { | |
case "INFO": | |
message = chalk.green(message); | |
level = chalk.black.bgGreenBright.bold(level); | |
break; | |
case "WARN": | |
message = chalk.yellow(message); | |
level = chalk.black.bgYellowBright.bold(level); | |
break; | |
case "ERROR": | |
message = chalk.red(message); | |
level = chalk.black.bgRedBright.bold(level); | |
break; | |
default: | |
break; | |
} | |
return `[${chalk.black.bgBlue.bold(label)}] [${chalk.black.bgWhiteBright(timestamp)}] [${level}]: ${message}`; | |
}); | |
const fileFormat = printf(({ level, message, label, timestamp }) => { | |
return `[${label}] [${timestamp}] [${level}]: ${message}`; | |
}); | |
const logger = createLogger({ | |
level: 'info', | |
format: combine( | |
label({ label: 'YOUR_LABEL' }), | |
timestamp(), | |
format.splat(), | |
consoleFormat | |
), | |
transports: [ | |
new transports.Console(), | |
new transports.File({ | |
filename:'logs/YOUR_LOG_FILE_NAME.log', | |
format: combine( | |
label({ label: 'YOUR_LOG_FILE_NAME' }), | |
timestamp(), | |
format.splat(), | |
fileFormat | |
) | |
}) | |
] | |
}); | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment