import config from './load-config' import path from 'path' import util from 'util' import Promise from 'bluebird' const fs = Promise.promisifyAll(require('fs')) let lfs function pz (z) { if (z < 10) { return '0' + z } return z } // Time stamp constructor function dateFormat (date) { return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' + 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') } message = message.replace(/\\u001b/g, '\x1b') fnc.call(this, message) } // Reassign logger functions and send them to the "middleware" const realConsoleLog = console.log console.log = function () { let message = util.format.apply(null, arguments) stampAndWrite.call(this, realConsoleLog, 'info', message) } const realConsoleWarn = console.warn console.warn = function () { let message = util.format.apply(null, arguments) stampAndWrite.call(this, realConsoleWarn, 'warn', message) } const realConsoleError = console.error console.error = function () { let message = util.format.apply(null, arguments) stampAndWrite.call(this, realConsoleError, ' err', message) } async function initializeLogger () { let logPath = path.resolve(config.logger.file) try { await fs.accessAsync(logPath, fs.W_OK) lfs = fs.createWriteStream(logPath, {flags: 'a'}) } catch (e) { lfs = null console.error('Failed to initiate log file write stream') console.error(e.stack) } } module.exports = function () { this.logProcess = (pid, msg) => { if (msg.indexOf('warn') === 0) { msg = msg.substring(5) console.warn('[%s] %s', pid, msg) } else if (msg.indexOf('error') === 0) { msg = msg.substring(6) console.error('[%s] %s', pid, msg) } else { console.log('[%s] %s', pid, msg) } } // Create log file write stream if (!config.logger || !config.logger.write) return initializeLogger() }