colorful logs

This commit is contained in:
Evert Prants 2017-12-01 12:42:08 +02:00
parent f8ed8e244d
commit e15dc7902c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -19,16 +19,15 @@ function dateFormat (date) {
} }
// Console.log/error/warn "middleware" - add timestamp and write to file // Console.log/error/warn "middleware" - add timestamp and write to file
function stampAndWrite (fnc, prfx, message) { function stampAndWrite (fnc, color, prfx, message) {
let prefix = '[' + prfx + '] [' + dateFormat(new Date()) + '] ' let prefix = '[' + prfx + '] [' + dateFormat(new Date()) + '] '
message = prefix + message message = color + prefix + message
message = message.replace(/\\u001b/g, '\x1b')
if (lfs) { if (lfs) {
lfs.write(message + '\n') lfs.write(message.replace(/(\u001b\[\d\d?m)/g, '') + '\n')
} }
message = message.replace(/\\u001b/g, '\x1b')
fnc.call(this, message) fnc.call(this, message)
} }
@ -36,19 +35,19 @@ function stampAndWrite (fnc, prfx, message) {
const realConsoleLog = console.log const realConsoleLog = console.log
console.log = function () { console.log = function () {
let message = util.format.apply(null, arguments) let message = util.format.apply(null, arguments)
stampAndWrite.call(this, realConsoleLog, 'info', message) stampAndWrite.call(this, realConsoleLog, '', 'info', message)
} }
const realConsoleWarn = console.warn const realConsoleWarn = console.warn
console.warn = function () { console.warn = function () {
let message = util.format.apply(null, arguments) let message = util.format.apply(null, arguments)
stampAndWrite.call(this, realConsoleWarn, 'warn', message) stampAndWrite.call(this, realConsoleWarn, '\x1b[33m', 'warn', message)
} }
const realConsoleError = console.error const realConsoleError = console.error
console.error = function () { console.error = function () {
let message = util.format.apply(null, arguments) let message = util.format.apply(null, arguments)
stampAndWrite.call(this, realConsoleError, ' err', message) stampAndWrite.call(this, realConsoleError, '\x1b[31m', ' err', message)
} }
async function initializeLogger () { async function initializeLogger () {