2017-09-08 15:30:00 +00:00
|
|
|
import config from './load-config'
|
|
|
|
import path from 'path'
|
|
|
|
import util from 'util'
|
2017-11-30 21:13:14 +00:00
|
|
|
import fs from 'fs-extra'
|
2017-09-09 11:15:11 +00:00
|
|
|
|
2017-09-08 15:30:00 +00:00
|
|
|
let lfs
|
|
|
|
|
2020-07-06 18:33:00 +00:00
|
|
|
const pz = z => z < 10 ? '0' + z : z
|
2017-08-02 21:24:01 +00:00
|
|
|
|
2017-09-08 15:30:00 +00:00
|
|
|
// Time stamp constructor
|
2017-08-02 21:24:01 +00:00
|
|
|
function dateFormat (date) {
|
|
|
|
return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' +
|
|
|
|
pz(date.getHours()) + ':' + pz(date.getMinutes()) + ':' + pz(date.getSeconds())
|
|
|
|
}
|
|
|
|
|
2017-09-08 15:30:00 +00:00
|
|
|
// Console.log/error/warn "middleware" - add timestamp and write to file
|
2017-12-01 10:42:08 +00:00
|
|
|
function stampAndWrite (fnc, color, prfx, message) {
|
2020-05-28 18:30:21 +00:00
|
|
|
const prefix = '[' + prfx + '] [' + dateFormat(new Date()) + '] '
|
2018-08-15 17:33:20 +00:00
|
|
|
message = color + prefix + message + ((color && color !== '') ? '\x1b[0m' : '')
|
2017-12-01 10:42:08 +00:00
|
|
|
message = message.replace(/\\u001b/g, '\x1b')
|
2017-09-08 15:30:00 +00:00
|
|
|
|
|
|
|
if (lfs) {
|
2020-05-28 18:30:21 +00:00
|
|
|
// eslint-disable-next-line no-control-regex
|
2017-12-01 10:42:08 +00:00
|
|
|
lfs.write(message.replace(/(\u001b\[\d\d?m)/g, '') + '\n')
|
2017-09-08 15:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fnc.call(this, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reassign logger functions and send them to the "middleware"
|
2017-08-02 21:24:01 +00:00
|
|
|
const realConsoleLog = console.log
|
|
|
|
console.log = function () {
|
2020-05-28 18:30:21 +00:00
|
|
|
const message = util.format.apply(null, arguments)
|
2017-12-01 10:42:08 +00:00
|
|
|
stampAndWrite.call(this, realConsoleLog, '', 'info', message)
|
2017-08-02 21:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const realConsoleWarn = console.warn
|
|
|
|
console.warn = function () {
|
2020-05-28 18:30:21 +00:00
|
|
|
const message = util.format.apply(null, arguments)
|
2017-12-01 10:42:08 +00:00
|
|
|
stampAndWrite.call(this, realConsoleWarn, '\x1b[33m', 'warn', message)
|
2017-08-02 21:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const realConsoleError = console.error
|
|
|
|
console.error = function () {
|
2020-05-28 18:30:21 +00:00
|
|
|
const message = util.format.apply(null, arguments)
|
2017-12-01 10:42:08 +00:00
|
|
|
stampAndWrite.call(this, realConsoleError, '\x1b[31m', ' err', message)
|
2017-08-02 21:24:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:15:11 +00:00
|
|
|
async function initializeLogger () {
|
2020-05-28 18:30:21 +00:00
|
|
|
const logPath = path.resolve(config.logger.file)
|
2017-09-09 11:15:11 +00:00
|
|
|
|
2019-01-04 23:10:23 +00:00
|
|
|
// Only throw bad permission errors
|
2017-09-09 11:15:11 +00:00
|
|
|
try {
|
2017-11-30 21:13:14 +00:00
|
|
|
await fs.access(logPath, fs.W_OK)
|
2019-01-04 23:10:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'ENOENT') throw e
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-05-28 18:30:21 +00:00
|
|
|
lfs = fs.createWriteStream(logPath, { flags: 'a' })
|
2017-09-09 11:15:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
lfs = null
|
|
|
|
console.error('Failed to initiate log file write stream')
|
2019-01-04 23:10:23 +00:00
|
|
|
|
|
|
|
// Rethrow error
|
|
|
|
throw e
|
2017-09-09 11:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 14:36:07 +00:00
|
|
|
export function initialize () {
|
|
|
|
// Create log file write stream
|
|
|
|
if (!config.logger || !config.logger.write) return
|
|
|
|
initializeLogger().catch((e) => console.error(e))
|
2017-08-02 21:24:01 +00:00
|
|
|
}
|