Przeglądaj źródła

Work made on a loggin mechanism using the debug module. Test suite... kinda started.

master
Bryan Miller 6 lat temu
rodzic
commit
608b3c0e99
4 zmienionych plików z 190 dodań i 0 usunięć
  1. +167
    -0
      app/logging.js
  2. +18
    -0
      test/logging.spec.js
  3. +1
    -0
      test/test-setup.spec.js
  4. +4
    -0
      wwwterm.js

+ 167
- 0
app/logging.js Wyświetl plik

@@ -0,0 +1,167 @@

const debug = require('debug');


var ENABLE_STATE_LOG = true;
var ENABLE_STATE_INFO = true;
var ENABLE_STATE_DEBUG = true;
var ENABLE_STATE_WARNING = true;
var ENABLE_STATE_ERROR = true;
var ENABLE_STATE_CRITICAL = true;

var LOG_LIST = [];


class Log {
constructor(namespace){
this.__ns = namespace;
this.__d = debug(namespace);
this.__d.enabled = ENABLE_STATE_LOG;
this.__fnc = null;
this.__enabled = ENABLE_STATE_LOG;
this.__ienabled = ENABLE_STATE_INFO;
this.__denabled = ENABLE_STATE_DEBUG;
this.__wenabled = ENABLE_STATE_WARNING;
this.__eenabled = ENABLE_STATE_ERROR;
this.__cenabled = ENABLE_STATE_CRITICAL;
LOG_LIST.push(this);
}

get namespace(){return this.__ns;}

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

get infoEnabled(){return this.__ienabled;}
set infoEnabled(e){this.__ienabled = (e === true);}

get debugEnabled(){return this.__denabled;}
set debugEnabled(e){this.__denabled = (e === true);}

get warningEnabled(){return this.__wenabled;}
set warningEnabled(e){this.__wenabled = (e === true);}

get errorEnabled(){return this.__eenabled;}
set errorEnabled(e){this.__eenabled = (e === true);}

get criticalEnabled(){return this.__cenabled;}
set criticalEnabled(e){this.__cenabled = (e === true);}

set logFunc(f){
this.__fnc = f;
this.__d.log = f;
}

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

switch(type){
case "d":
if (!ENABLE_STATE_DEBUG || !this.__denabled){return;}
args[0] = "[ DEBUG ]: " + args[0];
break;
case "w":
if (!ENABLE_STATE_WARNING || !this.__wenabled){return;}
args[0] = "[ WARNING ]: " + args[0];
break;
case "e":
if (!ENABLE_STATE_ERROR || !this.__eenabled){return;}
args[0] = "[ ERROR ]: " + args[0];
break;
case "c":
if (!ENABLE_STATE_CRITICAL || !this.__cenabled){return;}
args[0] = "[ CRITICAL ]: " + args[0];
break;
case "i":
default:
if (!ENABLE_STATE_INFO || !this.__ienabled){return;}
args[0] = "[ INFO ]: " + args[0];
}

this.__d.apply(this.__d, args);
}

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

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

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

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

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

extend(ens){
var nlog = new LOG(this.__ns + ":" + ens);
nlog.enabled = this.__enabled;
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;
}
}



module.exports = {
Log:Log,
enabled: function(){
if (arguments.length > 0)
ENABLE_STATE_LOG = (arguments[0] === true);
return ENABLE_STATE_LOG;
},

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

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

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

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

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


+ 18
- 0
test/logging.spec.js Wyświetl plik

@@ -0,0 +1,18 @@
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;
//});
//});

it('Test to see if I subbed my toe', function(){
let log = new logging.Log();
});
});

+ 1
- 0
test/test-setup.spec.js Wyświetl plik

@@ -4,6 +4,7 @@ const sinon = require('sinon');
const chai = require('chai');

beforeEach(function(){
console.log("PING");
this.sandbox = sinon.sandbox.create();
})


+ 4
- 0
wwwterm.js Wyświetl plik

@@ -0,0 +1,4 @@
var logging = require('./app/logging.js');

var log = new logging.Log("bob");
log.info("So there!");

Ładowanie…
Anuluj
Zapisz