log to file
This commit is contained in:
parent
3446ec01a5
commit
8a231dfd73
@ -76,3 +76,8 @@
|
|||||||
pass=""
|
pass=""
|
||||||
[email.transport.tls]
|
[email.transport.tls]
|
||||||
rejectUnauthorized=false
|
rejectUnauthorized=false
|
||||||
|
|
||||||
|
# Application log file location
|
||||||
|
[logger]
|
||||||
|
write=true
|
||||||
|
file="/var/log/icynet.log"
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
import config from './load-config'
|
||||||
|
import path from 'path'
|
||||||
|
import fs from 'fs'
|
||||||
|
import util from 'util'
|
||||||
|
|
||||||
|
let lfs
|
||||||
|
|
||||||
function pz (z) {
|
function pz (z) {
|
||||||
if (z < 10) {
|
if (z < 10) {
|
||||||
return '0' + z
|
return '0' + z
|
||||||
@ -5,30 +12,41 @@ function pz (z) {
|
|||||||
return z
|
return z
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Time stamp constructor
|
||||||
function dateFormat (date) {
|
function dateFormat (date) {
|
||||||
return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' +
|
return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' +
|
||||||
pz(date.getHours()) + ':' + pz(date.getMinutes()) + ':' + pz(date.getSeconds())
|
pz(date.getHours()) + ':' + pz(date.getMinutes()) + ':' + pz(date.getSeconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Console.log/error/warn "middleware" - add timestamp and write to file
|
||||||
|
function stampAndWrite (fnc, prfx, message) {
|
||||||
|
let prefix = '[' + prfx + '] [' + dateFormat(new Date()) + '] '
|
||||||
|
message = prefix + message
|
||||||
|
|
||||||
|
if (lfs) {
|
||||||
|
lfs.write(message + '\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
fnc.call(this, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reassign logger functions and send them to the "middleware"
|
||||||
const realConsoleLog = console.log
|
const realConsoleLog = console.log
|
||||||
console.log = function () {
|
console.log = function () {
|
||||||
process.stdout.write('\x1b[2K\r')
|
let message = util.format.apply(null, arguments)
|
||||||
process.stdout.write('[info] [' + dateFormat(new Date()) + '] ')
|
stampAndWrite.call(this, realConsoleLog, 'info', message)
|
||||||
realConsoleLog.apply(this, arguments)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const realConsoleWarn = console.warn
|
const realConsoleWarn = console.warn
|
||||||
console.warn = function () {
|
console.warn = function () {
|
||||||
process.stdout.write('\x1b[2K\r')
|
let message = util.format.apply(null, arguments)
|
||||||
process.stdout.write('[warn] [' + dateFormat(new Date()) + '] ')
|
stampAndWrite.call(this, realConsoleWarn, 'warn', message)
|
||||||
realConsoleWarn.apply(this, arguments)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const realConsoleError = console.error
|
const realConsoleError = console.error
|
||||||
console.error = function () {
|
console.error = function () {
|
||||||
process.stderr.write('\x1b[2K\r')
|
let message = util.format.apply(null, arguments)
|
||||||
process.stderr.write('[ err] [' + dateFormat(new Date()) + '] ')
|
stampAndWrite.call(this, realConsoleError, ' err', message)
|
||||||
realConsoleError.apply(this, arguments)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
@ -43,4 +61,15 @@ module.exports = function () {
|
|||||||
console.log('[%s] %s', pid, msg)
|
console.log('[%s] %s', pid, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create log file write stream
|
||||||
|
if (!config.logger || !config.logger.write) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
lfs = fs.createWriteStream(path.resolve(config.logger.file), {flags: 'a'})
|
||||||
|
} catch (e) {
|
||||||
|
lfs = null
|
||||||
|
console.error('Failed to initiate log file write stream')
|
||||||
|
console.error(e.stack)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user