From d92d8855187f641e534316e8b4dec18cd9bfb3e6 Mon Sep 17 00:00:00 2001 From: Tomas Mihalcin Date: Mon, 15 Jan 2018 09:33:54 +0100 Subject: [PATCH] fix: deferred logger configuration. logger will be configured either by user or at first call to getLogger --- lib/log4js.js | 6 ++++-- test/tap/configuration-test.js | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index c0b72d8..cb7d8fe 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -163,6 +163,10 @@ function isMaster() { * @return {Logger} instance of logger for the category */ function getLogger(category) { + if (!enabled) { + configure(process.env.LOG4JS_CONFIG || defaultConfig); + } + const cat = category || 'default'; debug(`creating logger as ${isMaster() ? 'master' : 'worker'}`); return new Logger((isMaster() ? sendLogEventToAppender : workerDispatch), cat); @@ -285,5 +289,3 @@ const log4js = { }; module.exports = log4js; -// set ourselves up -configure(process.env.LOG4JS_CONFIG || defaultConfig); diff --git a/test/tap/configuration-test.js b/test/tap/configuration-test.js index 0e44907..5b0576d 100644 --- a/test/tap/configuration-test.js +++ b/test/tap/configuration-test.js @@ -37,7 +37,7 @@ test('log4js configure', (batch) => { } }; - sandbox.require( + const log4js = sandbox.require( '../../lib/log4js', { requires: { @@ -46,6 +46,8 @@ test('log4js configure', (batch) => { } ); + log4js.getLogger('test-logger'); + delete process.env.LOG4JS_CONFIG; t.equal(fileRead, 1, 'should load the specified local config file');