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