removed dependency on process.mixin

This commit is contained in:
csausdev 2010-03-21 13:18:44 +11:00
parent 87823f6e22
commit 0d100c0ab2
2 changed files with 50 additions and 59 deletions

View File

@ -72,7 +72,9 @@ var log4js = {
*/ */
loggers: {}, loggers: {},
appenders: {}, appenders: {}
};
/** /**
* Get a logger instance. Instance is cached on categoryName level. * Get a logger instance. Instance is cached on categoryName level.
@ -80,78 +82,77 @@ var log4js = {
* @return {Logger} instance of logger for the category * @return {Logger} instance of logger for the category
* @static * @static
*/ */
getLogger: function(categoryName) { exports.getLogger = log4js.getLogger = function(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 = DEFAULT_CATEGORY;
} }
var self = this, appenderList; var appenderList;
if (!this.loggers[categoryName]) { if (!log4js.loggers[categoryName]) {
// Create the logger for this name if it doesn't already exist // Create the logger for this name if it doesn't already exist
this.loggers[categoryName] = new Logger(categoryName); log4js.loggers[categoryName] = new Logger(categoryName);
if (this.appenders[categoryName]) { if (log4js.appenders[categoryName]) {
appenderList = this.appenders[categoryName]; appenderList = log4js.appenders[categoryName];
appenderList.forEach(function(appender) { appenderList.forEach(function(appender) {
self.loggers[categoryName].addListener("log", appender); log4js.loggers[categoryName].addListener("log", appender);
}); });
} }
if (this.appenders[ALL_CATEGORIES]) { if (log4js.appenders[ALL_CATEGORIES]) {
appenderList = this.appenders[ALL_CATEGORIES]; appenderList = log4js.appenders[ALL_CATEGORIES];
appenderList.forEach(function(appender) { appenderList.forEach(function(appender) {
self.loggers[categoryName].addListener("log", appender); log4js.loggers[categoryName].addListener("log", appender);
}); });
} }
} }
return this.loggers[categoryName]; return log4js.loggers[categoryName];
}, };
/** /**
* Get the default logger instance. * Get the default logger instance.
* @return {Logger} instance of default logger * @return {Logger} instance of default logger
* @static * @static
*/ */
getDefaultLogger: function() { exports.getDefaultLogger = log4js.getDefaultLogger = function() {
return this.getLogger(DEFAULT_CATEGORY); return log4js.getLogger(DEFAULT_CATEGORY);
}, };
addAppender: function (appender, categoryName) { exports.addAppender = log4js.addAppender = function (appender, categoryName) {
var category = categoryName || ALL_CATEGORIES; var category = categoryName || ALL_CATEGORIES;
if (!this.appenders[category]) { if (!log4js.appenders[category]) {
this.appenders[category] = []; log4js.appenders[category] = [];
} }
this.appenders[category].push(appender); log4js.appenders[category].push(appender);
if (category === ALL_CATEGORIES) { if (category === ALL_CATEGORIES) {
for (var logger in this.loggers) { for (var logger in log4js.loggers) {
if (this.loggers.hasOwnProperty(logger)) { if (log4js.loggers.hasOwnProperty(logger)) {
this.loggers[logger].addListener("log", appender); log4js.loggers[logger].addListener("log", appender);
} }
} }
} else if (this.loggers[category]) { } else if (log4js.loggers[category]) {
this.loggers[category].addListener("log", appender); log4js.loggers[category].addListener("log", appender);
} }
};
}, exports.clearAppenders = log4js.clearAppenders = function() {
log4js.appenders = [];
clearAppenders: function() { for (var logger in log4js.loggers) {
this.appenders = []; if (log4js.loggers.hasOwnProperty(logger)) {
for (var logger in this.loggers) { log4js.loggers[logger].listeners.length = 0;
if (this.loggers.hasOwnProperty(logger)) {
this.loggers[logger].listeners.length = 0;
} }
} }
}, };
configure: function(configurationFile) { exports.configure = log4js.configure = function(configurationFile) {
var config = JSON.parse(fs.readFileSync(configurationFile)); var config = JSON.parse(fs.readFileSync(configurationFile));
configureAppenders(config.appenders); configureAppenders(config.appenders);
configureLevels(config.levels); configureLevels(config.levels);
}, };
levels: { exports.levels = log4js.levels = {
ALL: new Level(Number.MIN_VALUE, "ALL"), ALL: new Level(Number.MIN_VALUE, "ALL"),
TRACE: new Level(5000, "TRACE"), TRACE: new Level(5000, "TRACE"),
DEBUG: new Level(10000, "DEBUG"), DEBUG: new Level(10000, "DEBUG"),
@ -160,8 +161,6 @@ var log4js = {
ERROR: new Level(40000, "ERROR"), ERROR: new Level(40000, "ERROR"),
FATAL: new Level(50000, "FATAL"), FATAL: new Level(50000, "FATAL"),
OFF: new Level(Number.MAX_VALUE, "OFF") OFF: new Level(Number.MAX_VALUE, "OFF")
}
}; };
var appenderMakers = { var appenderMakers = {
@ -194,9 +193,6 @@ var layoutMakers = {
} }
}; };
function configureAppender(appenderConfig) {
}
function configureAppenders(appenderList) { function configureAppenders(appenderList) {
log4js.clearAppenders(); log4js.clearAppenders();
if (appenderList) { if (appenderList) {
@ -360,7 +356,7 @@ Logger.prototype.isLevelEnabled = function(otherLevel) {
} }
); );
consoleAppender = function (layout) { var consoleAppender = function (layout) {
layout = layout || basicLayout; layout = layout || basicLayout;
return function(loggingEvent) { return function(loggingEvent) {
sys.puts(layout(loggingEvent)); sys.puts(layout(loggingEvent));
@ -373,7 +369,7 @@ consoleAppender = function (layout) {
* @param file file log messages will be written to * @param file file log messages will be written to
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout). * @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
*/ */
fileAppender = function(file, layout) { var fileAppender = function(file, layout) {
layout = layout || basicLayout; layout = layout || basicLayout;
file = file || "log4js.log"; file = file || "log4js.log";
//syncs are generally bad, but we need //syncs are generally bad, but we need
@ -389,7 +385,7 @@ fileAppender = function(file, layout) {
}; };
}; };
logLevelFilter = function(levelString, appender) { var logLevelFilter = function(levelString, appender) {
var level = Level.toLevel(levelString); var level = Level.toLevel(levelString);
return function(logEvent) { return function(logEvent) {
if (logEvent.level.isGreaterThanOrEqualTo(level)) { if (logEvent.level.isGreaterThanOrEqualTo(level)) {
@ -407,7 +403,7 @@ logLevelFilter = function(levelString, appender) {
* *
* @author Stephan Strittmatter * @author Stephan Strittmatter
*/ */
basicLayout = function(loggingEvent) { var basicLayout = function(loggingEvent) {
var timestampLevelAndCategory = '[' + loggingEvent.startTime.toFormattedString() + '] '; var timestampLevelAndCategory = '[' + loggingEvent.startTime.toFormattedString() + '] ';
timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] '; timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] ';
timestampLevelAndCategory += loggingEvent.categoryName + ' - '; timestampLevelAndCategory += loggingEvent.categoryName + ' - ';
@ -426,7 +422,7 @@ basicLayout = function(loggingEvent) {
return output; return output;
}; };
messagePassThroughLayout = function(loggingEvent) { var messagePassThroughLayout = function(loggingEvent) {
return loggingEvent.message; return loggingEvent.message;
}; };
@ -435,7 +431,7 @@ messagePassThroughLayout = function(loggingEvent) {
* Takes a pattern string and returns a layout function. * Takes a pattern string and returns a layout function.
* @author Stephan Strittmatter * @author Stephan Strittmatter
*/ */
patternLayout = function(pattern) { var patternLayout = function(pattern) {
pattern = pattern || patternLayout.DEFAULT_CONVERSION_PATTERN; pattern = pattern || patternLayout.DEFAULT_CONVERSION_PATTERN;
var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/; var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/;
@ -604,11 +600,10 @@ Date.prototype.toFormattedString = function(format) {
} }
}; };
log4js.consoleAppender = consoleAppender; exports.consoleAppender = log4js.consoleAppender = consoleAppender;
log4js.fileAppender = fileAppender; exports.fileAppender = log4js.fileAppender = fileAppender;
log4js.logLevelFilter = logLevelFilter; exports.logLevelFilter = log4js.logLevelFilter = logLevelFilter;
log4js.basicLayout = basicLayout; exports.basicLayout = log4js.basicLayout = basicLayout;
log4js.patternLayout = patternLayout; exports.patternLayout = log4js.patternLayout = patternLayout;
log4js.messagePassThroughLayout = messagePassThroughLayout; exports.messagePassThroughLayout = log4js.messagePassThroughLayout = messagePassThroughLayout;
process.mixin(exports,log4js);

View File

@ -14,10 +14,6 @@ describe 'log4js'
logger.addListener("log", function (logEvent) { event = logEvent; }); logger.addListener("log", function (logEvent) { event = logEvent; });
end end
it 'should have a version'
log4js.version.should.not.be undefined
end
describe 'getLogger' describe 'getLogger'
it 'should take a category and return a Logger' it 'should take a category and return a Logger'