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('log4js').addBatch({
36 'getBufferedLogger': {
38 var log4js = require('../lib/log4js');
39 log4js.clearAppenders();
40 var logger = log4js.getBufferedLogger('tests');
44 'should take a category and return a logger': function (logger) {
45 assert.equal(logger.target.category, 'tests');
46 assert.isFunction(logger.flush);
47 assert.isFunction(logger.trace);
48 assert.isFunction(logger.debug);
49 assert.isFunction(logger.info);
50 assert.isFunction(logger.warn);
51 assert.isFunction(logger.error);
52 assert.isFunction(logger.fatal);
57 var log4js = require('../lib/log4js');
58 log4js.clearAppenders();
59 var logger = log4js.getBufferedLogger('tests1');
61 logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
62 logger.debug("Debug event");
63 logger.trace("Trace event 1");
64 logger.trace("Trace event 2");
65 logger.warn("Warning event");
66 logger.error("Aargh!", new Error("Pants are on fire!"));
68 "Simulated CouchDB problem",
69 { err: 127, cause: "incendiary underwear" }
74 'should not emit log events if .flush() is not called.': function (events) {
75 assert.equal(events.length, 0);
79 'log events after flush() is called': {
81 var log4js = require('../lib/log4js');
82 log4js.clearAppenders();
83 var logger = log4js.getBufferedLogger('tests2');
84 logger.target.setLevel("TRACE");
86 logger.target.addListener("log", function (logEvent) { events.push(logEvent); });
87 logger.debug("Debug event");
88 logger.trace("Trace event 1");
89 logger.trace("Trace event 2");
90 logger.warn("Warning event");
91 logger.error("Aargh!", new Error("Pants are on fire!"));
93 "Simulated CouchDB problem",
94 { err: 127, cause: "incendiary underwear" }
100 'should emit log events when .flush() is called.': function (events) {
101 assert.equal(events.length, 6);
109 var log4js = require('../lib/log4js');
110 log4js.clearAppenders();
111 var logger = log4js.getLogger('tests');
112 logger.setLevel("DEBUG");
116 'should take a category and return a logger': function(logger) {
117 assert.equal(logger.category, 'tests');
118 assert.equal(logger.level.toString(), "DEBUG");
119 assert.isFunction(logger.debug);
120 assert.isFunction(logger.info);
121 assert.isFunction(logger.warn);
122 assert.isFunction(logger.error);
123 assert.isFunction(logger.fatal);
127 topic: function(logger) {
129 logger.addListener("log", function (logEvent) { events.push(logEvent); });
130 logger.debug("Debug event");
131 logger.trace("Trace event 1");
132 logger.trace("Trace event 2");
133 logger.warn("Warning event");
134 logger.error("Aargh!", new Error("Pants are on fire!"));
135 logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
139 'should emit log events': function(events) {
140 assert.equal(events[0].level.toString(), 'DEBUG');
141 assert.equal(events[0].data[0], 'Debug event');
142 assert.instanceOf(events[0].startTime, Date);
145 'should not emit events of a lower level': function(events) {
146 assert.equal(events.length, 4);
147 assert.equal(events[1].level.toString(), 'WARN');
150 'should include the error if passed in': function(events) {
151 assert.instanceOf(events[2].data[1], Error);
152 assert.equal(events[2].data[1].message, 'Pants are on fire!');
157 'when shutdown is called': {
159 var callback = this.callback;
161 appenderShutdownCalled: false,
162 shutdownCallbackCalled: false
164 log4js = sandbox.require(
171 appender: function() {},
172 configure: function(configuration) {
173 return function() {};
175 shutdown: function(cb) {
176 events.appenderShutdownCalled = true;
183 config = { appenders:
185 "filename" : "cheesy-wotsits.log",
192 log4js.configure(config);
193 log4js.shutdown(function shutdownCallback() {
194 events.shutdownCallbackCalled = true;
195 // Re-enable log writing so other tests that use logger are not
197 require('../lib/logger').enableAllLogWrites();
198 callback(null, events);
202 'should invoke appender shutdowns': function(events) {
203 assert.ok(events.appenderShutdownCalled);
206 'should call callback': function(events) {
207 assert.ok(events.shutdownCallbackCalled);
211 'invalid configuration': {
212 'should throw an exception': function() {
213 assert.throws(function() {
214 require('log4js').configure({ "type": "invalid" });
219 'configuration when passed as object': {
222 log4js = sandbox.require(
229 appender: function() {},
230 configure: function(configuration) {
231 appenderConfig = configuration;
232 return function() {};
238 config = { appenders:
240 "filename" : "cheesy-wotsits.log",
246 log4js.configure(config);
247 return appenderConfig;
249 'should be passed to appender config': function(configuration) {
250 assert.equal(configuration.filename, 'cheesy-wotsits.log');
254 'configuration that causes an error': {
256 var log4js = sandbox.require(
263 appender: function() {},
264 configure: function(configuration) {
265 throw new Error("oh noes");
271 config = { appenders:
273 "filename" : "cheesy-wotsits.log",
280 log4js.configure(config);
285 'should wrap error in a meaningful message': function(e) {
286 assert.ok(e.message.indexOf('log4js configuration problem for') > -1);
290 'configuration when passed as filename': {
294 log4js = sandbox.require(
300 return { mtime: Date.now() };
304 configFilename = filename;
305 return JSON.stringify({
308 , filename: "whatever.log"
320 appender: function() {},
321 configure: function(configuration) {
322 appenderConfig = configuration;
323 return function() {};
329 log4js.configure("/path/to/cheese.json");
330 return [ configFilename, appenderConfig ];
332 'should read the config from a file': function(args) {
333 assert.equal(args[0], '/path/to/cheese.json');
335 'should pass config to appender': function(args) {
336 assert.equal(args[1].filename, "whatever.log");
340 'with no appenders defined' : {
344 fakeConsoleAppender = {
346 appender: function() {
347 return function(evt) {
348 that.callback(null, evt);
351 configure: function() {
352 return fakeConsoleAppender.appender();
355 log4js = sandbox.require(
359 './appenders/console': fakeConsoleAppender
363 logger = log4js.getLogger("some-logger");
364 logger.debug("This is a test");
366 'should default to the console appender': function(evt) {
367 assert.equal(evt.data[0], "This is a test");
373 var log4js = require('../lib/log4js');
374 log4js.clearAppenders();
377 'without a category': {
378 'should register the function as a listener for all loggers': function (log4js) {
380 appender = function(evt) { appenderEvent = evt; },
381 logger = log4js.getLogger("tests");
383 log4js.addAppender(appender);
384 logger.debug("This is a test");
385 assert.equal(appenderEvent.data[0], "This is a test");
386 assert.equal(appenderEvent.categoryName, "tests");
387 assert.equal(appenderEvent.level.toString(), "DEBUG");
389 'if an appender for a category is defined': {
390 'should register for that category': function (log4js) {
395 log4js.addAppender(function (evt) { appenderEvent = evt; });
396 log4js.addAppender(function (evt) { otherEvent = evt; }, 'cheese');
398 cheeseLogger = log4js.getLogger('cheese');
399 cheeseLogger.debug('This is a test');
400 assert.deepEqual(appenderEvent, otherEvent);
401 assert.equal(otherEvent.data[0], 'This is a test');
402 assert.equal(otherEvent.categoryName, 'cheese');
404 otherEvent = undefined;
405 appenderEvent = undefined;
406 log4js.getLogger('pants').debug("this should not be propagated to otherEvent");
407 assert.isUndefined(otherEvent);
408 assert.equal(appenderEvent.data[0], "this should not be propagated to otherEvent");
414 'should only register the function as a listener for that category': function(log4js) {
416 appender = function(evt) { appenderEvent = evt; },
417 logger = log4js.getLogger("tests");
419 log4js.addAppender(appender, 'tests');
420 logger.debug('this is a category test');
421 assert.equal(appenderEvent.data[0], 'this is a category test');
423 appenderEvent = undefined;
424 log4js.getLogger('some other category').debug('Cheese');
425 assert.isUndefined(appenderEvent);
429 'with multiple categories': {
430 'should register the function as a listener for all the categories': function(log4js) {
432 appender = function(evt) { appenderEvent = evt; },
433 logger = log4js.getLogger('tests');
435 log4js.addAppender(appender, 'tests', 'biscuits');
437 logger.debug('this is a test');
438 assert.equal(appenderEvent.data[0], 'this is a test');
439 appenderEvent = undefined;
441 var otherLogger = log4js.getLogger('biscuits');
442 otherLogger.debug("mmm... garibaldis");
443 assert.equal(appenderEvent.data[0], "mmm... garibaldis");
445 appenderEvent = undefined;
447 log4js.getLogger("something else").debug("pants");
448 assert.isUndefined(appenderEvent);
450 'should register the function when the list of categories is an array': function(log4js) {
452 appender = function(evt) { appenderEvent = evt; };
454 log4js.addAppender(appender, ['tests', 'pants']);
456 log4js.getLogger('tests').debug('this is a test');
457 assert.equal(appenderEvent.data[0], 'this is a test');
459 appenderEvent = undefined;
461 log4js.getLogger('pants').debug("big pants");
462 assert.equal(appenderEvent.data[0], "big pants");
464 appenderEvent = undefined;
466 log4js.getLogger("something else").debug("pants");
467 assert.isUndefined(appenderEvent);
474 var appenderEvents = [],
477 'appender': function () {
478 return function(evt) {
479 appenderEvents.push(evt);
482 'configure': function (config) {
483 return fakeConsole.appender();
489 log4js = sandbox.require(
493 './appenders/console': fakeConsole
496 console: globalConsole
500 logger = log4js.getLogger('a-test');
502 logger.debug("this is a test");
503 globalConsole.log("this should not be logged");
505 return appenderEvents;
508 'should configure a console appender': function(appenderEvents) {
509 assert.equal(appenderEvents[0].data[0], 'this is a test');
512 'should not replace console.log with log4js version': function(appenderEvents) {
513 assert.equal(appenderEvents.length, 1);
518 topic: setupConsoleTest,
520 'when replaceConsole called': {
521 topic: function(test) {
522 test.log4js.replaceConsole();
524 test.fakeConsole.log("Some debug message someone put in a module");
525 test.fakeConsole.debug("Some debug");
526 test.fakeConsole.error("An error");
527 test.fakeConsole.info("some info");
528 test.fakeConsole.warn("a warning");
530 test.fakeConsole.log("cheese (%s) and biscuits (%s)", "gouda", "garibaldis");
531 test.fakeConsole.log({ lumpy: "tapioca" });
532 test.fakeConsole.log("count %d", 123);
533 test.fakeConsole.log("stringify %j", { lumpy: "tapioca" });
535 return test.logEvents;
538 'should replace console.log methods with log4js ones': function(logEvents) {
539 assert.equal(logEvents.length, 9);
540 assert.equal(logEvents[0].data[0], "Some debug message someone put in a module");
541 assert.equal(logEvents[0].level.toString(), "INFO");
542 assert.equal(logEvents[1].data[0], "Some debug");
543 assert.equal(logEvents[1].level.toString(), "DEBUG");
544 assert.equal(logEvents[2].data[0], "An error");
545 assert.equal(logEvents[2].level.toString(), "ERROR");
546 assert.equal(logEvents[3].data[0], "some info");
547 assert.equal(logEvents[3].level.toString(), "INFO");
548 assert.equal(logEvents[4].data[0], "a warning");
549 assert.equal(logEvents[4].level.toString(), "WARN");
550 assert.equal(logEvents[5].data[0], "cheese (%s) and biscuits (%s)");
551 assert.equal(logEvents[5].data[1], "gouda");
552 assert.equal(logEvents[5].data[2], "garibaldis");
556 topic: function(test) {
557 test.log4js.restoreConsole();
559 test.fakeConsole.log("This should cause the error described in the setup");
564 'should call the original console methods': function (err) {
565 assert.instanceOf(err, Error);
566 assert.equal(err.message, "this should not be called.");
570 'console configuration': {
571 topic: setupConsoleTest,
573 topic: function(test) {
574 test.log4js.replaceConsole();
575 test.log4js.configure({ replaceConsole: false });
577 test.fakeConsole.log("This should cause the error described in the setup");
582 'should allow for turning off console replacement': function (err) {
583 assert.instanceOf(err, Error);
584 assert.equal(err.message, 'this should not be called.');
588 topic: function(test) {
589 test.log4js.restoreConsole();
590 test.log4js.configure({ replaceConsole: true });
591 //log4js.configure clears all appenders
592 test.log4js.addAppender(function(evt) {
593 test.logEvents.push(evt);
596 test.fakeConsole.debug("Some debug");
597 return test.logEvents;
600 'should allow for turning on console replacement': function (logEvents) {
601 assert.equal(logEvents.length, 1);
602 assert.equal(logEvents[0].level.toString(), "DEBUG");
603 assert.equal(logEvents[0].data[0], "Some debug");
607 'configuration persistence' : {
610 firstLog4js = require('../lib/log4js'),
613 firstLog4js.clearAppenders();
614 firstLog4js.addAppender(function(evt) { logEvent = evt; });
616 secondLog4js = require('../lib/log4js');
617 secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
621 'should maintain appenders between requires': function (logEvent) {
622 assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
626 'getDefaultLogger': {
628 return require('../lib/log4js').getDefaultLogger();
630 'should return a logger': function(logger) {
631 assert.ok(logger.info);
632 assert.ok(logger.debug);
633 assert.ok(logger.error);