From 11b21ce77360ae7e730d0bb173f186d04f2760ec Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 1 Feb 2018 07:58:46 +1100 Subject: [PATCH] fix(webpack): added loading of core appenders --- lib/appenders/index.js | 23 +++++++++++++---------- lib/categories.js | 3 --- lib/log4js.js | 30 +++++++++++++++++------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/appenders/index.js b/lib/appenders/index.js index c5448e0..930d574 100644 --- a/lib/appenders/index.js +++ b/lib/appenders/index.js @@ -5,6 +5,14 @@ const clustering = require('../clustering'); const levels = require('../levels'); const layouts = require('../layouts'); +// pre-load the core appenders so that webpack can find them +const coreAppenders = new Map(); +coreAppenders.set('console', require('./console')); +coreAppenders.set('stdout', require('./stdout')); +coreAppenders.set('stderr', require('./stderr')); +coreAppenders.set('file', require('./file')); +coreAppenders.set('dateFile', require('./dateFile')); + const appenders = new Map(); const tryLoading = (modulePath, config) => { @@ -22,10 +30,11 @@ const tryLoading = (modulePath, config) => { } }; -const loadAppenderModule = (type, config) => tryLoading(`./${type}`, config) || - tryLoading(type, config) || - tryLoading(path.join(path.dirname(require.main.filename), type), config) || - tryLoading(path.join(process.cwd(), type), config); +const loadAppenderModule = (type, config) => coreAppenders.get(type) || + tryLoading(`./${type}`, config) || + tryLoading(type, config) || + tryLoading(path.join(path.dirname(require.main.filename), type), config) || + tryLoading(path.join(process.cwd(), type), config); const createAppender = (name, config) => { const appenderConfig = config.appenders[name]; @@ -64,12 +73,6 @@ const setup = (config) => { }); }; -// setup({ -// appenders: { -// stdout: { type: 'stdout' } -// } -// }); - configuration.addListener((config) => { configuration.throwExceptionIf( config, diff --git a/lib/categories.js b/lib/categories.js index b107c66..5069669 100644 --- a/lib/categories.js +++ b/lib/categories.js @@ -83,9 +83,6 @@ const setup = (config) => { }); }; -// setup({ -// categories: { default: { appenders: ['stdout'], level: 'OFF' } } -// }); configuration.addListener(setup); const configForCategory = (category) => { diff --git a/lib/log4js.js b/lib/log4js.js index d015211..1ef0196 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -42,19 +42,6 @@ function sendLogEventToAppender(logEvent) { }); } -/** - * Get a logger instance. - * @static - * @param loggerCategoryName - * @return {Logger} instance of logger for the category - */ -function getLogger(category) { - if (!enabled) { - configure(process.env.LOG4JS_CONFIG || defaultConfig); - } - return new Logger(category || 'default'); -} - function loadConfigurationFile(filename) { if (filename) { debug(`Loading configuration from ${filename}`); @@ -118,6 +105,23 @@ function shutdown(cb) { return null; } +/** + * Get a logger instance. + * @static + * @param loggerCategoryName + * @return {Logger} instance of logger for the category + */ +function getLogger(category) { + if (!enabled) { + configure(process.env.LOG4JS_CONFIG || { + appenders: { out: { type: 'stdout' } }, + categories: { default: { appenders: ['out'], level: 'OFF' } } + }); + } + return new Logger(category || 'default'); +} + + /** * @name log4js * @namespace Log4js