This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
IcyNet.eu/scripts/logger.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-08-02 21:24:01 +00:00
function pz (z) {
if (z < 10) {
return '0' + z
}
return z
}
function dateFormat (date) {
return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' +
pz(date.getHours()) + ':' + pz(date.getMinutes()) + ':' + pz(date.getSeconds())
}
const realConsoleLog = console.log
console.log = function () {
process.stdout.write('\x1b[2K\r')
process.stdout.write('[info] [' + dateFormat(new Date()) + '] ')
realConsoleLog.apply(this, arguments)
}
const realConsoleWarn = console.warn
console.warn = function () {
process.stdout.write('\x1b[2K\r')
process.stdout.write('[warn] [' + dateFormat(new Date()) + '] ')
realConsoleWarn.apply(this, arguments)
}
const realConsoleError = console.error
console.error = function () {
2017-08-28 22:36:13 +00:00
process.stderr.write('\x1b[2K\r')
process.stderr.write('[ err] [' + dateFormat(new Date()) + '] ')
2017-08-02 21:24:01 +00:00
realConsoleError.apply(this, arguments)
}
module.exports = function () {
this.logProcess = (pid, msg) => {
if (msg.indexOf('warn') === 0) {
msg = msg.substring(5)
console.warn('[%s] %s', pid, msg)
2017-08-02 21:24:01 +00:00
} else if (msg.indexOf('error') === 0) {
msg = msg.substring(6)
console.error('[%s] %s', pid, msg)
} else {
console.log('[%s] %s', pid, msg)
2017-08-02 21:24:01 +00:00
}
}
}