Merge pull request #98 from danbell/master
Check environment variable LOG4JS_CONFIG for configuration file location.
This commit is contained in:
commit
892181f88f
12
README.md
12
README.md
@ -73,17 +73,19 @@ The first 5 lines of the code above could also be written as:
|
|||||||
|
|
||||||
var log4js = require('log4js');
|
var log4js = require('log4js');
|
||||||
log4js.configure({
|
log4js.configure({
|
||||||
appenders: [
|
appenders: [
|
||||||
{ type: 'console' },
|
{ type: 'console' },
|
||||||
{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
|
{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
## configuration
|
## configuration
|
||||||
|
|
||||||
You can configure the appenders and log levels manually (as above), or provide a
|
You can configure the appenders and log levels manually (as above), or provide a
|
||||||
configuration file (`log4js.configure('path/to/file.json')`), or a configuration object.
|
configuration file (`log4js.configure('path/to/file.json')`), or a configuration object. The
|
||||||
|
configuration file location may also be specified via the environment variable
|
||||||
|
LOG4JS_CONFIG (`export LOG4JS_CONFIG=path/to/file.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`.
|
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.
|
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.
|
||||||
|
|
||||||
|
@ -240,8 +240,9 @@ function initReloadConfiguration(filename, options) {
|
|||||||
|
|
||||||
function configure(configurationFileOrObject, options) {
|
function configure(configurationFileOrObject, options) {
|
||||||
var config = configurationFileOrObject;
|
var config = configurationFileOrObject;
|
||||||
|
config = config || process.env.LOG4JS_CONFIG;
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
if (config === undefined || config === null || typeof(config) === 'string') {
|
if (config === undefined || config === null || typeof(config) === 'string') {
|
||||||
if (options.reloadSecs) {
|
if (options.reloadSecs) {
|
||||||
initReloadConfiguration(config, options);
|
initReloadConfiguration(config, options);
|
||||||
|
@ -84,6 +84,48 @@ vows.describe('log4js configure').addBatch({
|
|||||||
'should add appender configure function to appenderMakers': function(log4js) {
|
'should add appender configure function to appenderMakers': function(log4js) {
|
||||||
assert.isFunction(log4js.appenderMakers['some/other/external']);
|
assert.isFunction(log4js.appenderMakers['some/other/external']);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
'when configuration file loaded via LOG4JS_CONFIG environment variable': {
|
||||||
|
topic: function() {
|
||||||
|
process.env.LOG4JS_CONFIG = 'some/path/to/mylog4js.json';
|
||||||
|
var fileRead = 0,
|
||||||
|
modulePath = 'some/path/to/mylog4js.json',
|
||||||
|
pathsChecked = [],
|
||||||
|
mtime = new Date(),
|
||||||
|
fakeFS = {
|
||||||
|
config: { appenders: [ { type: 'console', layout: { type: 'messagePassThrough' } } ],
|
||||||
|
levels: { 'a-test' : 'INFO' } },
|
||||||
|
readdirSync: function(dir) {
|
||||||
|
return require('fs').readdirSync(dir);
|
||||||
|
},
|
||||||
|
readFileSync: function (file, encoding) {
|
||||||
|
fileRead += 1;
|
||||||
|
assert.isString(file);
|
||||||
|
assert.equal(file, modulePath);
|
||||||
|
assert.equal(encoding, 'utf8');
|
||||||
|
return JSON.stringify(fakeFS.config);
|
||||||
|
},
|
||||||
|
statSync: function (path) {
|
||||||
|
pathsChecked.push(path);
|
||||||
|
if (path === modulePath) {
|
||||||
|
return { mtime: mtime };
|
||||||
|
} else {
|
||||||
|
throw new Error("no such file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
log4js = sandbox.require('../lib/log4js',
|
||||||
|
{
|
||||||
|
requires: {
|
||||||
|
'fs': fakeFS,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
delete process.env.LOG4JS_CONFIG;
|
||||||
|
return fileRead;
|
||||||
|
},
|
||||||
|
'should load the specified local configuration file' : function(fileRead) {
|
||||||
|
assert.equal(fileRead, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).exportTo(module);
|
}).exportTo(module);
|
||||||
|
Loading…
Reference in New Issue
Block a user