enigma-bbs/core/logger.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

/* jslint node: true */
'use strict';
2014-10-17 02:21:06 +00:00
// deps
const bunyan = require('bunyan');
const paths = require('path');
const fs = require('graceful-fs');
const _ = require('lodash');
module.exports = class Log {
static init() {
const Config = require('./config.js').get();
const logPath = Config.paths.logs;
const err = this.checkLogPath(logPath);
if (err) {
console.error(err.message); // eslint-disable-line no-console
return process.exit();
}
const logStreams = [];
if (_.isObject(Config.logging.rotatingFile)) {
Config.logging.rotatingFile.path = paths.join(
logPath,
Config.logging.rotatingFile.fileName
);
logStreams.push(Config.logging.rotatingFile);
}
const serializers = Log.standardSerializers();
this.log = bunyan.createLogger({
name: 'ENiGMA½',
streams: logStreams,
serializers: serializers,
});
}
static standardSerializers() {
const serializers = {
err: bunyan.stdSerializers.err, // handle 'err' fields with stack/etc.
};
2014-10-17 02:21:06 +00:00
// try to remove sensitive info by default, e.g. 'password' fields
['formData', 'formValue', 'user'].forEach(keyName => {
serializers[keyName] = fd => Log.hideSensitive(fd);
});
2014-10-17 02:21:06 +00:00
return serializers;
}
static checkLogPath(logPath) {
try {
if (!fs.statSync(logPath).isDirectory()) {
return new Error(`${logPath} is not a directory`);
}
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(
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}":"********"`;
}
)
);
} catch (e) {
// be safe and return empty obj!
return {};
}
}
2014-10-17 02:21:06 +00:00
};