Quellcode durchsuchen

Improved logging. Still trying to figure out tests.

master
Bryan Miller vor 5 Jahren
Ursprung
Commit
e467ac85a4
3 geänderte Dateien mit 64 neuen und 35 gelöschten Zeilen
  1. +25
    -23
      app/logging.js
  2. +35
    -12
      test/logging.spec.js
  3. +4
    -0
      wwwterm.js

+ 25
- 23
app/logging.js Datei anzeigen

@@ -1,8 +1,6 @@

const debug = require('debug');


var ENABLE_STATE_LOG = true;
var ENABLE_STATE_INFO = true;
var ENABLE_STATE_DEBUG = true;
var ENABLE_STATE_WARNING = true;
@@ -16,9 +14,8 @@ class Log {
constructor(namespace){
this.__ns = namespace;
this.__d = debug(namespace);
this.__d.enabled = ENABLE_STATE_LOG;
//console.log(debug);
this.__fnc = null;
this.__enabled = ENABLE_STATE_LOG;
this.__ienabled = ENABLE_STATE_INFO;
this.__denabled = ENABLE_STATE_DEBUG;
this.__wenabled = ENABLE_STATE_WARNING;
@@ -29,8 +26,10 @@ class Log {

get namespace(){return this.__ns;}

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

get infoEnabled(){return this.__ienabled;}
set infoEnabled(e){this.__ienabled = (e === true);}
@@ -53,7 +52,7 @@ class Log {
}

log(){
if (!ENABLE_STATE_LOG || !this.__enabled || arguments.length < 2){return;}
if (!this.__d.enabled || arguments.length < 2){return;}
var type = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
if (typeof(args[0]) !== 'string'){return;}
@@ -85,34 +84,28 @@ class Log {
}

info(){
let args = Array.prototype.slice.call(arguments);
args.splice(0, 0, 'i');
this.log.apply(this, args);
this.log.apply(this, ['i'].concat(Array.prototype.slice.call(arguments)));
}

debug(){
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'd');
this.log.apply(this, args);
this.log.apply(this, ['d'].concat(Array.prototype.slice.call(arguments)));
}

warning(){
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'w');
this.log.apply(this, args);
this.log.apply(this, ['w'].concat(Array.prototype.slice.call(arguments)));
}

error(){
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'e');
this.log.apply(this, args);
this.log.apply(this, ['e'].concat(Array.prototype.slice.call(arguments)));
}

critical(){
let args = (Array.prototype.slice(arguments)).splice(0, 0, 'c');
this.log.apply(this, args);
this.log.apply(this, ['c'].concat(Array.prototype.slice.call(arguments)));
}

extend(ens){
var nlog = new LOG(this.__ns + ":" + ens);
nlog.enabled = this.__enabled;
nlog.enabled = this.__d.enable;
nlog.infoEnabled = this.__ienabled;
nlog.debugEnabled = this.__denabled;
nlog.warningEnabled = this.__wenabled;
@@ -128,10 +121,19 @@ class Log {

module.exports = {
Log:Log,
enabled: function(){
if (arguments.length > 0)
ENABLE_STATE_LOG = (arguments[0] === true);
return ENABLE_STATE_LOG;
enable: function(namespace){
let l = LOG_LIST.find((lg)=>{return lg.namespace === namespace;});
if (l)
l.enabled = true;
},

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

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

infoEnabled: function(){

+ 35
- 12
test/logging.spec.js Datei anzeigen

@@ -1,18 +1,41 @@
const expect = require('chai').expect
const logging = require('../app/logging');
var debug = require('debug');

describe('Tests fer app/logging module', function(){
//beforeEach(function(){
//this.debugcb = this.sandbox.spy();
//debug.log = this.debugcb;
//this.debugcb = debugcb;
//this.debug = this.sandbox.stub(require.cache[require.resolve('debug')], 'exports').callsFake(function(){
//return debugcb;
//});
//});
describe('Tests for app/logging module', function(){
beforeEach(function(){
var debugcb = this.sandbox.spy();
debugcb.namespace = "";
debugcb.enabled = true;

it('Test to see if I subbed my toe', function(){
let log = new logging.Log();
//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;
});
});


/*it('Create Log instance', function(){
let log = new (require('../app/logging')).Log("test");
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);
});
});

+ 4
- 0
wwwterm.js Datei anzeigen

@@ -2,3 +2,7 @@ var logging = require('./app/logging.js');

var log = new logging.Log("bob");
log.info("So there!");
log.debug("Debug test.");
log.warning("Warning test.");
log.error("Error test.");
log.critical("Crit test!");

Laden…
Abbrechen
Speichern