Created
January 8, 2019 07:07
-
-
Save noizbuster/fa52cdab30af9bf765e8954dc06492f6 to your computer and use it in GitHub Desktop.
my winston logger
This file contains hidden or 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 winston = require('winston'); | |
require('winston-daily-rotate-file'); | |
// can replace with values on configuration | |
const LOG_CONSOLE_LEVEL = 'debug'; | |
const LOG_CONSOLE_JSON = true; | |
const LOG_FILE_LEVEL = 'info'; | |
const LOG_FILE_NAME = 'VirtualWorkers-%DATE%.log'; | |
// Winston Daily Rotate File : https://github.com/winstonjs/winston-daily-rotate-file | |
let rotatedTransport = new (winston.transports.DailyRotateFile)({ | |
level: LOG_FILE_LEVEL, | |
dirname: './log', | |
filename: LOG_FILE_NAME, | |
datePattern: 'YYYY-MM-DD-HH', | |
zippedArchive: true, | |
maxSize: '20m', | |
maxFiles: '50', | |
format: winston.format.combine(winston.format.timestamp(), winston.format.json()) | |
}); | |
rotatedTransport.on('rotate', function (oldFilename, newFilename) { | |
logger.info('logfile rotated, oldFileName: ' + oldFilename + ', newFileName' + newFilename); | |
}); | |
// Winston Console Transports | |
// https://github.com/winstonjs/winston/blob/master/docs/transports.md | |
let consoleTransport = new winston.transports.Console({ | |
format: winston.format.combine( | |
winston.format.colorize(), | |
winston.format.align(), | |
winston.format.timestamp(), | |
winston.format.printf((info) => { | |
const { | |
timestamp, level, message, ...args | |
} = info; | |
const ts = timestamp.replace('T', ' '); | |
return `${ts} [${level}]: ${message} ${Object.keys(args).length && LOG_CONSOLE_JSON ? '\n' + prettyJ(args) : ''}`; | |
}) | |
), | |
level: LOG_CONSOLE_LEVEL | |
}); | |
const logger = winston.createLogger({ | |
levels: winston.config.syslog.levels, | |
transports: [ | |
consoleTransport, | |
rotatedTransport | |
] | |
}); | |
// colorize stringified json on terminal | |
function prettyJ(json) { | |
if (typeof json !== 'string') { | |
json = JSON.stringify(json, undefined, 2); | |
} | |
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g, | |
function (match) { | |
let cls = "\x1b[36m"; | |
if (/^"/.test(match)) { | |
if (/:$/.test(match)) { | |
cls = "\x1b[34m"; | |
} else { | |
cls = "\x1b[32m"; | |
} | |
} else if (/true|false/.test(match)) { | |
cls = "\x1b[35m"; | |
} else if (/null/.test(match)) { | |
cls = "\x1b[31m"; | |
} | |
return cls + match + "\x1b[0m"; | |
} | |
); | |
} | |
function shortHands(logger) { | |
const mapping = { | |
en: 'emerg', | |
a: 'alert', | |
c: 'crit', | |
e: 'error', | |
w: 'warning', | |
n: 'notice', | |
i: 'info', | |
d: 'debug' | |
}; | |
Object.entries(mapping).forEach((item) => { | |
// logger[item[0]] = logger.log(item[1], ...args); | |
logger[item[0]] = logger[item[1]]; | |
}) | |
} | |
shortHands(logger); | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment