Lazy load any new style appenders

This commit is contained in:
Danny Brain 2011-10-27 16:25:38 +11:00
parent 25aa075fad
commit 23a2758a6d
5 changed files with 34 additions and 26 deletions

View File

@ -43,6 +43,7 @@ function createAppender(hookioOptions, actualAppender) {
function configure(config) { function configure(config) {
var actualAppender; var actualAppender;
if (config.appender && config.mode === 'master') { if (config.appender && config.mode === 'master') {
log4js.loadAppender(config.appender.type);
actualAppender = log4js.appenderMakers[config.appender.type](config.appender); actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
} }
delete config.appender; delete config.appender;

View File

@ -136,6 +136,7 @@ function configureAppenders(appenderList) {
clearAppenders(); clearAppenders();
if (appenderList) { if (appenderList) {
appenderList.forEach(function(appenderConfig) { appenderList.forEach(function(appenderConfig) {
loadAppender(appenderConfig.type);
var appender; var appender;
appenderConfig.makers = appenderMakers; appenderConfig.makers = appenderMakers;
appender = appenderMakers[appenderConfig.type](appenderConfig); appender = appenderMakers[appenderConfig.type](appenderConfig);
@ -347,15 +348,10 @@ function replaceConsole(logger) {
}); });
} }
function loadAppenders() { function loadAppender(appender) {
var appenderList = fs.readdirSync(__dirname + '/appenders'); var appenderModule = require('./appenders/' + appender);
appenderList.forEach(function(file) { module.exports.appenders[appenderModule.name] = appenderModule.appender;
if (/\.js$/.test(file)) { appenderMakers[appenderModule.name] = appenderModule.configure;
var appenderModule = require('./appenders/' + file);
module.exports.appenders[appenderModule.name] = appenderModule.appender;
appenderMakers[appenderModule.name] = appenderModule.configure;
}
});
} }
module.exports = { module.exports = {
@ -363,6 +359,7 @@ module.exports = {
getDefaultLogger: getDefaultLogger, getDefaultLogger: getDefaultLogger,
addAppender: addAppender, addAppender: addAppender,
loadAppender: loadAppender,
clearAppenders: clearAppenders, clearAppenders: clearAppenders,
configure: configure, configure: configure,
@ -375,7 +372,11 @@ module.exports = {
connectLogger: require('./connect-logger').connectLogger connectLogger: require('./connect-logger').connectLogger
}; };
loadAppenders(); //load the old-style appenders
[ 'console', 'file', 'logLevelFilter' ].forEach(function(appender) {
loadAppender(appender);
});
//set ourselves up if we can find a default log4js.json //set ourselves up if we can find a default log4js.json
configure(findConfiguration()); configure(findConfiguration());
//replace console.log, etc with log4js versions //replace console.log, etc with log4js versions

View File

@ -22,10 +22,10 @@
"lib": "lib" "lib": "lib"
}, },
"dependencies": { "dependencies": {
"hook.io": "0.7.7"
}, },
"devDependencies": { "devDependencies": {
"vows": ">=0.5.2", "vows": ">=0.5.2",
"sandboxed-module": ">= 0.1.1" "sandboxed-module": ">= 0.1.1",
"hook.io": "0.7.7"
} }
} }

View File

@ -4,13 +4,18 @@ var sandbox = require('sandboxed-module');
function fancyResultingHookioAppender(opts) { function fancyResultingHookioAppender(opts) {
var result = { ons: {}, emissions: {}, logged: [] }; var result = { ons: {}, emissions: {}, logged: [] };
var fakeLog4Js = { var fakeLog4Js = {
appenderMakers: { file: function (config) { appenderMakers: {}
};
fakeLog4Js.loadAppender = function (appender) {
fakeLog4Js.appenderMakers[appender] = function (config) {
return function log(logEvent) { return function log(logEvent) {
result.logged.push(logEvent); result.logged.push(logEvent);
} }
}} };
}; };
var fakeHookIo = { Hook: function() { } }; var fakeHookIo = { Hook: function() { } };
fakeHookIo.Hook.prototype.start = function () { fakeHookIo.Hook.prototype.start = function () {
result.startCalled = true; result.startCalled = true;
@ -30,6 +35,7 @@ function fancyResultingHookioAppender(opts) {
result.ons[on].functionToExec(data); result.ons[on].functionToExec(data);
} }
}; };
return { theResult: result, return { theResult: result,
theModule: sandbox.require('../lib/appenders/hookio', { theModule: sandbox.require('../lib/appenders/hookio', {
requires: { requires: {

View File

@ -69,7 +69,7 @@ vows.describe('log4js').addBatch({
, log4js = sandbox.require( , log4js = sandbox.require(
'../lib/log4js' '../lib/log4js'
, { requires: , { requires:
{ './appenders/file.js': { './appenders/file':
{ {
name: "file" name: "file"
, appender: function() {} , appender: function() {}
@ -123,10 +123,10 @@ vows.describe('log4js').addBatch({
}); });
}, },
readdirSync: function() { readdirSync: function() {
return ['file.js']; return ['file'];
} }
} }
, './appenders/file.js': , './appenders/file':
{ {
name: "file" name: "file"
, appender: function() {} , appender: function() {}
@ -168,7 +168,7 @@ vows.describe('log4js').addBatch({
'../lib/log4js' '../lib/log4js'
, { , {
requires: { requires: {
'./appenders/console.js': fakeConsoleAppender './appenders/console': fakeConsoleAppender
} }
} }
); );
@ -303,7 +303,7 @@ vows.describe('log4js').addBatch({
{ {
requires: { requires: {
'fs': fakeFS 'fs': fakeFS
, './appenders/console.js': fakeConsole , './appenders/console': fakeConsole
} }
} }
); );
@ -434,7 +434,7 @@ vows.describe('log4js').addBatch({
{ {
requires: { requires: {
'fs': fakeFS, 'fs': fakeFS,
'./appenders/console.js': fakeConsole './appenders/console': fakeConsole
}, },
globals: { globals: {
'console': fakeConsole, 'console': fakeConsole,
@ -462,7 +462,7 @@ vows.describe('log4js').addBatch({
assert.equal(logEvents[2].data[0], 'debug4'); assert.equal(logEvents[2].data[0], 'debug4');
} }
}, },
'configuration reload with configuration staying the same' : { 'configuration reload with configuration staying the same' : {
topic: function() { topic: function() {
var pathsChecked = [], var pathsChecked = [],
@ -494,10 +494,10 @@ vows.describe('log4js').addBatch({
} }
}, },
fakeConsole = { fakeConsole = {
'name': 'console', 'name': 'console',
'appender': function () { 'appender': function () {
return function(evt) { logEvents.push(evt); }; return function(evt) { logEvents.push(evt); };
}, },
'configure': function (config) { 'configure': function (config) {
return fakeConsole.appender(); return fakeConsole.appender();
} }
@ -510,8 +510,8 @@ vows.describe('log4js').addBatch({
'../lib/log4js', '../lib/log4js',
{ {
requires: { requires: {
'fs': fakeFS, 'fs': fakeFS,
'./appenders/console.js': fakeConsole './appenders/console': fakeConsole
}, },
globals: { globals: {
'console': fakeConsole, 'console': fakeConsole,
@ -538,7 +538,7 @@ vows.describe('log4js').addBatch({
var logEvents = args[1]; var logEvents = args[1];
assert.length(logEvents, 2); assert.length(logEvents, 2);
assert.equal(logEvents[0].data[0], 'info1'); assert.equal(logEvents[0].data[0], 'info1');
assert.equal(logEvents[1].data[0], 'info3'); assert.equal(logEvents[1].data[0], 'info3');
} }
} }