Created
April 2, 2024 09:39
-
-
Save akash-gajjar/b7aaf83c66148b5daa5c79643bb042ff to your computer and use it in GitHub Desktop.
Logger using 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
import dotenv from 'dotenv'; | |
import winston, { addColors, format } from 'winston'; | |
dotenv.config(); | |
const formatParams = (info) => { | |
const { timestamp, level, message, label, ...args } = info; | |
return `${timestamp} ${level} ${label}: ${message} ${Object.keys(args).length ? JSON.stringify(args, '', '') : ''}`; | |
}; | |
const alignColorsAndTime = format.combine( | |
format.colorize({ all: true }), | |
format.label({ label: '[LOGGER]' }), | |
format.timestamp(), | |
format.align(), | |
format.printf(formatParams), | |
); | |
addColors({ | |
info: 'bold blue', // fontStyle color | |
warn: 'italic yellow', | |
error: 'bold red', | |
debug: 'green', | |
}); | |
const Logger = winston.createLogger({ | |
transports: [ | |
new winston.transports.Console({ | |
format: winston.format.combine(winston.format.colorize(), alignColorsAndTime), | |
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug', | |
prettyPrint: true, | |
colorize: true, | |
timestamp: true, | |
}), | |
], | |
}); | |
export default Logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment