From fd05d90c2fdc86fd0f7330042b9462b9abe3d6d0 Mon Sep 17 00:00:00 2001 From: jci-fox Date: Tue, 4 Mar 2014 09:45:56 -0600 Subject: [PATCH] adding ability to check if a logger exists This allows for not accidentally adding a non-configured logger --- lib/log4js.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index fc3baca..db2c230 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -62,6 +62,11 @@ var events = require('events') replaceConsole: false }; +function hasLogger(logger) { + return loggers.hasOwnProperty(logger); +} + + /** * Get a logger instance. Instance is cached on categoryName level. * @param {String} categoryName name of category to log to. @@ -76,7 +81,7 @@ function getLogger (categoryName) { } var appenderList; - if (!loggers[categoryName]) { + if (!hasLogger(categoryName)) { // Create the logger for this name if it doesn't already exist loggers[categoryName] = new Logger(categoryName); if (appenders[categoryName]) { @@ -115,7 +120,7 @@ function addAppender () { if (category === ALL_CATEGORIES) { addAppenderToAllLoggers(appender); - } else if (loggers[category]) { + } else if (hasLogger(category)) { loggers[category].addListener("log", appender); } }); @@ -123,7 +128,7 @@ function addAppender () { function addAppenderToAllLoggers(appender) { for (var logger in loggers) { - if (loggers.hasOwnProperty(logger)) { + if (hasLogger(logger)) { loggers[logger].addListener("log", appender); } } @@ -139,7 +144,7 @@ function addAppenderToCategory(appender, category) { function clearAppenders () { appenders = {}; for (var logger in loggers) { - if (loggers.hasOwnProperty(logger)) { + if (hasLogger(logger)) { loggers[logger].removeAllListeners("log"); } } @@ -304,6 +309,7 @@ function loadAppender(appender) { module.exports = { getLogger: getLogger, getDefaultLogger: getDefaultLogger, + hasLogger: hasLogger, addAppender: addAppender, loadAppender: loadAppender,