adding ability to check if a logger exists

This allows for not accidentally adding a non-configured logger
This commit is contained in:
jci-fox 2014-03-04 09:45:56 -06:00
parent 83ad0babf3
commit fd05d90c2f

View File

@ -62,6 +62,11 @@ var events = require('events')
replaceConsole: false replaceConsole: false
}; };
function hasLogger(logger) {
return loggers.hasOwnProperty(logger);
}
/** /**
* 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.
@ -76,7 +81,7 @@ function getLogger (categoryName) {
} }
var appenderList; var appenderList;
if (!loggers[categoryName]) { if (!hasLogger(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
loggers[categoryName] = new Logger(categoryName); loggers[categoryName] = new Logger(categoryName);
if (appenders[categoryName]) { if (appenders[categoryName]) {
@ -115,7 +120,7 @@ function addAppender () {
if (category === ALL_CATEGORIES) { if (category === ALL_CATEGORIES) {
addAppenderToAllLoggers(appender); addAppenderToAllLoggers(appender);
} else if (loggers[category]) { } else if (hasLogger(category)) {
loggers[category].addListener("log", appender); loggers[category].addListener("log", appender);
} }
}); });
@ -123,7 +128,7 @@ function addAppender () {
function addAppenderToAllLoggers(appender) { function addAppenderToAllLoggers(appender) {
for (var logger in loggers) { for (var logger in loggers) {
if (loggers.hasOwnProperty(logger)) { if (hasLogger(logger)) {
loggers[logger].addListener("log", appender); loggers[logger].addListener("log", appender);
} }
} }
@ -139,7 +144,7 @@ function addAppenderToCategory(appender, category) {
function clearAppenders () { function clearAppenders () {
appenders = {}; appenders = {};
for (var logger in loggers) { for (var logger in loggers) {
if (loggers.hasOwnProperty(logger)) { if (hasLogger(logger)) {
loggers[logger].removeAllListeners("log"); loggers[logger].removeAllListeners("log");
} }
} }
@ -304,6 +309,7 @@ function loadAppender(appender) {
module.exports = { module.exports = {
getLogger: getLogger, getLogger: getLogger,
getDefaultLogger: getDefaultLogger, getDefaultLogger: getDefaultLogger,
hasLogger: hasLogger,
addAppender: addAppender, addAppender: addAppender,
loadAppender: loadAppender, loadAppender: loadAppender,