Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix and adding ability to change logging level #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/changeLevel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

/**
* Module dependencies.
*/

var Log = require('../lib/log')
, log = new Log();

var loop = 0;

function LogStuffOnLevels(){
loop++;
log.debug('a debug message %s',loop);
log.info('a info message %s',loop);
log.notice('a notice message %s',loop);
log.warning('a warning message %s',loop);
log.error('a error message %s',loop);
log.critical('a critical message %s',loop);
log.alert('a alert message %s',loop);
log.emergency('a emergency message %s',loop);
}

for(var i=0;i<8;i++){
log.setLevel(i);
LogStuffOnLevels();
}
17 changes: 15 additions & 2 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var EventEmitter = require('events').EventEmitter;
*/

var Log = exports = module.exports = function Log(level, stream){
if ('string' == typeof level) level = exports[level.toUpperCase()];
this.level = level || exports.DEBUG;
if ('string' === typeof level) level = exports[level.toUpperCase()];
if ('undefined' === typeof level) level = exports.DEBUG;
this.level = level;
this.stream = stream || process.stdout;
if (this.stream.readable) this.read();
};
Expand Down Expand Up @@ -157,6 +158,18 @@ Log.prototype = {
);
}
},

/**
* Change current logging level.
*
* @param {Number} level
* @api public
*/

setLevel: function(level) {
if ('string' == typeof level) level = exports[level.toUpperCase()];
this.level = level;
},

/**
* Log emergency `msg`.
Expand Down