Merge pull request #20 from danbell/master
Added ability to reload configuration file periodically.
This commit is contained in:
commit
71f9eef6fe
18
README.md
18
README.md
@ -52,7 +52,23 @@ Output:
|
||||
|
||||
You can either configure the appenders and log levels manually (as above), or provide a
|
||||
configuration file (`log4js.configure('path/to/file.json')`) explicitly, or just let log4js look for a file called `log4js.json` (it looks in the current directory first, then the require paths, and finally looks for the default config included in the same directory as the `log4js.js` file).
|
||||
An example file can be found in `test/log4js.json`. An example config file with log rolling is in `test/with-log-rolling.json`
|
||||
An example file can be found in `test/log4js.json`. An example config file with log rolling is in `test/with-log-rolling.json`.
|
||||
By default, the configuration file is checked for changes every 60 seconds, and if changed, reloaded. This allows changes to logging levels
|
||||
to occur without restarting the application.
|
||||
|
||||
To turn off configuration file change checking, configure with:
|
||||
|
||||
var log4js = require('log4js');
|
||||
log4js.configure(undefined, {}); // load 'log4js.json' from NODE_PATH
|
||||
|
||||
Or:
|
||||
|
||||
log4js.configure('my_log4js_configuration.json', {});
|
||||
|
||||
To specify a different period:
|
||||
|
||||
log4js.configure(undefined, { reloadSecs: 300 }); // load 'log4js.json' from NODE_PATH
|
||||
|
||||
You can also pass an object to the configure function, which has the same properties as the json versions.
|
||||
|
||||
## connect/express logger
|
||||
|
@ -157,6 +157,10 @@ function configureLevels(levels) {
|
||||
getLogger(category).setLevel(levels[category]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (l in loggers) {
|
||||
loggers[l].setLevel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,21 +246,6 @@ function getDefaultLogger () {
|
||||
return getLogger(DEFAULT_CATEGORY);
|
||||
}
|
||||
|
||||
function configure (configurationFileOrObject) {
|
||||
var config = configurationFileOrObject;
|
||||
if (typeof(config) === "string") {
|
||||
config = JSON.parse(fs.readFileSync(config, "utf8"));
|
||||
}
|
||||
if (config) {
|
||||
try {
|
||||
configureAppenders(config.appenders);
|
||||
configureLevels(config.levels);
|
||||
} catch (e) {
|
||||
throw new Error("Problem reading log4js config " + sys.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findConfiguration() {
|
||||
//add current directory onto the list of configPaths
|
||||
var paths = ['.'].concat(require.paths);
|
||||
@ -276,6 +265,79 @@ function findConfiguration() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function loadConfigurationFile(filename) {
|
||||
filename = filename || findConfiguration();
|
||||
if (filename) {
|
||||
return JSON.parse(fs.readFileSync(filename, "utf8"));
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function configureOnceOff(config) {
|
||||
if (config) {
|
||||
try {
|
||||
configureAppenders(config.appenders);
|
||||
configureLevels(config.levels);
|
||||
} catch (e) {
|
||||
throw new Error("Problem reading log4js config " + sys.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var configState = {};
|
||||
|
||||
function reloadConfiguration() {
|
||||
var filename = configState.filename || findConfiguration(),
|
||||
mtime;
|
||||
if (!filename) {
|
||||
// can't find anything to reload
|
||||
return;
|
||||
}
|
||||
try {
|
||||
mtime = fs.statSync(filename).mtime;
|
||||
} catch (e) {
|
||||
getLogger('log4js').warn('Failed to load configuration file ' + filename);
|
||||
return;
|
||||
}
|
||||
if (configState.lastFilename && configState.lastFilename === filename) {
|
||||
if (mtime.getTime() > configState.lastMTime.getTime()) {
|
||||
configState.lastMTime = mtime;
|
||||
} else {
|
||||
filename = null;
|
||||
}
|
||||
} else {
|
||||
configState.lastFilename = filename;
|
||||
configState.lastMTime = mtime;
|
||||
}
|
||||
configureOnceOff(loadConfigurationFile(filename));
|
||||
}
|
||||
|
||||
function initReloadConfiguration(filename, options) {
|
||||
if (configState.timerId) {
|
||||
clearInterval(configState.timerId);
|
||||
delete configState.timerId;
|
||||
}
|
||||
configState.filename = filename;
|
||||
configState.timerId = setInterval(reloadConfiguration, options.reloadSecs*1000);
|
||||
}
|
||||
|
||||
function configure (configurationFileOrObject, options) {
|
||||
var config = configurationFileOrObject;
|
||||
if (config === undefined || config === null || typeof(config) === 'string') {
|
||||
options = options || { reloadSecs: 60 };
|
||||
if (options.reloadSecs) {
|
||||
initReloadConfiguration(config, options);
|
||||
}
|
||||
configureOnceOff(loadConfigurationFile(config));
|
||||
} else {
|
||||
options = options || {};
|
||||
if (options.reloadSecs) {
|
||||
getLogger('log4js').warn('Ignoring configuration reload parameter for "object" configuration.');
|
||||
}
|
||||
configureOnceOff(config);
|
||||
}
|
||||
}
|
||||
|
||||
function replaceConsole(logger) {
|
||||
function replaceWith(fn) {
|
||||
return function() {
|
||||
|
@ -390,6 +390,77 @@ vows.describe('log4js').addBatch({
|
||||
|
||||
assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
|
||||
}
|
||||
},
|
||||
'configuration reload' : {
|
||||
topic: function() {
|
||||
var pathsChecked = [],
|
||||
logEvents = [],
|
||||
logger,
|
||||
modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
|
||||
fakeFS = {
|
||||
config: { appenders: [ { type: 'console', layout: { type: 'messagePassThrough' } } ],
|
||||
levels: { 'a-test' : 'INFO' } },
|
||||
readdirSync: function(dir) {
|
||||
return require('fs').readdirSync(dir);
|
||||
},
|
||||
readFileSync: function (file, encoding) {
|
||||
assert.equal(file, modulePath);
|
||||
assert.equal(encoding, 'utf8');
|
||||
return JSON.stringify(fakeFS.config);
|
||||
},
|
||||
statSync: function (path) {
|
||||
pathsChecked.push(path);
|
||||
if (path === modulePath) {
|
||||
return { mtime: new Date() };
|
||||
} else {
|
||||
throw new Error("no such file");
|
||||
}
|
||||
}
|
||||
},
|
||||
fakeConsole = {
|
||||
'name': 'console',
|
||||
'appender': function () {
|
||||
return function(evt) { logEvents.push(evt); };
|
||||
},
|
||||
'configure': function (config) {
|
||||
return fakeConsole.appender();
|
||||
}
|
||||
},
|
||||
setIntervalCallback,
|
||||
fakeSetInterval = function(cb, timeout) {
|
||||
setIntervalCallback = cb;
|
||||
},
|
||||
log4js = sandbox.require(
|
||||
'../lib/log4js',
|
||||
{
|
||||
requires: {
|
||||
'fs': fakeFS,
|
||||
'./appenders/console.js': fakeConsole
|
||||
},
|
||||
globals: {
|
||||
'console': fakeConsole,
|
||||
'setInterval' : fakeSetInterval,
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
logger = log4js.getLogger('a-test');
|
||||
logger.info("info1");
|
||||
logger.debug("debug2 - should be ignored");
|
||||
delete fakeFS.config.levels;
|
||||
setIntervalCallback();
|
||||
logger.info("info3");
|
||||
logger.debug("debug4");
|
||||
|
||||
return [ pathsChecked, logEvents, modulePath ];
|
||||
},
|
||||
'should configure log4js from first log4js.json found': function(args) {
|
||||
var logEvents = args[1];
|
||||
assert.length(logEvents, 3);
|
||||
assert.equal(logEvents[0].data[0], 'info1');
|
||||
assert.equal(logEvents[1].data[0], 'info3');
|
||||
assert.equal(logEvents[2].data[0], 'debug4');
|
||||
}
|
||||
}
|
||||
|
||||
}).export(module);
|
||||
|
Loading…
Reference in New Issue
Block a user