Skip to content

Instantly share code, notes, and snippets.

@akash-gajjar
Created April 2, 2024 09:39
Show Gist options
  • Save akash-gajjar/b7aaf83c66148b5daa5c79643bb042ff to your computer and use it in GitHub Desktop.
Save akash-gajjar/b7aaf83c66148b5daa5c79643bb042ff to your computer and use it in GitHub Desktop.
Logger using winston
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