logolite
Version:
Lite Virtual Logger and Writer
142 lines (139 loc) • 3.52 kB
JavaScript
;
const debug = require('debug');
const LogConfig = require('./config');
const LogTracer = require('./tracer');
const MockLogger = require('./logmock');
function Logger(a) {
a = a || {};
let b = null;
let c = function () {
return debug(a.sector || a.scope || LogConfig.DEFAULT_SECTOR);
};
let d = function (b) {
return a.target === 'conlog' || LogConfig.IS_DEBUGLOG_ENABLED || LogConfig.DEBUGLOG_NAMES.indexOf('all') >= 0 || LogConfig.DEBUGLOG_NAMES.indexOf(b) >= 0;
};
this.has = function (e) {
let f = a.mappings && a.mappings[e] || e;
if (LogConfig.isAlwaysEnabledFor(f)) return true;
if (d(f)) {
b = b || c();
return b.enabled;
}
let g = a.store.rootLogger;
if (g && typeof g.has === 'function') {
return g.has(f);
}
return g != null && g[f] !== undefined;
};
this.log = function (e) {
let f = a.mappings && a.mappings[e] || e;
if (!LogConfig.isAlwaysMutedFor(f)) {
if (d(f)) {
let a = Array.prototype.slice.call(arguments, 1);
b = b || c();
b.apply(null, a);
return;
}
let g = a.store.rootLogger;
if (g) {
if (typeof g.log === 'function') {
if (f !== e) arguments[0] = f;
g.log.apply(g, arguments);
} else if (typeof g[f] === 'function') {
let a = Array.prototype.slice.call(arguments, 1);
g[f].apply(g, a);
}
}
}
if (a.store.interceptors.length > 0) {
let b = arguments;
if (f !== e) b[0] = f;
a.store.interceptors.forEach(function (a) {
a.log.apply(a, b);
});
}
};
}
let LogAdapter = function () {
let a = {
rootLogger: null,
isInfoSent: false,
interceptors: []
};
let b = null;
this.getLogger = function (b) {
b = b || {};
b.store = a;
return new Logger(b);
};
this.getRootLogger = function () {
return a && a.rootLogger;
};
this.addInterceptor = function (b) {
if (c(b)) {
a.interceptors.push(b);
}
return this;
};
this.removeInterceptor = function (b) {
let c = a.interceptors.indexOf(b);
if (c >= 0) {
a.interceptors.splice(c, 1);
}
return this;
};
this.clearInterceptors = function () {
a.interceptors.length = 0;
return this;
};
this.countInterceptors = function () {
return a.interceptors.length;
};
this.connectTo = function (c, d) {
// @Deprecated
if (LogConfig.IS_MOCKLOGGER_ENABLED) {
c = b = b || new MockLogger({
level: 'all'
});
}
let e = c && c !== a.rootLogger;
if (e) {
a.rootLogger = c;
}
if (e || !a.isInfoSent) {
if (typeof d === 'string') {
d = {
onLevel: d
};
}
d = d || {};
d.onLevel = d.onLevel || 'info';
this.getLogger({
mappings: d.mappings
}).log(d.onLevel, LogTracer.ROOT.add(LogConfig.libraryInfo).toMessage({
tags: ['logolite-metadata'],
reset: true
}));
a.isInfoSent = true;
}
};
this.reset = function () {
Object.keys(a).forEach(function (b) {
if (b === 'rootLogger') {
a[b] = null;
} else if (b === 'isInfoSent') {
a[b] = false;
} else if (b === 'interceptors') {
a[b].length = 0;
} else {
delete a[b];
}
});
return this;
};
// this.connectTo(null);
let c = function (a) {
return a && typeof a.log === 'function';
};
};
module.exports = new LogAdapter();