Browse Source

Logging now returns a class instance and logs are created with .createLog(). Test suite works now!

master
Bryan Miller 5 years ago
parent
commit
f39d640cfb
2 changed files with 162 additions and 62 deletions
  1. +82
    -34
      app/logging.js
  2. +80
    -28
      test/logging.spec.js

+ 82
- 34
app/logging.js View File



var LOG_LIST = []; var LOG_LIST = [];


function AddToLogList(lg, namespace){
idx = LOG_LIST.findIndex((i)=>{return i.namespace === namespace;});
if (idx < 0){
LOG_LIST.push(lg);
return true;
}
return false;
}


class Log { class Log {
constructor(namespace){ constructor(namespace){
this.__ns = namespace;
this.__d = debug(namespace);
if (AddToLogList(this, namespace)){
this.__d = debug(namespace);
} else {
this.__d = null;
}
//console.log(debug); //console.log(debug);
this.__fnc = null; this.__fnc = null;
this.__ienabled = ENABLE_STATE_INFO; this.__ienabled = ENABLE_STATE_INFO;
this.__wenabled = ENABLE_STATE_WARNING; this.__wenabled = ENABLE_STATE_WARNING;
this.__eenabled = ENABLE_STATE_ERROR; this.__eenabled = ENABLE_STATE_ERROR;
this.__cenabled = ENABLE_STATE_CRITICAL; this.__cenabled = ENABLE_STATE_CRITICAL;
LOG_LIST.push(this);
} }


get namespace(){return this.__ns;}
get valid(){return (this.__d !== null);}
get namespace(){return (this.__d) ? this.__d.namespace : "";}


get enabled(){return this.__d.enabled}
get enabled(){return (this.__d && this.__d.enabled);}
set enabled(e){ set enabled(e){
this.__d.enabled = (e === true);
if (this.__d)
this.__d.enabled = (e === true);
} }


get infoEnabled(){return this.__ienabled;} get infoEnabled(){return this.__ienabled;}
} }


log(){ log(){
if (!this.__d.enabled || arguments.length < 2){return;}
if (!this.enabled || arguments.length < 2){return;}
var type = arguments[0]; var type = arguments[0];
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 1);
if (typeof(args[0]) !== 'string'){return;} if (typeof(args[0]) !== 'string'){return;}
} }


extend(ens){ extend(ens){
var nlog = new LOG(this.__ns + ":" + ens);
nlog.enabled = this.__d.enable;
nlog.infoEnabled = this.__ienabled;
nlog.debugEnabled = this.__denabled;
nlog.warningEnabled = this.__wenabled;
nlog.errorEnabled = this.__eenabled;
nlog.criticalEnabled = this.__cenabled;
if (this.__fnc !== null)
nlog.logFunc = this.__fnc;
return nlog;
if (this.valid){
var nlog = new LOG(this.__ns + ":" + ens);
nlog.enabled = this.__d.enable;
nlog.infoEnabled = this.__ienabled;
nlog.debugEnabled = this.__denabled;
nlog.warningEnabled = this.__wenabled;
nlog.errorEnabled = this.__eenabled;
nlog.criticalEnabled = this.__cenabled;
if (this.__fnc !== null)
nlog.logFunc = this.__fnc;
return nlog;
}
return null;
}

destroy(){
if (this.valid){
this.__d.destroy();
this.__d = null;
}
var idx = LOG_LIST.findIndex((i)=>{return i.namespace === this.namespace;});
if (idx >= 0)
LOG_LIST.splice(idx, 1);
} }
} }






module.exports = {
Log:Log,
enable: function(namespace){
class LoggingSys{
constructor(){}

get length(){return LOG_LIST.length;}

createLog(namespace){
var log = LOG_LIST.find((i)=>{return i.namespace === namespace;});
if (!log)
log = new Log(namespace);
return log;
}

enable(namespace){
let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;}); let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
if (l) if (l)
l.enabled = true; l.enabled = true;
},
}


enabled: function(namespace){
enabled(namespace){
let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;}); let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
return (l) ? l.enabled : false; return (l) ? l.enabled : false;
},
}


disable: function(){
disable(){
LOG_LIST.forEach((l)=>{l.enabled=false;}); LOG_LIST.forEach((l)=>{l.enabled=false;});
},
}


infoEnabled: function(){
infoEnabled(){
if (arguments.length > 0) if (arguments.length > 0)
ENABLE_STATE_INFO = (arguments[0] === true); ENABLE_STATE_INFO = (arguments[0] === true);
return ENABLE_STATE_INFO; return ENABLE_STATE_INFO;
},
}


debugEnabled: function(){
debugEnabled(){
if (arguments.length > 0) if (arguments.length > 0)
ENABLE_STATE_DEBUG = (arguments[0] === true); ENABLE_STATE_DEBUG = (arguments[0] === true);
return ENABLE_STATE_DEBUG; return ENABLE_STATE_DEBUG;
},
}


warningEnabled: function(){
warningEnabled(){
if (arguments.length > 0) if (arguments.length > 0)
ENABLE_STATE_WARNING = (arguments[0] === true); ENABLE_STATE_WARNING = (arguments[0] === true);
return ENABLE_STATE_WARNING; return ENABLE_STATE_WARNING;
},
}


errorEnabled: function(){
errorEnabled(){
if (arguments.length > 0) if (arguments.length > 0)
ENABLE_STATE_ERROR = (arguments[0] === true); ENABLE_STATE_ERROR = (arguments[0] === true);
return ENABLE_STATE_ERROR; return ENABLE_STATE_ERROR;
},
}


criticalEnabled: function(){
criticalEnabled(){
if (arguments.length > 0) if (arguments.length > 0)
ENABLE_STATE_CRITICAL = (arguments[0] === true); ENABLE_STATE_CRITICAL = (arguments[0] === true);
return ENABLE_STATE_CRITICAL; return ENABLE_STATE_CRITICAL;
} }

clear(){
LOG_LIST.forEach((e)=>{e.destroy();});
LOG_LIST = [];
}
}; };





var lsys = new LoggingSys();
module.exports = lsys;




+ 80
- 28
test/logging.spec.js View File



describe('Tests for app/logging module', function(){ describe('Tests for app/logging module', function(){
beforeEach(function(){ beforeEach(function(){
var debugcb = this.sandbox.spy();
debugcb.namespace = "";
debugcb.enabled = true;
if (!this.debug){
var debugcb = this.sandbox.spy();
debugcb.namespace = "";
debugcb.enabled = true;
debugcb.destroy = function(){};


//console.log(require.cache[require.resolve('debug')]);
//console.log(require.cache[require.resolve('debug')]);


this.debugcb = debugcb;
this.debug = this.sandbox.stub(require.cache[require.resolve('debug')], 'exports').callsFake(function(ns){
debugcb.namespace = ns;
return debugcb;
});
this.debugcb = debugcb;
this.debug = this.sandbox.stub(require.cache[require.resolve('debug')], 'exports').callsFake(function(ns){
debugcb.namespace = ns;
return debugcb;
});
}
}); });




/*it('Create Log instance', function(){
let log = new (require('../app/logging')).Log("test");
it('Create Log instance', function(){
let log = (require('../app/logging')).createLog("test");
expect(this.debug.called).to.eql(true); expect(this.debug.called).to.eql(true);
});*/

it('Test info()', function(){
let log = new (require('../app/logging')).Log("test");
let msg = "Test Burger";
log.info(msg);

console.log(log.__d === this.debugcb);
console.log(log.__d);
console.log(this.debugcb);
var cb2 = this.debugcb;
cb2.namespace = 'dummy';
console.log(this.debugcb);
console.log(cb2);
console.log(log.__d);
expect(this.debugcb.called).to.eq(true);
//expect(this.debugcb.calledWith("[ INFO ]: " + msg)).to.eql(true);
});

it('Log instance persists.', function(){
expect((require('../app/logging')).length).to.eql(1);
});

it('No duplicates when same namespace used.', function(){
let lsys = require('../app/logging');
let log = lsys.createLog("test");
expect(lsys.length).to.eql(1);
});

it('Clear all current logs.', function(){
let lsys = require('../app/logging');
lsys.clear();
expect(lsys.length).to.eql(0);
});

describe('Testing core log methods', function(){
after(function(){
(require('../app/logging')).clear();
});

it('.info()', function(){
let log = (require('../app/logging')).createLog("test");
let msg = "Test Burger";
log.info(msg);

expect(this.debugcb.calledWith("[ INFO ]: " + msg)).to.eql(true);
});

it('.debug()', function(){
let log = (require('../app/logging')).createLog("test");
let msg = "Test Burger";
log.debug(msg);

expect(this.debugcb.calledWith("[ DEBUG ]: " + msg)).to.eql(true);
});

it('.warning()', function(){
let log = (require('../app/logging')).createLog("test");
let msg = "Test Burger";
log.warning(msg);

expect(this.debugcb.calledWith("[ WARNING ]: " + msg)).to.eql(true);
});

it('.error()', function(){
let log = (require('../app/logging')).createLog("test");
let msg = "Test Burger";
log.error(msg);

expect(this.debugcb.calledWith("[ ERROR ]: " + msg)).to.eql(true);
});

it('.critical()', function(){
let log = (require('../app/logging')).createLog("test");
let msg = "Test Burger";
log.critical(msg);

expect(this.debugcb.calledWith("[ CRITICAL ]: " + msg)).to.eql(true);
});
}); });
}); });





Loading…
Cancel
Save