UNPKG

logolite

Version:

Lite Virtual Logger and Writer

458 lines (457 loc) 11.5 kB
'use strict'; const LogConfig = require('./config'); const LogFormat = require('./format'); const DEFAULT_PARENT_ID_NAME = '_parentId_'; const DEFAULT_NODE_ID_NAME = '_nodeId_'; const DEFAULT_NODE_TYPE_NAME = '_nodeType_'; function LogTracer(a) { a = a || { key: 'instanceId', value: LogConfig.DEFAULT_INSTANCE_ID }; let b = a.parent; let c = a.key; let d = a.value; let e = {}; let f = []; let g = LogConfig.TRACKING_DEPTH; let h = LogConfig.IS_TRACING_ID_PREDEFINED; Object.defineProperties(this, { 'parent': { get: function () { return b; }, set: function (a) {} }, 'key': { get: function () { return c; }, set: function (a) {} }, 'value': { get: function () { return d; }, set: function (a) {} } }); this.clear = function () { let a = Object.keys(e); if (a.length > f.length) { a.forEach(function (a) { if (f.indexOf(a) < 0) delete e[a]; }); } return this; }; this.reset = function () { let a = this; e = __clearMap(e); if (h) { if (a.parent) { e[DEFAULT_PARENT_ID_NAME] = a.parent.value; } e[DEFAULT_NODE_ID_NAME] = d; e[DEFAULT_NODE_TYPE_NAME] = c; } else { if (g == 1) { if (a.parent) { e[a.parent.key] = a.parent.value; } } else if (g > 1) { let b = a.parent; while (b) { e[b.key] = b.value; b = b.parent; } } e[c] = d; } f = Object.keys(e); return this; }; this.branch = function (a) { return new LogTracer({ parent: this, key: a.key, value: a.value || LogConfig.getLogID() }); }; this.copy = function () { return new LogTracer(this); }; this.get = function (a) { return e[a]; }; this.put = function (a, b) { if (a && b) e[a] = b; return this; }; this.add = function (a) { if (a) { Object.keys(a).forEach(function (b) { e[b] = a[b]; }); } return this; }; this.toMessage = function (a, f) { a = a || {}; if (LogConfig.IS_FULL_LOG_MODE) { // build the #message part let b = a.note; if (!b) { const c = a.text || a.tmpl || a.template; if (c) { b = LogFormat(c, e); } b = b || 'none'; } // build the #context part let c = null; if (a.info) { c = LogConfig.stringify(a.info); } else { c = LogConfig.stringify(e); } // build the #extraInfo part let d = '{}'; // build the #tags part let f = '[]'; if (a.tags) { f = LogConfig.stringify(a.tags); } // build the output message let g = b + LogConfig.PREFIX_OF_INFO + c + LogConfig.PREFIX_OF_EXTRA + d + LogConfig.PREFIX_OF_TAGS + f; // return / exit return g; } if (f === 'direct') { let b = a.text || a.tmpl || a.template; let c = b && LogFormat(b, e) || LogConfig.stringify(e); if (a.info) { c = c + " - Info: " + LogConfig.stringify(a.info); } if (a.tags) { c = c + " - Tags: " + LogConfig.stringify(a.tags); } a.reset && this.reset(); a.clear !== false && this.clear(); return c; } let g = null, h, i, j; if (LogConfig.IS_INTERCEPTOR_ENABLED && BOX.interceptors.length > 0) { h = LogConfig.clone(e); } if (LogConfig.IS_TAGS_EMBEDDABLE || LogConfig.IS_INTERCEPTOR_ENABLED) { i = a.tags instanceof Array && LogConfig.clone(a.tags); } if (LogConfig.IS_TEXT_EMBEDDABLE || LogConfig.IS_TEMPLATE_APPLIED) { let b = a.text || a.tmpl || a.template; j = b && LogFormat(b, e); } if (LogConfig.IS_TAGS_EMBEDDABLE && i) { e[LogConfig.TAGS_FIELD_NAME] = i; } if (LogConfig.IS_TEXT_EMBEDDABLE && j) { e[LogConfig.TEXT_FIELD_NAME] = j; } if (LogConfig.IS_STRINGIFY_ENABLED || a.stringify) { if (LogConfig.IS_TEMPLATE_APPLIED && j) { g = j; } else { g = LogConfig.stringify(e); } } else { g = LogConfig.clone(e); } if (LogConfig.IS_INTERCEPTOR_ENABLED && BOX.interceptors.length > 0) { if (LogConfig.IS_TAGS_EMBEDDABLE && i) { h[LogConfig.TAGS_FIELD_NAME] = i; } if (LogConfig.IS_TEXT_EMBEDDABLE && j) { h[LogConfig.TEXT_FIELD_NAME] = j; } BOX.interceptors.forEach(function (a) { a(h, i, c, d, b); }); } a.reset && this.reset(); a.clear !== false && this.clear(); return g; }; this.stringify = function (a) { a = a || {}; a.stringify = true; return this.toMessage(a); }; this.toString = function (a) { return this.stringify(a); }; Object.defineProperties(this, { getLogID: { get: function () { return LogConfig.getLogID; }, set: function (a) {} } }); this.reset(); } const BOX = { ROOT: null, interceptors: [] }; Object.defineProperties(LogTracer, { ROOT: { get: function () { return BOX.ROOT = BOX.ROOT || new LogTracer(); }, set: function (a) {} }, reset: { get: function () { return function () { BOX.ROOT = null; return LogTracer; }; }, set: function (a) {} }, empty: { get: function () { return function () { Array.prototype.forEach.call(arguments, function (a) { if (a && typeof a === 'object') { Object.keys(a).forEach(function (b) { delete a[b]; }); } }); return LogTracer; }; }, set: function (a) {} } }); Object.defineProperties(LogTracer, { getLogID: { get: function () { return LogConfig.getLogID; }, set: function (a) {} }, isInterceptorEnabled: { get: function () { return LogConfig.IS_INTERCEPTOR_ENABLED; }, set: function (a) {} }, addInterceptor: { get: function () { return function (a) { if (typeof a === 'function' && BOX.interceptors.indexOf(a) < 0) { BOX.interceptors.push(a); return true; } return false; }; }, set: function (a) {} }, removeInterceptor: { get: function () { return function (a) { let b = BOX.interceptors.indexOf(a); if (b >= 0) { BOX.interceptors.splice(b, 1); return true; } return false; }; }, set: function (a) {} }, clearInterceptors: { get: function () { return function () { BOX.interceptors.length = 0; return true; }; }, set: function (a) {} }, countInterceptors: { get: function () { return function () { return BOX.interceptors.length; }; }, set: function (a) {} }, accumulationAppender: { get: function () { return function (a, b, c, d) { if (isInvalidHelper(a, b, c)) return; for (let e = 0; e < b.length; e++) { if (!b[e].matchingPath) { if (typeof b[e].matchingField === 'string') { b[e].matchingPath = b[e].matchingField.split(',').map(function (a) { return a.trim(); }); } if (b[e].matchingField instanceof Array) { b[e].matchingPath = b[e].matchingField; } } let f = b[e].selectedFields; let g = b[e].countTo || b[e].counterField; let h = b[e].storeTo || b[e].storageField; let i = matchField(b[e].matchingRule, b[e].matchingPath, c); let j = matchTags(b[e].anyTags, b[e].allTags, d); let k = matchFilter(b[e].filter, c, d); if (checkConditionals(i, j, k)) { if (g) { a[g] = (a[g] || 0) + 1; } if (h) { a[h] = a[h] || []; if (f && c) { if (!(f instanceof Array)) { f = [f]; } let b = {}; f.forEach(function (a) { b[a] = c[a]; }); a[h].push(b); } else { a[h].push(LogConfig.clone(c)); } } } } }; }, set: function (a) {} }, setupDefaultInterceptors: { get: function () { return function (a) { LogTracer.clearInterceptors(); if (!(a instanceof Array)) { throw new Error('descriptors is not an array'); } a.forEach(function (a, b) { if (!a) { throw new Error('descriptors[' + b + '] is empty'); } if (!a.accumulator || typeof a.accumulator !== 'object') { throw new Error('descriptors[' + b + '].accumulator is not an object'); } if (!(a.mappings instanceof Array)) { throw new Error('descriptors[' + b + '].mappings is not an array'); } let c = LogTracer.accumulationAppender.bind(null, a.accumulator, a.mappings); LogTracer.addInterceptor(c); }); }; }, set: function (a) {} } }); function __clearMap(a) { Object.keys(a).forEach(function (b) { delete a[b]; }); return a; } function checkConditionals() { let a = false; for (let b = 0; b < arguments.length; b++) { if (arguments[b] === true) { a = true; } else if (arguments[b] === false) { a = false; break; } } return a; } function isInvalidHelper(a, b, c) { if (!a || !(typeof a === 'object')) return true; if (!b || !(b instanceof Array)) return true; if (!c || !(typeof c === 'object')) return true; return false; } function hasAnyTags(a, b) { if (!(a instanceof Array)) return false; for (let c = 0; c < b.length; c++) { if (typeof b[c] === 'string') { if (a.indexOf(b[c]) >= 0) return true; } else if (b[c] instanceof RegExp && a.some(function (a) { return a.match(b[c]) != null; })) return true; } return false; } function hasAllTags(a, b) { if (!(a instanceof Array)) return false; for (let c = 0; c < b.length; c++) { if (typeof b[c] === 'string') { if (a.indexOf(b[c]) < 0) return false; } else if (b[c] instanceof RegExp) { if (a.every(function (a) { return a.match(b[c]) == null; })) return false; } else return false; } return true; } function matchTags(a, b, c) { if (typeof c === 'string') c = [c]; let d = null; if (d !== false && a) { if (typeof a === 'string') a = [a]; if (a instanceof Array) { d = hasAnyTags(c, a); } } if (d !== false && b) { if (typeof b === 'string') b = [b]; if (b instanceof Array) { d = hasAllTags(c, b); } } return d; } function matchField(a, b, c) { let d = null; if (!(c && typeof c === 'object' && b instanceof Array)) return d; let e = c; for (let d = 0; d < b.length; d++) { e = e && e[b[d]]; } if (a instanceof RegExp) { d = typeof e === 'string' && e.match(a) != null; } else if (a instanceof Array) { d = a.indexOf(e) >= 0; } else if (a instanceof Function) { d = a(e) == true; } else if (typeof a === 'string') { d = e == a; } return d; } function matchFilter(a, b, c) { let d = null; if (a instanceof Function) { d = a(b, c) == true; } return d; } module.exports = LogTracer;