Skip to content

Instantly share code, notes, and snippets.

@kamikat
Created November 13, 2018 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamikat/577084d2f63f4d0125f76e59777baa89 to your computer and use it in GitHub Desktop.
Save kamikat/577084d2f63f4d0125f76e59777baa89 to your computer and use it in GitHub Desktop.
Colorized logger of winston@3.x (boilerplate)
const process = require('process');
const winston = require('winston');
const clc = require('cli-color');
const {
TERM = '',
LOG_LEVEL = 'info'
} = process.env;
const colors = ~TERM.indexOf('256') ? {
silly: clc.xterm(33),
debug: clc.xterm(207),
info: clc.xterm(48),
warn: clc.xterm(226),
error: clc.xterm(196),
verbose: clc.xterm(8),
} : {
silly: clc.blue,
debug: clc.cyan,
info: clc.green,
warn: clc.yellow,
error: clc.red,
verbose: clc.white,
};
const logger = winston.createLogger({
level: LOG_LEVEL,
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.splat(),
winston.format.timestamp(),
winston.format.printf(
({ level, message, timestamp }) =>
colors[level](`${timestamp} [${level.toUpperCase()}] ${message}`)
),
),
}),
]
});
logger.colors = colors;
module.exports = logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment