Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Logger into separate file, added support for loading appenders …
…outside log4js, removed 'name' from appender requirements
- Loading branch information
Gareth Jones
committed
Jun 1, 2012
1 parent
0ed1a13
commit a046523
Showing
14 changed files
with
236 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
var levels = require('./levels'), | ||
util = require('util'), | ||
events = require('events'), | ||
DEFAULT_CATEGORY = '[default]'; | ||
|
||
/** | ||
* Models a logging event. | ||
* @constructor | ||
* @param {String} categoryName name of category | ||
* @param {Log4js.Level} level level of message | ||
* @param {Array} data objects to log | ||
* @param {Log4js.Logger} logger the associated logger | ||
* @author Seth Chisamore | ||
*/ | ||
function LoggingEvent (categoryName, level, data, logger) { | ||
this.startTime = new Date(); | ||
this.categoryName = categoryName; | ||
this.data = data; | ||
this.level = level; | ||
this.logger = logger; | ||
} | ||
|
||
/** | ||
* Logger to log messages. | ||
* use {@see Log4js#getLogger(String)} to get an instance. | ||
* @constructor | ||
* @param name name of category to log to | ||
* @author Stephan Strittmatter | ||
*/ | ||
function Logger (name, level) { | ||
this.category = name || DEFAULT_CATEGORY; | ||
|
||
if (! this.level) { | ||
this.__proto__.level = levels.TRACE; | ||
} | ||
} | ||
util.inherits(Logger, events.EventEmitter); | ||
Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY; | ||
|
||
Logger.prototype.setLevel = function(level) { | ||
this.level = levels.toLevel(level, this.level || levels.TRACE); | ||
}; | ||
|
||
Logger.prototype.removeLevel = function() { | ||
delete this.level; | ||
}; | ||
|
||
Logger.prototype.log = function() { | ||
var args = Array.prototype.slice.call(arguments) | ||
, logLevel = args.shift() | ||
, loggingEvent = new LoggingEvent(this.category, logLevel, args, this); | ||
this.emit("log", loggingEvent); | ||
}; | ||
|
||
Logger.prototype.isLevelEnabled = function(otherLevel) { | ||
return this.level.isLessThanOrEqualTo(otherLevel); | ||
}; | ||
|
||
['Trace','Debug','Info','Warn','Error','Fatal'].forEach( | ||
function(levelString) { | ||
var level = levels.toLevel(levelString); | ||
Logger.prototype['is'+levelString+'Enabled'] = function() { | ||
return this.isLevelEnabled(level); | ||
}; | ||
|
||
Logger.prototype[levelString.toLowerCase()] = function () { | ||
if (this.isLevelEnabled(level)) { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(level); | ||
Logger.prototype.log.apply(this, args); | ||
} | ||
}; | ||
} | ||
); | ||
|
||
|
||
exports.LoggingEvent = LoggingEvent; | ||
exports.Logger = Logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var assert = require('assert'), | ||
vows = require('vows'), | ||
sandbox = require('sandboxed-module'); | ||
|
||
function makeTestAppender() { | ||
return { | ||
configure: function(config, options) { | ||
this.configureCalled = true; | ||
this.config = config; | ||
this.options = options; | ||
return this.appender(); | ||
}, | ||
appender: function() { | ||
var self = this; | ||
return function(logEvt) { self.logEvt = logEvt; } | ||
} | ||
}; | ||
} | ||
|
||
vows.describe('log4js configure').addBatch({ | ||
'appenders': { | ||
'when specified by type': { | ||
topic: function() { | ||
var testAppender = makeTestAppender(), | ||
log4js = sandbox.require( | ||
'../lib/log4js', | ||
{ | ||
requires: { | ||
'./appenders/cheese': testAppender | ||
} | ||
} | ||
); | ||
log4js.configure( | ||
{ | ||
appenders: [ | ||
{ type: "cheese", flavour: "gouda" } | ||
] | ||
}, | ||
{ pants: "yes" } | ||
); | ||
return testAppender; | ||
}, | ||
'should load appender': function(testAppender) { | ||
assert.ok(testAppender.configureCalled); | ||
}, | ||
'should pass config to appender': function(testAppender) { | ||
assert.equal(testAppender.config.flavour, 'gouda'); | ||
}, | ||
'should pass log4js options to appender': function(testAppender) { | ||
assert.equal(testAppender.options.pants, 'yes'); | ||
} | ||
}, | ||
'when core appender loaded via loadAppender': { | ||
topic: function() { | ||
var testAppender = makeTestAppender(), | ||
log4js = sandbox.require( | ||
'../lib/log4js', | ||
{ requires: { './appenders/cheese': testAppender } } | ||
); | ||
|
||
log4js.loadAppender('cheese'); | ||
return log4js; | ||
}, | ||
'should load appender from ../lib/appenders': function(log4js) { | ||
assert.ok(log4js.appenders.cheese); | ||
}, | ||
'should add appender configure function to appenderMakers' : function(log4js) { | ||
assert.isFunction(log4js.appenderMakers.cheese); | ||
} | ||
}, | ||
'when appender in node_modules loaded via loadAppender': { | ||
topic: function() { | ||
var testAppender = makeTestAppender(), | ||
log4js = sandbox.require( | ||
'../lib/log4js', | ||
{ requires: { 'some/other/external': testAppender } } | ||
); | ||
log4js.loadAppender('some/other/external'); | ||
return log4js; | ||
}, | ||
'should load appender via require': function(log4js) { | ||
assert.ok(log4js.appenders['some/other/external']); | ||
}, | ||
'should add appender configure function to appenderMakers': function(log4js) { | ||
assert.isFunction(log4js.appenderMakers['some/other/external']); | ||
} | ||
} | ||
} | ||
}).exportTo(module); |
Oops, something went wrong.