simple-node-framework
Version:
Simple nodeJs framework that provides easy ways to use log, cache, database, session, redis, share request scope and more.
87 lines (70 loc) • 2.69 kB
JavaScript
const Scope = require('../scope');
const Helpers = require('../util/helpers');
const logger = require('../log').instance;
// this class provides log and scope features to classes
class Loggable extends Scope {
constructor({ module }) {
super({ module });
this.logger = logger;
this.module = module;
this.configureLog();
}
// put the request id and user in the scope.
// this method have to be MANUALY called on the first line of the application controller
// super.activateRequestLog(req);
activateRequestLog(req) {
const requestId = Helpers.requestId(req);
this.addScope({...req._scope,request_id: requestId, user: req.user})
}
// put in the log all the scope variables
addScopeToLog(obj = {}) {
if (!obj) return undefined;
// clone if its an object
const _obj = (typeof obj === 'object') ? Object.assign({}, obj) : { obj };
if (this.scope) {
// include natural field to add request_id
_obj.natural = _obj.natural || {};
_obj.natural.scope = _obj.natural.scope || {};
Object.keys(this.scope).map(key => {
if (key !== '_chain') {
_obj.natural.scope[key] = this.scope[key];
}
});
// resolvendo o Bug que fazia o stack nao ser logado
if (obj instanceof Error) {
_obj.stacktrace = obj.stack || obj.stacktrace;
_obj.message = obj.message;
}
}
return _obj;
}
// create the overloads to automacaly repass the module name and the scope to the log
configureLog() {
this.log = {};
this.log.info = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.info(this.module, msg, _obj);
};
this.log.warn = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.info(this.module, msg, _obj);
};
this.log.debug = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.debug(this.module, msg, _obj);
};
this.log.trace = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.trace(this.module, msg, _obj);
};
this.log.error = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.error(this.module, msg, _obj);
};
this.log.fatal = (msg, obj) => {
const _obj = this.addScopeToLog(obj);
this.logger.fatal(this.module, msg, _obj);
};
}
}
module.exports = Loggable;