Moved abspath option checking into file appender, log4js options now passed to appenders
This commit is contained in:
parent
05d5265554
commit
0901794b35
@ -50,11 +50,16 @@ function fileAppender (file, layout, logSize, numBackups) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function configure(config) {
|
function configure(config, options) {
|
||||||
var layout;
|
var layout;
|
||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layouts.layout(config.layout.type, config.layout);
|
layout = layouts.layout(config.layout.type, config.layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options && options.cwd && !config.absolute) {
|
||||||
|
config.filename = path.join(options.cwd, config.filename);
|
||||||
|
}
|
||||||
|
|
||||||
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,14 +138,14 @@ function clearAppenders () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function configureAppenders(appenderList) {
|
function configureAppenders(appenderList, options) {
|
||||||
clearAppenders();
|
clearAppenders();
|
||||||
if (appenderList) {
|
if (appenderList) {
|
||||||
appenderList.forEach(function(appenderConfig) {
|
appenderList.forEach(function(appenderConfig) {
|
||||||
loadAppender(appenderConfig.type);
|
loadAppender(appenderConfig.type);
|
||||||
var appender;
|
var appender;
|
||||||
appenderConfig.makers = appenderMakers;
|
appenderConfig.makers = appenderMakers;
|
||||||
appender = appenderMakers[appenderConfig.type](appenderConfig);
|
appender = appenderMakers[appenderConfig.type](appenderConfig, options);
|
||||||
if (appender) {
|
if (appender) {
|
||||||
addAppender(appender, appenderConfig.category);
|
addAppender(appender, appenderConfig.category);
|
||||||
} else {
|
} else {
|
||||||
@ -259,10 +259,10 @@ function loadConfigurationFile(filename) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function configureOnceOff(config) {
|
function configureOnceOff(config, options) {
|
||||||
if (config) {
|
if (config) {
|
||||||
try {
|
try {
|
||||||
configureAppenders(config.appenders);
|
configureAppenders(config.appenders, options);
|
||||||
configureLevels(config.levels);
|
configureLevels(config.levels);
|
||||||
|
|
||||||
if (config.replaceConsole) {
|
if (config.replaceConsole) {
|
||||||
@ -321,25 +321,7 @@ function configure(configurationFileOrObject, options) {
|
|||||||
getLogger('log4js').warn('Ignoring configuration reload parameter for "object" configuration.');
|
getLogger('log4js').warn('Ignoring configuration reload parameter for "object" configuration.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
configureOnceOff(config, options);
|
||||||
if (options.hasOwnProperty('cwd')) {
|
|
||||||
if (config.hasOwnProperty('appenders')) {
|
|
||||||
config.appenders.forEach(function(appender) {
|
|
||||||
if (!appender.hasOwnProperty('type')) return;
|
|
||||||
|
|
||||||
if (appender.type === 'file') {
|
|
||||||
if(!appender.hasOwnProperty('filename')) return;
|
|
||||||
if(appender.hasOwnProperty('absolute')) {
|
|
||||||
if(appender.absolute) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
appender.filename = path.join(options.cwd, appender.filename);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
configureOnceOff(config);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var originalConsoleFunctions = {
|
var originalConsoleFunctions = {
|
||||||
|
@ -86,7 +86,7 @@ vows.describe('log4js').addBatch({
|
|||||||
'invalid configuration': {
|
'invalid configuration': {
|
||||||
'should throw an exception': function() {
|
'should throw an exception': function() {
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
log4js.configure({ "type": "invalid" });
|
require('log4js').configure({ "type": "invalid" });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -393,7 +393,7 @@ vows.describe('log4js').addBatch({
|
|||||||
'configuration': {
|
'configuration': {
|
||||||
topic: function(test) {
|
topic: function(test) {
|
||||||
test.log4js.replaceConsole();
|
test.log4js.replaceConsole();
|
||||||
test.log4js.configure({ doNotReplaceConsole: true });
|
test.log4js.configure({ replaceConsole: false });
|
||||||
try {
|
try {
|
||||||
test.fakeConsole.log("This should cause the error described in the setup");
|
test.fakeConsole.log("This should cause the error described in the setup");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -407,14 +407,20 @@ vows.describe('log4js').addBatch({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
'configuration persistence' : {
|
'configuration persistence' : {
|
||||||
'should maintain appenders between requires': function () {
|
topic: function() {
|
||||||
var logEvent, firstLog4js = require('../lib/log4js'), secondLog4js;
|
var logEvent,
|
||||||
|
firstLog4js = require('../lib/log4js'),
|
||||||
|
secondLog4js;
|
||||||
|
|
||||||
firstLog4js.clearAppenders();
|
firstLog4js.clearAppenders();
|
||||||
firstLog4js.addAppender(function(evt) { logEvent = evt; });
|
firstLog4js.addAppender(function(evt) { logEvent = evt; });
|
||||||
|
|
||||||
secondLog4js = require('../lib/log4js');
|
secondLog4js = require('../lib/log4js');
|
||||||
secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
|
secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
|
||||||
|
|
||||||
|
return logEvent;
|
||||||
|
},
|
||||||
|
'should maintain appenders between requires': function (logEvent) {
|
||||||
assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
|
assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -3,93 +3,66 @@ var vows = require('vows')
|
|||||||
, sandbox = require('sandboxed-module');
|
, sandbox = require('sandboxed-module');
|
||||||
|
|
||||||
vows.describe('log4js-abspath').addBatch({
|
vows.describe('log4js-abspath').addBatch({
|
||||||
'configuration is passed as object with options.cwd': {
|
'options': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var appenderConfig
|
var appenderOptions,
|
||||||
, log4js = sandbox.require(
|
log4js = sandbox.require(
|
||||||
'../lib/log4js'
|
'../lib/log4js',
|
||||||
, { requires:
|
{ requires:
|
||||||
{ './appenders/file':
|
{ './appenders/fake':
|
||||||
{
|
{
|
||||||
name: "file"
|
name: "fake",
|
||||||
, appender: function() {}
|
appender: function() {},
|
||||||
, configure: function(configuration) {
|
configure: function(configuration, options) {
|
||||||
appenderConfig = configuration;
|
appenderOptions = options;
|
||||||
return function() {};
|
return function() {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
),
|
||||||
, config = {
|
config = {
|
||||||
"appenders": [
|
"appenders": [
|
||||||
{
|
{
|
||||||
"type" : "file",
|
"type" : "fake",
|
||||||
"filename" : "cheesy-wotsits.log",
|
"filename" : "cheesy-wotsits.log"
|
||||||
"maxLogSize" : 1024,
|
|
||||||
"backups" : 3,
|
|
||||||
"pollInterval" : 15
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
log4js.configure(config, {
|
log4js.configure(config, {
|
||||||
cwd: '/absolute/path/to'
|
cwd: '/absolute/path/to'
|
||||||
});
|
});
|
||||||
return appenderConfig;
|
return appenderOptions;
|
||||||
},
|
},
|
||||||
'should be an absolute path': function(configuration) {
|
'should be passed to appenders during configuration': function(options) {
|
||||||
assert.equal(configuration.filename, '/absolute/path/to/cheesy-wotsits.log');
|
assert.equal(options.cwd, '/absolute/path/to');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'configuration passed as filename with options.cwd': {
|
'file appender': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var appenderConfig
|
var fileOpened,
|
||||||
, configFilename
|
fileAppender = sandbox.require(
|
||||||
, log4js = sandbox.require(
|
'../lib/appenders/file',
|
||||||
'../lib/log4js'
|
{ requires:
|
||||||
, { requires:
|
{ '../streams':
|
||||||
{ 'fs':
|
|
||||||
{
|
{
|
||||||
statSync: function() {
|
RollingFileStream: function(file) {
|
||||||
return { mtime: Date.now() };
|
fileOpened = file;
|
||||||
},
|
},
|
||||||
readFileSync: function(filename) {
|
BufferedWriteStream: function(other) {
|
||||||
configFilename = filename;
|
return { on: function() { }, end: function() {} }
|
||||||
return JSON.stringify({
|
|
||||||
appenders: [
|
|
||||||
{ type: "file"
|
|
||||||
, filename: "whatever.log"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
},
|
|
||||||
readdirSync: function() {
|
|
||||||
return ['file'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
, './appenders/file':
|
|
||||||
{
|
|
||||||
name: "file"
|
|
||||||
, appender: function() {}
|
|
||||||
, configure: function(configuration) {
|
|
||||||
appenderConfig = configuration;
|
|
||||||
return function() {};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
log4js.configure("/path/to/cheese.json", {
|
fileAppender.configure({ filename: "whatever.log", maxLogSize: 10 }, { cwd: '/absolute/path/to' });
|
||||||
cwd: '/absolute/path/to'
|
return fileOpened;
|
||||||
});
|
|
||||||
return [ configFilename, appenderConfig ];
|
|
||||||
},
|
},
|
||||||
'should read the config from a file': function(args) {
|
'should prepend options.cwd to config.filename': function(fileOpened) {
|
||||||
assert.equal(args[0], '/path/to/cheese.json');
|
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
|
||||||
},
|
|
||||||
'should be an absolute path': function(args) {
|
|
||||||
assert.equal(args[1].filename, "/absolute/path/to/whatever.log");
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}).export(module);
|
}).export(module);
|
Loading…
Reference in New Issue
Block a user