diff --git a/lib/log4js-node.js b/lib/log4js-node.js index 7fbe76c..6857baf 100644 --- a/lib/log4js-node.js +++ b/lib/log4js-node.js @@ -72,7 +72,9 @@ var log4js = { */ loggers: {}, - appenders: {}, + appenders: {} +}; + /** * Get a logger instance. Instance is cached on categoryName level. @@ -80,78 +82,77 @@ var log4js = { * @return {Logger} instance of logger for the category * @static */ - getLogger: function(categoryName) { +exports.getLogger = log4js.getLogger = function(categoryName) { // Use default logger if categoryName is not specified or invalid if (!(typeof categoryName == "string")) { categoryName = DEFAULT_CATEGORY; } - var self = this, appenderList; - if (!this.loggers[categoryName]) { + var appenderList; + if (!log4js.loggers[categoryName]) { // Create the logger for this name if it doesn't already exist - this.loggers[categoryName] = new Logger(categoryName); - if (this.appenders[categoryName]) { - appenderList = this.appenders[categoryName]; + log4js.loggers[categoryName] = new Logger(categoryName); + if (log4js.appenders[categoryName]) { + appenderList = log4js.appenders[categoryName]; appenderList.forEach(function(appender) { - self.loggers[categoryName].addListener("log", appender); + log4js.loggers[categoryName].addListener("log", appender); }); } - if (this.appenders[ALL_CATEGORIES]) { - appenderList = this.appenders[ALL_CATEGORIES]; + if (log4js.appenders[ALL_CATEGORIES]) { + appenderList = log4js.appenders[ALL_CATEGORIES]; 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. * @return {Logger} instance of default logger * @static */ - getDefaultLogger: function() { - return this.getLogger(DEFAULT_CATEGORY); - }, +exports.getDefaultLogger = log4js.getDefaultLogger = function() { + return log4js.getLogger(DEFAULT_CATEGORY); +}; - addAppender: function (appender, categoryName) { +exports.addAppender = log4js.addAppender = function (appender, categoryName) { var category = categoryName || ALL_CATEGORIES; - if (!this.appenders[category]) { - this.appenders[category] = []; + if (!log4js.appenders[category]) { + log4js.appenders[category] = []; } - this.appenders[category].push(appender); + log4js.appenders[category].push(appender); if (category === ALL_CATEGORIES) { - for (var logger in this.loggers) { - if (this.loggers.hasOwnProperty(logger)) { - this.loggers[logger].addListener("log", appender); + for (var logger in log4js.loggers) { + if (log4js.loggers.hasOwnProperty(logger)) { + log4js.loggers[logger].addListener("log", appender); } } - } else if (this.loggers[category]) { - this.loggers[category].addListener("log", appender); + } else if (log4js.loggers[category]) { + log4js.loggers[category].addListener("log", appender); } - - }, +}; - clearAppenders: function() { - this.appenders = []; - for (var logger in this.loggers) { - if (this.loggers.hasOwnProperty(logger)) { - this.loggers[logger].listeners.length = 0; +exports.clearAppenders = log4js.clearAppenders = function() { + log4js.appenders = []; + for (var logger in log4js.loggers) { + if (log4js.loggers.hasOwnProperty(logger)) { + log4js.loggers[logger].listeners.length = 0; } } - }, +}; - configure: function(configurationFile) { +exports.configure = log4js.configure = function(configurationFile) { var config = JSON.parse(fs.readFileSync(configurationFile)); configureAppenders(config.appenders); configureLevels(config.levels); - }, +}; - levels: { +exports.levels = log4js.levels = { ALL: new Level(Number.MIN_VALUE, "ALL"), TRACE: new Level(5000, "TRACE"), DEBUG: new Level(10000, "DEBUG"), @@ -159,9 +160,7 @@ var log4js = { WARN: new Level(30000, "WARN"), ERROR: new Level(40000, "ERROR"), FATAL: new Level(50000, "FATAL"), - OFF: new Level(Number.MAX_VALUE, "OFF") - } - + OFF: new Level(Number.MAX_VALUE, "OFF") }; var appenderMakers = { @@ -194,9 +193,6 @@ var layoutMakers = { } }; -function configureAppender(appenderConfig) { -} - function configureAppenders(appenderList) { log4js.clearAppenders(); if (appenderList) { @@ -360,7 +356,7 @@ Logger.prototype.isLevelEnabled = function(otherLevel) { } ); -consoleAppender = function (layout) { +var consoleAppender = function (layout) { layout = layout || basicLayout; return function(loggingEvent) { sys.puts(layout(loggingEvent)); @@ -373,7 +369,7 @@ consoleAppender = function (layout) { * @param file file log messages will be written to * @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; file = file || "log4js.log"; //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); return function(logEvent) { if (logEvent.level.isGreaterThanOrEqualTo(level)) { @@ -407,7 +403,7 @@ logLevelFilter = function(levelString, appender) { * * @author Stephan Strittmatter */ -basicLayout = function(loggingEvent) { +var basicLayout = function(loggingEvent) { var timestampLevelAndCategory = '[' + loggingEvent.startTime.toFormattedString() + '] '; timestampLevelAndCategory += '[' + loggingEvent.level.toString() + '] '; timestampLevelAndCategory += loggingEvent.categoryName + ' - '; @@ -426,7 +422,7 @@ basicLayout = function(loggingEvent) { return output; }; -messagePassThroughLayout = function(loggingEvent) { +var messagePassThroughLayout = function(loggingEvent) { return loggingEvent.message; }; @@ -435,7 +431,7 @@ messagePassThroughLayout = function(loggingEvent) { * Takes a pattern string and returns a layout function. * @author Stephan Strittmatter */ -patternLayout = function(pattern) { +var patternLayout = function(pattern) { pattern = pattern || patternLayout.DEFAULT_CONVERSION_PATTERN; var regex = /%(-?[0-9]+)?(\.?[0-9]+)?([cdmnpr%])(\{([^\}]+)\})?|([^%]+)/; @@ -604,11 +600,10 @@ Date.prototype.toFormattedString = function(format) { } }; -log4js.consoleAppender = consoleAppender; -log4js.fileAppender = fileAppender; -log4js.logLevelFilter = logLevelFilter; -log4js.basicLayout = basicLayout; -log4js.patternLayout = patternLayout; -log4js.messagePassThroughLayout = messagePassThroughLayout; +exports.consoleAppender = log4js.consoleAppender = consoleAppender; +exports.fileAppender = log4js.fileAppender = fileAppender; +exports.logLevelFilter = log4js.logLevelFilter = logLevelFilter; +exports.basicLayout = log4js.basicLayout = basicLayout; +exports.patternLayout = log4js.patternLayout = patternLayout; +exports.messagePassThroughLayout = log4js.messagePassThroughLayout = messagePassThroughLayout; -process.mixin(exports,log4js); diff --git a/spec/spec.logging.js b/spec/spec.logging.js index 87ef185..312b8e0 100644 --- a/spec/spec.logging.js +++ b/spec/spec.logging.js @@ -14,10 +14,6 @@ describe 'log4js' logger.addListener("log", function (logEvent) { event = logEvent; }); end - it 'should have a version' - log4js.version.should.not.be undefined - end - describe 'getLogger' it 'should take a category and return a Logger'