Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 351a912

Browse files
author
Gareth Jones
committed
simplified the reload config code a little, moved the tests into their own file, improved coverage
1 parent c5fd75d commit 351a912

File tree

3 files changed

+355
-250
lines changed

3 files changed

+355
-250
lines changed

lib/log4js.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,7 @@ function getDefaultLogger () {
188188
var configState = {};
189189

190190
function loadConfigurationFile(filename) {
191-
if (filename &&
192-
(!configState.lastFilename ||
193-
filename !== configState.lastFilename ||
194-
!configState.lastMTime ||
195-
fs.statSync(filename).mtime !== configState.lastMTime)
196-
) {
197-
configState.lastFilename = filename;
198-
configState.lastMTime = fs.statSync(filename).mtime;
191+
if (filename) {
199192
return JSON.parse(fs.readFileSync(filename, "utf8"));
200193
}
201194
return undefined;
@@ -222,21 +215,23 @@ function configureOnceOff(config, options) {
222215
}
223216

224217
function reloadConfiguration() {
225-
var filename = configState.filename,
226-
mtime;
218+
var mtime = getMTime(configState.filename);
219+
if (!mtime) return;
220+
221+
if (configState.lastMTime && (mtime.getTime() > configState.lastMTime.getTime())) {
222+
configureOnceOff(loadConfigurationFile(configState.filename));
223+
}
224+
configState.lastMTime = mtime;
225+
}
226+
227+
function getMTime(filename) {
228+
var mtime;
227229
try {
228-
mtime = fs.statSync(filename).mtime;
230+
mtime = fs.statSync(configState.filename).mtime;
229231
} catch (e) {
230232
getLogger('log4js').warn('Failed to load configuration file ' + filename);
231-
return;
232-
}
233-
if (configState.lastFilename && configState.lastFilename === filename) {
234-
if (mtime.getTime() > configState.lastMTime.getTime()) {
235-
configureOnceOff(loadConfigurationFile(filename));
236-
}
237-
} else {
238-
configureOnceOff(loadConfigurationFile(filename));
239233
}
234+
return mtime;
240235
}
241236

242237
function initReloadConfiguration(filename, options) {
@@ -245,6 +240,7 @@ function initReloadConfiguration(filename, options) {
245240
delete configState.timerId;
246241
}
247242
configState.filename = filename;
243+
configState.lastMTime = getMTime(filename);
248244
configState.timerId = setInterval(reloadConfiguration, options.reloadSecs*1000);
249245
}
250246

test/logging-test.js

Lines changed: 0 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -498,237 +498,6 @@ vows.describe('log4js').addBatch({
498498
assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
499499
}
500500
},
501-
'configuration reload with configuration changing' : {
502-
topic: function() {
503-
var pathsChecked = [],
504-
logEvents = [],
505-
logger,
506-
modulePath = 'path/to/log4js.json',
507-
fakeFS = {
508-
lastMtime: Date.now(),
509-
config: {
510-
appenders: [
511-
{ type: 'console', layout: { type: 'messagePassThrough' } }
512-
],
513-
levels: { 'a-test' : 'INFO' }
514-
},
515-
readdirSync: function(dir) {
516-
return require('fs').readdirSync(dir);
517-
},
518-
readFileSync: function (file, encoding) {
519-
assert.equal(file, modulePath);
520-
assert.equal(encoding, 'utf8');
521-
return JSON.stringify(fakeFS.config);
522-
},
523-
statSync: function (path) {
524-
pathsChecked.push(path);
525-
if (path === modulePath) {
526-
fakeFS.lastMtime += 1;
527-
return { mtime: new Date(fakeFS.lastMtime) };
528-
} else {
529-
throw new Error("no such file");
530-
}
531-
}
532-
},
533-
fakeConsole = {
534-
'name': 'console',
535-
'appender': function () {
536-
return function(evt) { logEvents.push(evt); };
537-
},
538-
'configure': function (config) {
539-
return fakeConsole.appender();
540-
}
541-
},
542-
setIntervalCallback,
543-
fakeSetInterval = function(cb, timeout) {
544-
setIntervalCallback = cb;
545-
},
546-
log4js = sandbox.require(
547-
'../lib/log4js',
548-
{
549-
requires: {
550-
'fs': fakeFS,
551-
'./appenders/console': fakeConsole
552-
},
553-
globals: {
554-
'console': fakeConsole,
555-
'setInterval' : fakeSetInterval,
556-
}
557-
}
558-
);
559-
560-
log4js.configure('path/to/log4js.json', { reloadSecs: 30 });
561-
logger = log4js.getLogger('a-test');
562-
logger.info("info1");
563-
logger.debug("debug2 - should be ignored");
564-
fakeFS.config.levels['a-test'] = "DEBUG";
565-
setIntervalCallback();
566-
logger.info("info3");
567-
logger.debug("debug4");
568-
569-
return logEvents;
570-
},
571-
'should configure log4js from first log4js.json found': function(logEvents) {
572-
assert.equal(logEvents[0].data[0], 'info1');
573-
assert.equal(logEvents[1].data[0], 'info3');
574-
assert.equal(logEvents[2].data[0], 'debug4');
575-
assert.equal(logEvents.length, 3);
576-
}
577-
},
578-
579-
'configuration reload with configuration staying the same' : {
580-
topic: function() {
581-
var pathsChecked = [],
582-
fileRead = 0,
583-
logEvents = [],
584-
logger,
585-
modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
586-
mtime = new Date(),
587-
fakeFS = {
588-
config: {
589-
appenders: [
590-
{ type: 'console', layout: { type: 'messagePassThrough' } }
591-
],
592-
levels: { 'a-test' : 'INFO' }
593-
},
594-
readdirSync: function(dir) {
595-
return require('fs').readdirSync(dir);
596-
},
597-
readFileSync: function (file, encoding) {
598-
fileRead += 1;
599-
assert.isString(file);
600-
assert.equal(file, modulePath);
601-
assert.equal(encoding, 'utf8');
602-
return JSON.stringify(fakeFS.config);
603-
},
604-
statSync: function (path) {
605-
pathsChecked.push(path);
606-
if (path === modulePath) {
607-
return { mtime: mtime };
608-
} else {
609-
throw new Error("no such file");
610-
}
611-
}
612-
},
613-
fakeConsole = {
614-
'name': 'console',
615-
'appender': function () {
616-
return function(evt) { logEvents.push(evt); };
617-
},
618-
'configure': function (config) {
619-
return fakeConsole.appender();
620-
}
621-
},
622-
setIntervalCallback,
623-
fakeSetInterval = function(cb, timeout) {
624-
setIntervalCallback = cb;
625-
},
626-
log4js = sandbox.require(
627-
'../lib/log4js',
628-
{
629-
requires: {
630-
'fs': fakeFS,
631-
'./appenders/console': fakeConsole
632-
},
633-
globals: {
634-
'console': fakeConsole,
635-
'setInterval' : fakeSetInterval,
636-
}
637-
}
638-
);
639-
640-
log4js.configure(modulePath, { reloadSecs: 3 });
641-
logger = log4js.getLogger('a-test');
642-
logger.info("info1");
643-
logger.debug("debug2 - should be ignored");
644-
setIntervalCallback();
645-
logger.info("info3");
646-
logger.debug("debug4");
647-
648-
return [ pathsChecked, logEvents, modulePath, fileRead ];
649-
},
650-
'should only read the configuration file once': function(args) {
651-
var fileRead = args[3];
652-
assert.equal(fileRead, 1);
653-
},
654-
'should configure log4js from first log4js.json found': function(args) {
655-
var logEvents = args[1];
656-
assert.equal(logEvents.length, 2);
657-
assert.equal(logEvents[0].data[0], 'info1');
658-
assert.equal(logEvents[1].data[0], 'info3');
659-
}
660-
},
661-
662-
'reload config when passed an object': {
663-
topic: function() {
664-
var test = setupConsoleTest();
665-
test.log4js.configure({}, { reloadSecs: 30 });
666-
return test.logEvents;
667-
},
668-
'should log a warning': function(events) {
669-
assert.equal(events[0].level.toString(), 'WARN');
670-
assert.equal(
671-
events[0].data[0],
672-
'Ignoring configuration reload parameter for "object" configuration.'
673-
);
674-
}
675-
},
676-
677-
'configure called twice with reload options': {
678-
topic: function() {
679-
var modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
680-
fakeFS = {
681-
readFileSync: function (file, encoding) {
682-
return JSON.stringify({});
683-
},
684-
statSync: function (path) {
685-
return { mtime: new Date() };
686-
}
687-
},
688-
fakeConsole = {
689-
'name': 'console',
690-
'appender': function () {
691-
return function(evt) { };
692-
},
693-
'configure': function (config) {
694-
return fakeConsole.appender();
695-
}
696-
},
697-
setIntervalCallback,
698-
intervalCleared = false,
699-
clearedId,
700-
fakeSetInterval = function(cb, timeout) {
701-
setIntervalCallback = cb;
702-
return 1234;
703-
},
704-
log4js = sandbox.require(
705-
'../lib/log4js',
706-
{
707-
requires: {
708-
'fs': fakeFS,
709-
'./appenders/console': fakeConsole
710-
},
711-
globals: {
712-
'console': fakeConsole,
713-
'setInterval' : fakeSetInterval,
714-
'clearInterval': function(interval) {
715-
intervalCleared = true;
716-
clearedId = interval;
717-
}
718-
}
719-
}
720-
);
721-
722-
log4js.configure(modulePath, { reloadSecs: 3 });
723-
log4js.configure(modulePath, { reloadSecs: 15 });
724-
725-
return { cleared: intervalCleared, id: clearedId };
726-
},
727-
'should clear the previous interval': function(result) {
728-
assert.isTrue(result.cleared);
729-
assert.equal(result.id, 1234);
730-
}
731-
},
732501

733502
'getDefaultLogger': {
734503
topic: function() {

0 commit comments

Comments
 (0)