Skip to content

Commit

Permalink
Levels are now configured dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Nov 18, 2015
1 parent dae9982 commit 8704013
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 92 deletions.
1 change: 1 addition & 0 deletions lib/ConfigurationElement.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ConfigurationElement {
return this.__configurations[name];
});
this.__defineSetter__(name, (configuration) => {
// TODO check if type is the same and if not call destroy on current plugin
if (!_.isEqual(this.__configurations[name], configuration)) {
this.__configurations[name] = configuration;
that.__update(name, configuration);
Expand Down
2 changes: 2 additions & 0 deletions lib/Configurators.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Configurator extends Plugin {
scan(currentConfiguration) {
throw new Error("Not implemented!");
}

destroy() {}
}

class Configurators extends ConfigurationElement {
Expand Down
124 changes: 32 additions & 92 deletions lib/Level.class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var _ = require('lodash');

class Level {
constructor(severity) {
this._severity = severity;
Expand All @@ -9,110 +11,48 @@ class Level {
return this._severity;
}

toString() {
switch (this._severity) {
case 1:
return "TRACE";
case 2:
return "DEBUG";
case 3:
return "INFO";
case 4:
return "WARN";
case 5:
return "ERROR";
default:
return "UNKNOWN";
}
}

parse(obj) {
if (obj instanceof Level) {
return obj;
}
if (obj === parseInt(obj)) {
return levels[obj];
return Level.instances[obj];
}
return this[obj];
}

get OFF() {
return this.off;
}

get off() {
return levels[6];
}

get ERR() {
return this.error;
}

get ERROR() {
return this.error;
}

get err() {
return this.error;
}

get error() {
return levels[5];
}

get WARNING() {
return this.warn;
}

get WARN() {
return this.warn;
}

get warning() {
return this.warn;
}

get warn() {
return levels[4];
}

get INFO() {
return this.info;
}

get info() {
return levels[3];
}

get DEBUG() {
return this.debug;
}

get debug() {
return levels[2];
}

get TRACE() {
return this.trace;
}

get trace() {
return levels[1];
}

get ALL() {
return this.all;
}
static configure(levels) {
Level.instances = [];
var stringMapping = {};
for (let i in levels) {
Level.instances.push(new Level(i));
}

get all() {
return levels[0];
for (let level of Level.instances) {
for (let i in levels) {
let firstWord;
for (let word of _.words(levels[i])) {
level.__defineGetter__(word, () => {
return Level.instances[i];
});
level.__defineGetter__(word.toUpperCase(), () => {
return Level.instances[i];
});
if (!stringMapping[i]) {
stringMapping[i] = word;
}
}
}
}
for (let k in stringMapping) {
Level.instances[k].toString = () => {
return stringMapping[k].toUpperCase();
}
}
}

}

var levels = [];
for (let i of [0,1,2,3,4,5,6]) {
levels[i] = new Level(i);
}
Level.configure(['all','trace', 'debug', 'info','warn/warning','error/err', 'off']);

module.exports = new Level();
module.exports = Level.instances[0];
4 changes: 4 additions & 0 deletions lib/Plugin.interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Plugin {
get type() {
throw new Error('Not implemented!');
}

destroy() {
throw new Error('Not implemented!');
}
}

module.exports = Plugin;
6 changes: 6 additions & 0 deletions test/Level.class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe("Level.class", () => {

expect(Level.TRACE).to.be.equal(Level.trace);
expect(Level.TRACE == 1).to.be.true;

expect(Level.ALL).to.be.equal(Level.all);
expect(Level.ALL == 0).to.be.true;

expect(Level.OFF).to.be.equal(Level.off);
expect(Level.OFF == 6).to.be.true;
});

it("should get the right log level for a string", () => {
Expand Down

0 comments on commit 8704013

Please sign in to comment.