Skip to content

Commit

Permalink
'with' method to add meta data with a log message
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Jun 15, 2015
1 parent 30f36ef commit e2e744b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brolog",
"version": "0.0.4",
"version": "0.0.5",
"authors": [
"Eyal Arubas <[email protected]>"
],
Expand Down
14 changes: 12 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function Logger(name){
this.level = LEVELS.toNumber.DBG;
this.counter = 0;
this.start = null;

// whatever is here will be sent as meta data to the log printers,
// *only* for the next log message, and then will be reset back to 'null'.
this.meta = null;
}

/**
Expand All @@ -71,7 +75,7 @@ function getConsolePrinter(console){
cdbg = console && console.debug ? console.debug.bind(console): clog,
cwrn = console && console.warn ? console.warn.bind(console): clog,
cerr = console && console.error ? console.error.bind(console): clog;
return function(gCounter, gStart, logger, nLevel, sLevel, msgs){
return function(gCounter, gStart, logger, nLevel, sLevel, msgs, meta){
var _msgs = [
"[" + gCounter + " " + (Date.now() - gStart) + "]",
"[" + logger.name + "]",
Expand Down Expand Up @@ -107,8 +111,9 @@ function _print(logger, level, args){
logger.counter++;
args = ARRAY_SLICE.apply(args);
PRINTERS.forEach(function(printer){
printer(gCounter, gStart, logger, level, LEVELS.toString[level], args);
printer(gCounter, gStart, logger, level, LEVELS.toString[level], args, logger.meta);
});
logger.meta = null;
return args.join(" ");
}

Expand All @@ -132,6 +137,11 @@ Logger.prototype.off = function(){
this.level = LEVELS.toNumber.OFF;
};

Logger.prototype.with = function(meta){
this.meta = meta;
return this;
};

Logger.prototype.setDebug = function(){
this.level = LEVELS.toNumber.DBG;
};
Expand Down

0 comments on commit e2e744b

Please sign in to comment.