changed config loading to be more predictable

This commit is contained in:
Gareth Jones
2012-05-29 15:50:35 +10:00
parent ccc4976206
commit 754ac2c5ac
2 changed files with 50 additions and 85 deletions

View File

@@ -55,7 +55,13 @@ var events = require('events')
, ALL_CATEGORIES = '[all]'
, appenders = {}
, loggers = {}
, appenderMakers = {};
, appenderMakers = {}
, defaultConfig = {
appenders: [
{ type: "console" }
],
replaceConsole: false
};
/**
* Get a logger instance. Instance is cached on categoryName level.
@@ -146,8 +152,6 @@ function configureAppenders(appenderList) {
throw new Error("log4js configuration problem for "+util.inspect(appenderConfig));
}
});
} else {
addAppender(consoleAppender());
}
}
@@ -243,22 +247,9 @@ function getDefaultLogger () {
return getLogger(DEFAULT_CATEGORY);
}
function findConfiguration(filename) {
var path;
try {
path = require.resolve(filename || 'log4js.json');
} catch (e) {
//file not found. default to the one in the log4js module.
path = filename || __dirname + '/log4js.json';
}
return path;
}
var configState = {};
function loadConfigurationFile(filename) {
filename = findConfiguration(filename);
if (filename && (!configState.lastFilename || filename !== configState.lastFilename ||
!configState.lastMTime || fs.statSync(filename).mtime !== configState.lastMTime)) {
configState.lastFilename = filename;
@@ -274,10 +265,10 @@ function configureOnceOff(config) {
configureAppenders(config.appenders);
configureLevels(config.levels);
if (config.doNotReplaceConsole) {
restoreConsole();
} else {
if (config.replaceConsole) {
replaceConsole();
} else {
restoreConsole();
}
} catch (e) {
throw new Error("Problem reading log4js config " + util.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
@@ -286,7 +277,7 @@ function configureOnceOff(config) {
}
function reloadConfiguration() {
var filename = findConfiguration(configState.filename),
var filename = configState.filename,
mtime;
if (!filename) {
// can't find anything to reload
@@ -316,7 +307,7 @@ function initReloadConfiguration(filename, options) {
configState.timerId = setInterval(reloadConfiguration, options.reloadSecs*1000);
}
function configure (configurationFileOrObject, options) {
function configure(configurationFileOrObject, options) {
var config = configurationFileOrObject;
options = options || {};
@@ -324,7 +315,7 @@ function configure (configurationFileOrObject, options) {
if (options.reloadSecs) {
initReloadConfiguration(config, options);
}
config = loadConfigurationFile(config);
config = loadConfigurationFile(config) || defaultConfig;
} else {
if (options.reloadSecs) {
getLogger('log4js').warn('Ignoring configuration reload parameter for "object" configuration.');
@@ -332,7 +323,7 @@ function configure (configurationFileOrObject, options) {
}
if (options.hasOwnProperty('cwd')) {
if(config.hasOwnProperty('appenders')) {
if (config.hasOwnProperty('appenders')) {
config.appenders.forEach(function(appender) {
if (!appender.hasOwnProperty('type')) return;
@@ -409,8 +400,8 @@ module.exports = {
loadAppender(appender);
});
//set ourselves up if we can find a default log4js.json
configure(findConfiguration());
//set ourselves up
configure();
//keep the old-style layouts
['basicLayout','messagePassThroughLayout','colouredLayout','coloredLayout'].forEach(function(item) {