moved Logger class out of main module

This commit is contained in:
Gareth Jones 2012-05-31 08:16:22 +10:00
parent 33a92b5dd6
commit 0ed1a137d6

View File

@ -50,7 +50,8 @@ var events = require('events')
, util = require('util') , util = require('util')
, layouts = require('./layouts') , layouts = require('./layouts')
, levels = require('./levels') , levels = require('./levels')
, DEFAULT_CATEGORY = '[default]' , LoggingEvent = require('./logger').LoggingEvent
, Logger = require('./logger').Logger
, ALL_CATEGORIES = '[all]' , ALL_CATEGORIES = '[all]'
, appenders = {} , appenders = {}
, loggers = {} , loggers = {}
@ -62,76 +63,6 @@ var events = require('events')
replaceConsole: false replaceConsole: false
}; };
/**
* 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.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);
}
};
}
);
/** /**
* Get a logger instance. Instance is cached on categoryName level. * Get a logger instance. Instance is cached on categoryName level.
* @param {String} categoryName name of category to log to. * @param {String} categoryName name of category to log to.
@ -142,7 +73,7 @@ function getLogger (categoryName) {
// Use default logger if categoryName is not specified or invalid // Use default logger if categoryName is not specified or invalid
if (!(typeof categoryName == "string")) { if (!(typeof categoryName == "string")) {
categoryName = DEFAULT_CATEGORY; categoryName = Logger.DEFAULT_CATEGORY;
} }
var appenderList; var appenderList;
@ -244,7 +175,7 @@ function setGlobalLogLevel(level) {
* @static * @static
*/ */
function getDefaultLogger () { function getDefaultLogger () {
return getLogger(DEFAULT_CATEGORY); return getLogger(Logger.DEFAULT_CATEGORY);
} }
var configState = {}; var configState = {};