Created
September 7, 2023 16:34
-
-
Save senthilmpro/09dc4a839d445f3cbc663b7d5332771e to your computer and use it in GitHub Desktop.
Winston Logger colorize using Node.js (code snippet)
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
// open terminal -> "npm install winston -S" | |
const winston = require('winston'); | |
// snippet from https://stackoverflow.com/questions/51012150/winston-3-0-colorize-whole-output-on-console | |
let alignColorsAndTime = winston.format.combine( | |
winston.format.colorize({ | |
all:true | |
}), | |
winston.format.label({ | |
label:'[LOGGER]' | |
}), | |
winston.format.timestamp({ | |
format:"YY-MM-DD HH:mm:ss" | |
}), | |
winston.format.printf( | |
info => ` ${info.label} ${info.timestamp} ${info.level} : ${info.message}` | |
) | |
); | |
const logger = winston.createLogger({ | |
level: 'info', | |
format: winston.format.combine(winston.format.colorize(), alignColorsAndTime), | |
defaultMeta: { service: 'user-service' }, | |
transports: [ | |
new winston.transports.File({ filename: 'error.log', level: 'error' }), | |
new winston.transports.File({ filename: 'combined.log' }), | |
], | |
}); | |
if (process.env.NODE_ENV !== 'production') { | |
logger.add(new winston.transports.Console({ | |
format: winston.format.simple(), | |
})); | |
} | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment