Skip to content

Commit

Permalink
issues_34: logger fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
glygapon committed Sep 2, 2019
1 parent 2389648 commit 19d9891
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions libs/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,54 @@
var util = require("util");

var LEVELS = {
DEBUG: 1<<0,
ERROR: 1<<1,
INFO: 1<<2,
WARN: 1<<3,
check: function(levels, level) {
DEBUG: 1 << 0,
ERROR: 1 << 1,
INFO: 1 << 2,
WARN: 1 << 3,
check: function (levels, level) {
return (levels & level) === level;
}
};

var Logger = module.exports = function(levels) {
var LOG = Object.freeze({
debug: console.debug || util.debug || console.log,
error: console.error || util.error || console.log,
info: console.info || util.print || console.log,
warn: console.warn || util.print || console.log
});

var Logger = module.exports = function (levels) {
if (this instanceof Logger) {
this.levels = levels || ( LEVELS.DEBUG | LEVELS.ERROR | LEVELS.INFO | LEVELS.WARN );
this.levels = levels || (LEVELS.DEBUG | LEVELS.ERROR | LEVELS.INFO | LEVELS.WARN);
} else {
return new Logger(levels);
}
}
};

Logger.prototype.setLogLevel = function(levels) {
Logger.prototype.setLogLevel = function (levels) {
this.levels = levels;
}

Logger.prototype.debug = function(msg) {
Logger.prototype.debug = function (msg) {
if (LEVELS.check(this.levels, LEVELS.DEBUG)) {
util.debug(msg);
LOG.debug(msg);
}
}

Logger.prototype.error = function(msg) {
Logger.prototype.error = function (msg) {
if (LEVELS.check(this.levels, LEVELS.ERROR)) {
util.error(msg);
LOG.error(msg);
}
}

Logger.prototype.info = function(msg) {
Logger.prototype.info = function (msg) {
if (LEVELS.check(this.levels, LEVELS.INFO)) {
util.print("INFO: " + msg);
LOG.info("INFO: " + msg);
}
}

Logger.prototype.warn = function(msg) {
Logger.prototype.warn = function (msg) {
if (LEVELS.check(this.levels, LEVELS.WARN)) {
util.print("WARN: " + msg);
LOG.warn("WARN: " + msg);
}
}

0 comments on commit 19d9891

Please sign in to comment.