Merge pull request #14 from AlexanderS/master

Global log level
This commit is contained in:
Gareth Jones 2011-06-04 20:41:04 -07:00
commit 8eaff77974
2 changed files with 98 additions and 1 deletions

View File

@ -265,7 +265,10 @@ function LoggingEvent (categoryName, level, message, exception, logger) {
*/
function Logger (name, level) {
this.category = name || DEFAULT_CATEGORY;
this.level = Level.toLevel(level, levels.TRACE);
if (! this.level) {
this.__proto__.level = levels.TRACE;
}
}
sys.inherits(Logger, events.EventEmitter);
@ -273,6 +276,10 @@ Logger.prototype.setLevel = function(level) {
this.level = Level.toLevel(level, levels.TRACE);
};
Logger.prototype.removeLevel = function() {
delete this.level;
};
Logger.prototype.log = function(logLevel, message, exception) {
var loggingEvent = new LoggingEvent(this.category, logLevel, message, exception, this);
this.emit("log", loggingEvent);
@ -297,6 +304,10 @@ Logger.prototype.isLevelEnabled = function(otherLevel) {
}
);
function setGlobalLogLevel(level) {
Logger.prototype.level = Level.toLevel(level, levels.TRACE);
}
/**
* Get the default logger instance.
* @return {Logger} instance of default logger
@ -661,6 +672,7 @@ module.exports = function (fileSystem, standardOutput, configPaths) {
configure: configure,
levels: levels,
setGlobalLogLevel: setGlobalLogLevel,
consoleAppender: consoleAppender,
fileAppender: fileAppender,

View File

@ -0,0 +1,85 @@
var vows = require('vows'),
assert = require('assert');
vows.describe('log4js global loglevel').addBatch({
'global loglevel' : {
topic: function() {
var log4js = require('../lib/log4js')();
return log4js;
},
'set global loglevel on creation': function(log4js) {
var log1 = log4js.getLogger('log1');
var level = 'OFF';
if (log1.level.toString() == level) {
level = 'TRACE';
}
assert.notEqual(log1.level.toString(), level);
log4js.setGlobalLogLevel(level);
assert.equal(log1.level.toString(), level);
var log2 = log4js.getLogger('log2');
assert.equal(log2.level.toString(), level);
},
'global change loglevel': function(log4js) {
var log1 = log4js.getLogger('log1');
var log2 = log4js.getLogger('log2');
var level = 'OFF';
if (log1.level.toString() == level) {
level = 'TRACE';
}
assert.notEqual(log1.level.toString(), level);
log4js.setGlobalLogLevel(level);
assert.equal(log1.level.toString(), level);
assert.equal(log2.level.toString(), level);
},
'override loglevel': function(log4js) {
var log1 = log4js.getLogger('log1');
var log2 = log4js.getLogger('log2');
var level = 'OFF';
if (log1.level.toString() == level) {
level = 'TRACE';
}
assert.notEqual(log1.level.toString(), level);
var oldLevel = log1.level.toString();
assert.equal(log2.level.toString(), oldLevel);
log2.setLevel(level);
assert.equal(log1.level.toString(), oldLevel);
assert.equal(log2.level.toString(), level);
assert.notEqual(oldLevel, level);
log2.removeLevel();
assert.equal(log1.level.toString(), oldLevel);
assert.equal(log2.level.toString(), oldLevel);
},
'preload loglevevl': function(log4js) {
var log1 = log4js.getLogger('log1');
var level = 'OFF';
if (log1.level.toString() == level) {
level = 'TRACE';
}
assert.notEqual(log1.level.toString(), level);
var oldLevel = log1.level.toString();
log4js.getLogger('log2').setLevel(level);
assert.equal(log1.level.toString(), oldLevel);
// get again same logger but as different variable
var log2 = log4js.getLogger('log2');
assert.equal(log2.level.toString(), level);
assert.notEqual(oldLevel, level);
log2.removeLevel();
assert.equal(log1.level.toString(), oldLevel);
assert.equal(log2.level.toString(), oldLevel);
}
}
}).export(module);