2 var vows = require('vows')
3 , assert = require('assert')
4 , sandbox = require('sandboxed-module');
6 function setupConsoleTest() {
11 ['trace','debug','log','info','warn','error'].forEach(function(fn) {
12 fakeConsole[fn] = function() {
13 throw new Error("this should not be called.");
17 log4js = sandbox.require(
26 log4js.clearAppenders();
27 log4js.addAppender(function(evt) {
31 return { log4js: log4js, logEvents: logEvents, fakeConsole: fakeConsole };
34 vows.describe('reload configuration').addBatch({
35 'with config file changing' : {
37 var pathsChecked = [],
40 modulePath = 'path/to/log4js.json',
42 lastMtime: Date.now(),
45 { type: 'console', layout: { type: 'messagePassThrough' } }
47 levels: { 'a-test' : 'INFO' }
49 readFileSync: function (file, encoding) {
50 assert.equal(file, modulePath);
51 assert.equal(encoding, 'utf8');
52 return JSON.stringify(fakeFS.config);
54 statSync: function (path) {
55 pathsChecked.push(path);
56 if (path === modulePath) {
57 fakeFS.lastMtime += 1;
58 return { mtime: new Date(fakeFS.lastMtime) };
60 throw new Error("no such file");
66 'appender': function () {
67 return function(evt) { logEvents.push(evt); };
69 'configure': function (config) {
70 return fakeConsole.appender();
74 fakeSetInterval = function(cb, timeout) {
75 setIntervalCallback = cb;
77 log4js = sandbox.require(
82 './appenders/console': fakeConsole
85 'console': fakeConsole,
86 'setInterval' : fakeSetInterval,
91 log4js.configure('path/to/log4js.json', { reloadSecs: 30 });
92 logger = log4js.getLogger('a-test');
94 logger.debug("debug2 - should be ignored");
95 fakeFS.config.levels['a-test'] = "DEBUG";
96 setIntervalCallback();
98 logger.debug("debug4");
102 'should configure log4js from first log4js.json found': function(logEvents) {
103 assert.equal(logEvents[0].data[0], 'info1');
104 assert.equal(logEvents[1].data[0], 'info3');
105 assert.equal(logEvents[2].data[0], 'debug4');
106 assert.equal(logEvents.length, 3);
110 'with config file staying the same' : {
112 var pathsChecked = [],
116 modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
121 { type: 'console', layout: { type: 'messagePassThrough' } }
123 levels: { 'a-test' : 'INFO' }
125 readFileSync: function (file, encoding) {
127 assert.isString(file);
128 assert.equal(file, modulePath);
129 assert.equal(encoding, 'utf8');
130 return JSON.stringify(fakeFS.config);
132 statSync: function (path) {
133 pathsChecked.push(path);
134 if (path === modulePath) {
135 return { mtime: mtime };
137 throw new Error("no such file");
143 'appender': function () {
144 return function(evt) { logEvents.push(evt); };
146 'configure': function (config) {
147 return fakeConsole.appender();
151 fakeSetInterval = function(cb, timeout) {
152 setIntervalCallback = cb;
154 log4js = sandbox.require(
159 './appenders/console': fakeConsole
162 'console': fakeConsole,
163 'setInterval' : fakeSetInterval,
168 log4js.configure(modulePath, { reloadSecs: 3 });
169 logger = log4js.getLogger('a-test');
170 logger.info("info1");
171 logger.debug("debug2 - should be ignored");
172 setIntervalCallback();
173 logger.info("info3");
174 logger.debug("debug4");
176 return [ pathsChecked, logEvents, modulePath, fileRead ];
178 'should only read the configuration file once': function(args) {
179 var fileRead = args[3];
180 assert.equal(fileRead, 1);
182 'should configure log4js from first log4js.json found': function(args) {
183 var logEvents = args[1];
184 assert.equal(logEvents.length, 2);
185 assert.equal(logEvents[0].data[0], 'info1');
186 assert.equal(logEvents[1].data[0], 'info3');
190 'when config file is removed': {
192 var pathsChecked = [],
196 modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
201 { type: 'console', layout: { type: 'messagePassThrough' } }
203 levels: { 'a-test' : 'INFO' }
205 readFileSync: function (file, encoding) {
207 assert.isString(file);
208 assert.equal(file, modulePath);
209 assert.equal(encoding, 'utf8');
210 return JSON.stringify(fakeFS.config);
212 statSync: function (path) {
213 this.statSync = function() {
214 throw new Error("no such file");
216 return { mtime: new Date() };
221 'appender': function () {
222 return function(evt) { logEvents.push(evt); };
224 'configure': function (config) {
225 return fakeConsole.appender();
229 fakeSetInterval = function(cb, timeout) {
230 setIntervalCallback = cb;
232 log4js = sandbox.require(
237 './appenders/console': fakeConsole
240 'console': fakeConsole,
241 'setInterval' : fakeSetInterval,
246 log4js.configure(modulePath, { reloadSecs: 3 });
247 logger = log4js.getLogger('a-test');
248 logger.info("info1");
249 logger.debug("debug2 - should be ignored");
250 setIntervalCallback();
251 logger.info("info3");
252 logger.debug("debug4");
254 return [ pathsChecked, logEvents, modulePath, fileRead ];
256 'should only read the configuration file once': function(args) {
257 var fileRead = args[3];
258 assert.equal(fileRead, 1);
260 'should not clear configuration when config file not found': function(args) {
261 var logEvents = args[1];
262 assert.equal(logEvents.length, 3);
263 assert.equal(logEvents[0].data[0], 'info1');
264 assert.equal(logEvents[1].level.toString(), 'WARN');
265 assert.include(logEvents[1].data[0], 'Failed to load configuration file');
266 assert.equal(logEvents[2].data[0], 'info3');
270 'when passed an object': {
272 var test = setupConsoleTest();
273 test.log4js.configure({}, { reloadSecs: 30 });
274 return test.logEvents;
276 'should log a warning': function(events) {
277 assert.equal(events[0].level.toString(), 'WARN');
280 'Ignoring configuration reload parameter for "object" configuration.'
285 'when called twice with reload options': {
287 var modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
289 readFileSync: function (file, encoding) {
290 return JSON.stringify({});
292 statSync: function (path) {
293 return { mtime: new Date() };
298 'appender': function () {
299 return function(evt) { };
301 'configure': function (config) {
302 return fakeConsole.appender();
306 intervalCleared = false,
308 fakeSetInterval = function(cb, timeout) {
309 setIntervalCallback = cb;
312 log4js = sandbox.require(
317 './appenders/console': fakeConsole
320 'console': fakeConsole,
321 'setInterval' : fakeSetInterval,
322 'clearInterval': function(interval) {
323 intervalCleared = true;
324 clearedId = interval;
330 log4js.configure(modulePath, { reloadSecs: 3 });
331 log4js.configure(modulePath, { reloadSecs: 15 });
333 return { cleared: intervalCleared, id: clearedId };
335 'should clear the previous interval': function(result) {
336 assert.isTrue(result.cleared);
337 assert.equal(result.id, 1234);