2014-11-13 06:16:47 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-09-04 23:46:28 +00:00
|
|
|
// deps
|
|
|
|
const bunyan = require('bunyan');
|
|
|
|
const paths = require('path');
|
2017-05-20 03:20:19 +00:00
|
|
|
const fs = require('graceful-fs');
|
2016-09-04 23:46:28 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
module.exports = class Log {
|
|
|
|
|
|
|
|
static init() {
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const logPath = Config.paths.logs;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-09-04 23:46:28 +00:00
|
|
|
const err = this.checkLogPath(logPath);
|
|
|
|
if(err) {
|
|
|
|
console.error(err.message); // eslint-disable-line no-console
|
|
|
|
return process.exit();
|
2015-04-24 03:42:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 23:46:28 +00:00
|
|
|
const logStreams = [];
|
|
|
|
if(_.isObject(Config.logging.rotatingFile)) {
|
|
|
|
Config.logging.rotatingFile.path = paths.join(logPath, Config.logging.rotatingFile.fileName);
|
|
|
|
logStreams.push(Config.logging.rotatingFile);
|
2015-04-24 03:42:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 23:46:28 +00:00
|
|
|
const serializers = {
|
2018-06-14 02:58:59 +00:00
|
|
|
err : bunyan.stdSerializers.err, // handle 'err' fields with stack/etc.
|
2016-09-04 23:46:28 +00:00
|
|
|
};
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
// try to remove sensitive info by default, e.g. 'password' fields
|
2016-09-04 23:46:28 +00:00
|
|
|
[ 'formData', 'formValue' ].forEach(keyName => {
|
2018-06-14 02:58:59 +00:00
|
|
|
serializers[keyName] = (fd) => Log.hideSensitive(fd);
|
2016-09-04 23:46:28 +00:00
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
this.log = bunyan.createLogger({
|
2016-09-04 23:46:28 +00:00
|
|
|
name : 'ENiGMA½ BBS',
|
|
|
|
streams : logStreams,
|
|
|
|
serializers : serializers,
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
}
|
2016-09-04 23:46:28 +00:00
|
|
|
|
|
|
|
static checkLogPath(logPath) {
|
|
|
|
try {
|
|
|
|
if(!fs.statSync(logPath).isDirectory()) {
|
|
|
|
return new Error(`${logPath} is not a directory`);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-09-04 23:46:28 +00:00
|
|
|
return null;
|
|
|
|
} catch(e) {
|
|
|
|
if('ENOENT' === e.code) {
|
|
|
|
return new Error(`${logPath} does not exist`);
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static hideSensitive(obj) {
|
|
|
|
try {
|
|
|
|
//
|
|
|
|
// Use a regexp -- we don't know how nested fields we want to seek and destroy may be
|
|
|
|
//
|
|
|
|
return JSON.parse(
|
2017-10-30 02:03:49 +00:00
|
|
|
JSON.stringify(obj).replace(/"(password|passwordConfirm|key|authCode)"\s?:\s?"([^"]+)"/, (match, valueName) => {
|
2016-09-04 23:46:28 +00:00
|
|
|
return `"${valueName}":"********"`;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch(e) {
|
|
|
|
// be safe and return empty obj!
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|