UNPKG

logolite

Version:

Lite Virtual Logger and Writer

107 lines (103 loc) 2.42 kB
'use strict'; const LogConfig = require('./config'); const util = require('util'); const dbg = require('debug')('logolite:logmock'); function MockLogger(a) { a = a || {}; a.action = a.action || 'cache'; const b = this; let c = null; let d = null; let e = -1; let f = []; this.log = function (b) { if (g(b)) { switch (a.action) { case 'cache': { f.push(arguments); break; } case 'print': { let a = Array.prototype.slice.call(arguments, 1); let c = util.format.apply(util, a); console.log(new Date().toISOString() + ' [' + b + '] ' + c); break; } } } }; this.has = function (a) { return g(a); }; this._alter = function (a) { a = a || {}; if (a.levels && typeof a.levels === 'object') { // clear the old methods if (d instanceof Array) { d.forEach(function (a) { delete b[a]; }); } // set the new levels c = a.levels; dbg.enabled && dbg('_logLevelMap: %s', JSON.stringify(c)); d = LogConfig.sortLevels(c).map(function (a) { return a.key; }); dbg.enabled && dbg('_logLevels: %s', JSON.stringify(d)); // define the new methods d.forEach(function (a) { b[a] = b.log.bind(b, a); }); // default logging level: all e = d.length - 1; } let f = a.level || a.logLevel; if (f && d.indexOf(f) >= 0) { e = d.indexOf(f); } if (f == 'all') { e = d.length - 1; } dbg.enabled && dbg('_logPosition: %s/%s', e, f); return this; }; this._probe = function () { return f.slice().map(function (a) { a = a || []; return { severity: a[0], payload: a[1] }; }); }; this._reset = function () { let a = this._probe(); f.length = 0; return a; }; let g = function (a) { let b = d.indexOf(a); dbg.enabled && dbg('_isEnabledFor: %s/%s/%s', a, b, e); return b >= 0 && b <= e; }; Object.defineProperty(this, 'messages', { get: function () { return f.slice(); }, set: function (a) {} }); a.logLevel = a.logLevel || a.level || 'all'; a.levels = a.levels || { error: 0, warn: 1, info: 2, debug: 3, trace: 4, verbose: 5 }; this._alter(a); } module.exports = MockLogger;