2011-07-12 11:02:48 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, sandbox = require('sandboxed-module');
|
2010-12-04 17:47:30 +08:00
|
|
|
|
|
|
|
vows.describe('log4js').addBatch({
|
|
|
|
'getLogger': {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var log4js = require('../lib/log4js');
|
2010-12-04 17:47:30 +08:00
|
|
|
log4js.clearAppenders();
|
|
|
|
var logger = log4js.getLogger('tests');
|
|
|
|
logger.setLevel("DEBUG");
|
|
|
|
return logger;
|
|
|
|
},
|
|
|
|
|
|
|
|
'should take a category and return a logger': function(logger) {
|
|
|
|
assert.equal(logger.category, 'tests');
|
|
|
|
assert.equal(logger.level.toString(), "DEBUG");
|
|
|
|
assert.isFunction(logger.debug);
|
|
|
|
assert.isFunction(logger.info);
|
|
|
|
assert.isFunction(logger.warn);
|
|
|
|
assert.isFunction(logger.error);
|
|
|
|
assert.isFunction(logger.fatal);
|
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-04 17:47:30 +08:00
|
|
|
'log events' : {
|
|
|
|
topic: function(logger) {
|
|
|
|
var events = [];
|
|
|
|
logger.addListener("log", function (logEvent) { events.push(logEvent); });
|
|
|
|
logger.debug("Debug event");
|
|
|
|
logger.trace("Trace event 1");
|
|
|
|
logger.trace("Trace event 2");
|
|
|
|
logger.warn("Warning event");
|
2010-12-11 18:55:21 +08:00
|
|
|
logger.error("Aargh!", new Error("Pants are on fire!"));
|
2010-12-11 18:59:50 +08:00
|
|
|
logger.error("Simulated CouchDB problem", { err: 127, cause: "incendiary underwear" });
|
2010-12-04 17:47:30 +08:00
|
|
|
return events;
|
|
|
|
},
|
|
|
|
|
|
|
|
'should emit log events': function(events) {
|
|
|
|
assert.equal(events[0].level.toString(), 'DEBUG');
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(events[0].data[0], 'Debug event');
|
2010-12-04 17:47:30 +08:00
|
|
|
assert.instanceOf(events[0].startTime, Date);
|
|
|
|
},
|
|
|
|
|
|
|
|
'should not emit events of a lower level': function(events) {
|
2010-12-11 18:55:21 +08:00
|
|
|
assert.length(events, 4);
|
2010-12-04 17:47:30 +08:00
|
|
|
assert.equal(events[1].level.toString(), 'WARN');
|
2010-12-11 18:55:21 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'should include the error if passed in': function (events) {
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.instanceOf(events[2].data[1], Error);
|
|
|
|
assert.equal(events[2].data[1].message, 'Pants are on fire!');
|
|
|
|
}
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-04 17:47:30 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
'fileAppender': {
|
|
|
|
topic: function() {
|
2011-07-17 18:49:39 +08:00
|
|
|
var appender
|
|
|
|
, logmessages = []
|
|
|
|
, thing = "thing"
|
|
|
|
, fakeFS = {
|
|
|
|
createWriteStream: function() {
|
2010-12-04 17:47:30 +08:00
|
|
|
assert.equal(arguments[0], './tmp-tests.log');
|
2011-07-17 18:49:39 +08:00
|
|
|
assert.isObject(arguments[1]);
|
|
|
|
assert.equal(arguments[1].flags, 'a');
|
|
|
|
assert.equal(arguments[1].mode, 0644);
|
|
|
|
assert.equal(arguments[1].encoding, 'utf8');
|
|
|
|
return {
|
|
|
|
write: function(message) {
|
|
|
|
logmessages.push(message);
|
|
|
|
}
|
|
|
|
, end: function() {}
|
|
|
|
, destroySoon: function() {}
|
|
|
|
};
|
2010-12-06 06:14:20 +08:00
|
|
|
},
|
|
|
|
watchFile: function() {
|
|
|
|
throw new Error("watchFile should not be called if logSize is not defined");
|
|
|
|
}
|
2010-12-04 17:47:30 +08:00
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js',
|
|
|
|
{
|
|
|
|
requires: {
|
|
|
|
'fs': fakeFS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-12-04 17:47:30 +08:00
|
|
|
log4js.clearAppenders();
|
2011-07-12 11:02:48 +08:00
|
|
|
|
|
|
|
appender = log4js.fileAppender('./tmp-tests.log', log4js.layouts.messagePassThroughLayout);
|
|
|
|
log4js.addAppender(appender, 'file-test');
|
2010-12-04 17:47:30 +08:00
|
|
|
|
|
|
|
var logger = log4js.getLogger('file-test');
|
|
|
|
logger.debug("this is a test");
|
|
|
|
|
|
|
|
return logmessages;
|
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
'should write log messages to file': function(logmessages) {
|
2010-12-04 17:47:30 +08:00
|
|
|
assert.length(logmessages, 1);
|
|
|
|
assert.equal(logmessages, "this is a test\n");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-05 14:17:37 +08:00
|
|
|
'fileAppender - with rolling based on size and number of files to keep': {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var watchCb,
|
|
|
|
filesOpened = [],
|
2011-07-17 18:49:39 +08:00
|
|
|
filesEnded = [],
|
2010-12-05 14:17:37 +08:00
|
|
|
filesRenamed = [],
|
|
|
|
newFilenames = [],
|
|
|
|
existingFiles = ['tests.log'],
|
2011-07-12 11:02:48 +08:00
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js'
|
|
|
|
, {
|
|
|
|
requires: {
|
|
|
|
'fs': {
|
|
|
|
watchFile: function(file, options, callback) {
|
|
|
|
assert.equal(file, 'tests.log');
|
|
|
|
assert.equal(options.persistent, false);
|
|
|
|
assert.equal(options.interval, 30000);
|
|
|
|
assert.isFunction(callback);
|
|
|
|
watchCb = callback;
|
|
|
|
},
|
2011-07-17 18:49:39 +08:00
|
|
|
createWriteStream: function(file) {
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(file, 'tests.log');
|
|
|
|
filesOpened.push(file);
|
2011-07-17 18:49:39 +08:00
|
|
|
return {
|
|
|
|
end: function() {
|
|
|
|
filesEnded.push(file);
|
|
|
|
}
|
|
|
|
};
|
2011-07-12 11:02:48 +08:00
|
|
|
},
|
|
|
|
statSync: function(file) {
|
|
|
|
if (existingFiles.indexOf(file) < 0) {
|
|
|
|
throw new Error("this file doesn't exist");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
renameSync: function(oldFile, newFile) {
|
|
|
|
filesRenamed.push(oldFile);
|
|
|
|
existingFiles.push(newFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-12-05 14:17:37 +08:00
|
|
|
var appender = log4js.fileAppender('tests.log', log4js.messagePassThroughLayout, 1024, 2, 30);
|
2011-07-21 18:42:14 +08:00
|
|
|
return [watchCb, filesOpened, filesEnded, filesRenamed, existingFiles];
|
2010-12-05 14:17:37 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'should close current log file, rename all old ones, open new one on rollover': function(args) {
|
2011-07-17 18:49:39 +08:00
|
|
|
var watchCb = args[0]
|
|
|
|
, filesOpened = args[1]
|
|
|
|
, filesEnded = args[2]
|
2011-07-21 18:42:14 +08:00
|
|
|
, filesRenamed = args[3]
|
|
|
|
, existingFiles = args[4];
|
2011-07-17 18:49:39 +08:00
|
|
|
assert.isFunction(watchCb);
|
|
|
|
//tell the watchCb that the file is below the threshold
|
|
|
|
watchCb({ size: 891 }, { size: 0 });
|
|
|
|
//filesOpened should still be the first one.
|
|
|
|
assert.length(filesOpened, 1);
|
|
|
|
//tell the watchCb that the file is now over the threshold
|
|
|
|
watchCb({ size: 1053 }, { size: 891 });
|
|
|
|
//it should have closed the first log file.
|
|
|
|
assert.length(filesEnded, 1);
|
|
|
|
//it should have renamed the previous log file
|
|
|
|
assert.length(filesRenamed, 1);
|
|
|
|
//and we should have two files now
|
|
|
|
assert.length(existingFiles, 2);
|
|
|
|
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1']);
|
|
|
|
//and opened a new log file.
|
|
|
|
assert.length(filesOpened, 2);
|
|
|
|
|
|
|
|
//now tell the watchCb that we've flipped over the threshold again
|
|
|
|
watchCb({ size: 1025 }, { size: 123 });
|
|
|
|
//it should have closed the old file
|
|
|
|
assert.length(filesEnded, 2);
|
|
|
|
//it should have renamed both the old log file, and the previous '.1' file
|
|
|
|
assert.length(filesRenamed, 3);
|
|
|
|
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log' ]);
|
|
|
|
//it should have renamed 2 more file
|
|
|
|
assert.length(existingFiles, 4);
|
|
|
|
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
|
|
|
|
//and opened a new log file
|
|
|
|
assert.length(filesOpened, 3);
|
|
|
|
|
|
|
|
//tell the watchCb we've flipped again.
|
|
|
|
watchCb({ size: 1024 }, { size: 234 });
|
|
|
|
//close the old one again.
|
|
|
|
assert.length(filesEnded, 3);
|
|
|
|
//it should have renamed the old log file and the 2 backups, with the last one being overwritten.
|
|
|
|
assert.length(filesRenamed, 5);
|
|
|
|
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log', 'tests.log.1', 'tests.log' ]);
|
|
|
|
//it should have renamed 2 more files
|
|
|
|
assert.length(existingFiles, 6);
|
|
|
|
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
|
|
|
|
//and opened a new log file
|
|
|
|
assert.length(filesOpened, 4);
|
|
|
|
}
|
2010-12-05 14:17:37 +08:00
|
|
|
},
|
|
|
|
|
2010-12-04 17:47:30 +08:00
|
|
|
'configure' : {
|
|
|
|
topic: function() {
|
|
|
|
var messages = {}, fakeFS = {
|
2011-07-17 18:49:39 +08:00
|
|
|
createWriteStream: function(file) {
|
|
|
|
return {
|
|
|
|
write: function(message) {
|
|
|
|
if (!messages.hasOwnProperty(file)) {
|
|
|
|
messages[file] = [];
|
|
|
|
}
|
|
|
|
messages[file].push(message);
|
|
|
|
}
|
|
|
|
, end: function() {}
|
|
|
|
, destroySoon: function() {}
|
|
|
|
};
|
2010-12-04 17:47:30 +08:00
|
|
|
},
|
|
|
|
readFileSync: function(file, encoding) {
|
|
|
|
return require('fs').readFileSync(file, encoding);
|
2010-12-06 15:23:43 +08:00
|
|
|
},
|
|
|
|
watchFile: function(file) {
|
|
|
|
messages.watchedFile = file;
|
|
|
|
}
|
2010-12-04 17:47:30 +08:00
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js'
|
|
|
|
, {
|
|
|
|
requires: {
|
|
|
|
'fs': fakeFS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-12-04 17:47:30 +08:00
|
|
|
return [ log4js, messages ];
|
|
|
|
},
|
|
|
|
'should load appender configuration from a json file': function(args) {
|
|
|
|
var log4js = args[0], messages = args[1];
|
|
|
|
delete messages['tmp-tests.log'];
|
|
|
|
log4js.clearAppenders();
|
|
|
|
//this config file defines one file appender (to ./tmp-tests.log)
|
|
|
|
//and sets the log level for "tests" to WARN
|
|
|
|
log4js.configure('test/log4js.json');
|
2011-07-12 11:02:48 +08:00
|
|
|
var logger = log4js.getLogger("tests");
|
2010-12-04 17:47:30 +08:00
|
|
|
logger.info('this should not be written to the file');
|
|
|
|
logger.warn('this should be written to the file');
|
|
|
|
assert.length(messages['tmp-tests.log'], 1);
|
|
|
|
assert.equal(messages['tmp-tests.log'][0], 'this should be written to the file\n');
|
|
|
|
},
|
|
|
|
'should handle logLevelFilter configuration': function(args) {
|
|
|
|
var log4js = args[0], messages = args[1];
|
|
|
|
delete messages['tmp-tests.log'];
|
|
|
|
delete messages['tmp-tests-warnings.log'];
|
|
|
|
log4js.clearAppenders();
|
|
|
|
log4js.configure('test/with-logLevelFilter.json');
|
|
|
|
var logger = log4js.getLogger("tests");
|
|
|
|
logger.info('main');
|
|
|
|
logger.error('both');
|
|
|
|
logger.warn('both');
|
|
|
|
logger.debug('main');
|
|
|
|
|
|
|
|
assert.length(messages['tmp-tests.log'], 4);
|
|
|
|
assert.length(messages['tmp-tests-warnings.log'], 2);
|
|
|
|
assert.deepEqual(messages['tmp-tests.log'], ['main\n','both\n','both\n','main\n']);
|
|
|
|
assert.deepEqual(messages['tmp-tests-warnings.log'], ['both\n','both\n']);
|
2010-12-06 15:23:43 +08:00
|
|
|
},
|
|
|
|
'should handle fileAppender with log rolling' : function(args) {
|
|
|
|
var log4js = args[0], messages = args[1];
|
|
|
|
delete messages['tmp-test.log'];
|
|
|
|
log4js.configure('test/with-log-rolling.json');
|
|
|
|
assert.equal(messages.watchedFile, 'tmp-test.log');
|
2011-01-16 10:21:37 +08:00
|
|
|
},
|
|
|
|
'should handle an object or a file name': function(args) {
|
2011-07-12 11:02:48 +08:00
|
|
|
var log4js = args[0],
|
2011-01-16 10:21:37 +08:00
|
|
|
messages = args[1],
|
|
|
|
config = {
|
|
|
|
"appenders": [
|
|
|
|
{
|
|
|
|
"type" : "file",
|
|
|
|
"filename" : "cheesy-wotsits.log",
|
|
|
|
"maxLogSize" : 1024,
|
|
|
|
"backups" : 3,
|
|
|
|
"pollInterval" : 15
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
delete messages['cheesy-wotsits.log'];
|
|
|
|
log4js.configure(config);
|
|
|
|
assert.equal(messages.watchedFile, 'cheesy-wotsits.log');
|
|
|
|
}
|
2010-12-05 07:56:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'with no appenders defined' : {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var logger
|
|
|
|
, message
|
|
|
|
, log4js = sandbox.require(
|
|
|
|
'../lib/log4js'
|
|
|
|
, {
|
|
|
|
globals: {
|
|
|
|
console: {
|
|
|
|
log: function(msg) {
|
|
|
|
message = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-12-05 07:56:09 +08:00
|
|
|
logger = log4js.getLogger("some-logger");
|
|
|
|
logger.debug("This is a test");
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
'should default to the console appender': function(message) {
|
|
|
|
assert.isTrue(/This is a test$/.test(message));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
'addAppender' : {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var log4js = require('../lib/log4js');
|
2010-12-07 06:12:43 +08:00
|
|
|
log4js.clearAppenders();
|
|
|
|
return log4js;
|
|
|
|
},
|
|
|
|
'without a category': {
|
|
|
|
'should register the function as a listener for all loggers': function (log4js) {
|
|
|
|
var appenderEvent, appender = function(evt) { appenderEvent = evt; }, logger = log4js.getLogger("tests");
|
|
|
|
log4js.addAppender(appender);
|
|
|
|
logger.debug("This is a test");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], "This is a test");
|
2010-12-07 06:12:43 +08:00
|
|
|
assert.equal(appenderEvent.categoryName, "tests");
|
|
|
|
assert.equal(appenderEvent.level.toString(), "DEBUG");
|
|
|
|
},
|
|
|
|
'should also register as an appender for loggers if an appender for that category is defined': function (log4js) {
|
|
|
|
var otherEvent, appenderEvent, cheeseLogger;
|
|
|
|
log4js.addAppender(function (evt) { appenderEvent = evt; });
|
|
|
|
log4js.addAppender(function (evt) { otherEvent = evt; }, 'cheese');
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
cheeseLogger = log4js.getLogger('cheese');
|
|
|
|
cheeseLogger.debug('This is a test');
|
|
|
|
assert.deepEqual(appenderEvent, otherEvent);
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(otherEvent.data[0], 'This is a test');
|
2010-12-07 06:12:43 +08:00
|
|
|
assert.equal(otherEvent.categoryName, 'cheese');
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
otherEvent = undefined;
|
|
|
|
appenderEvent = undefined;
|
|
|
|
log4js.getLogger('pants').debug("this should not be propagated to otherEvent");
|
|
|
|
assert.isUndefined(otherEvent);
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], "this should not be propagated to otherEvent");
|
2010-12-07 06:12:43 +08:00
|
|
|
}
|
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
'with a category': {
|
|
|
|
'should only register the function as a listener for that category': function(log4js) {
|
|
|
|
var appenderEvent, appender = function(evt) { appenderEvent = evt; }, logger = log4js.getLogger("tests");
|
|
|
|
log4js.addAppender(appender, 'tests');
|
|
|
|
logger.debug('this is a category test');
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], 'this is a category test');
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
appenderEvent = undefined;
|
|
|
|
log4js.getLogger('some other category').debug('Cheese');
|
|
|
|
assert.isUndefined(appenderEvent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'with multiple categories': {
|
|
|
|
'should register the function as a listener for all the categories': function(log4js) {
|
|
|
|
var appenderEvent, appender = function(evt) { appenderEvent = evt; }, logger = log4js.getLogger('tests');
|
|
|
|
log4js.addAppender(appender, 'tests', 'biscuits');
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
logger.debug('this is a test');
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], 'this is a test');
|
2010-12-07 06:12:43 +08:00
|
|
|
appenderEvent = undefined;
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
var otherLogger = log4js.getLogger('biscuits');
|
|
|
|
otherLogger.debug("mmm... garibaldis");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], "mmm... garibaldis");
|
2010-12-07 06:12:43 +08:00
|
|
|
|
|
|
|
appenderEvent = undefined;
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
log4js.getLogger("something else").debug("pants");
|
|
|
|
assert.isUndefined(appenderEvent);
|
|
|
|
},
|
|
|
|
'should register the function when the list of categories is an array': function(log4js) {
|
|
|
|
var appenderEvent, appender = function(evt) { appenderEvent = evt; };
|
|
|
|
log4js.addAppender(appender, ['tests', 'pants']);
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
log4js.getLogger('tests').debug('this is a test');
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], 'this is a test');
|
2010-12-07 06:12:43 +08:00
|
|
|
|
|
|
|
appenderEvent = undefined;
|
|
|
|
|
|
|
|
log4js.getLogger('pants').debug("big pants");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(appenderEvent.data[0], "big pants");
|
2010-12-07 06:12:43 +08:00
|
|
|
|
|
|
|
appenderEvent = undefined;
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-07 06:12:43 +08:00
|
|
|
log4js.getLogger("something else").debug("pants");
|
|
|
|
assert.isUndefined(appenderEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-05 07:56:09 +08:00
|
|
|
'default setup': {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var pathsChecked = [],
|
2010-12-05 07:56:09 +08:00
|
|
|
message,
|
|
|
|
logger,
|
2011-07-12 11:02:48 +08:00
|
|
|
modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
|
2010-12-05 07:56:09 +08:00
|
|
|
fakeFS = {
|
|
|
|
readFileSync: function (file, encoding) {
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(file, modulePath);
|
2010-12-05 07:56:09 +08:00
|
|
|
assert.equal(encoding, 'utf8');
|
|
|
|
return '{ "appenders" : [ { "type": "console", "layout": { "type": "messagePassThrough" }} ] }';
|
|
|
|
},
|
|
|
|
statSync: function (path) {
|
|
|
|
pathsChecked.push(path);
|
2011-07-12 11:02:48 +08:00
|
|
|
if (path === modulePath) {
|
2010-12-05 07:56:09 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw new Error("no such file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
fakeConsole = {
|
|
|
|
log : function (msg) { message = msg; },
|
|
|
|
info: this.log,
|
|
|
|
warn: this.log,
|
|
|
|
debug: this.log,
|
|
|
|
error: this.log
|
|
|
|
},
|
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js',
|
|
|
|
{
|
|
|
|
requires: {
|
|
|
|
'fs': fakeFS
|
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
'console': fakeConsole
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2010-12-05 07:56:09 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
logger = log4js.getLogger('a-test');
|
|
|
|
logger.debug("this is a test");
|
2011-01-16 10:05:13 +08:00
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
return [ pathsChecked, message, modulePath ];
|
2010-12-05 07:56:09 +08:00
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-05 07:56:09 +08:00
|
|
|
'should check current directory, require paths, and finally the module dir for log4js.json': function(args) {
|
|
|
|
var pathsChecked = args[0];
|
2011-07-12 11:02:48 +08:00
|
|
|
expectedPaths = ['log4js.json'].concat(
|
|
|
|
require.paths.map(function(item) {
|
|
|
|
return item + '/log4js.json';
|
|
|
|
}),
|
|
|
|
args[2]
|
|
|
|
);
|
|
|
|
assert.deepEqual(pathsChecked, expectedPaths);
|
2010-12-05 07:56:09 +08:00
|
|
|
},
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2010-12-05 07:56:09 +08:00
|
|
|
'should configure log4js from first log4js.json found': function(args) {
|
|
|
|
var message = args[1];
|
|
|
|
assert.equal(message, 'this is a test');
|
|
|
|
}
|
2010-12-05 11:36:35 +08:00
|
|
|
},
|
2010-12-07 05:08:29 +08:00
|
|
|
|
2010-12-06 17:26:23 +08:00
|
|
|
'logLevelFilter': {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var log4js = require('../lib/log4js'), logEvents = [], logger;
|
2010-12-06 17:26:23 +08:00
|
|
|
log4js.clearAppenders();
|
2011-01-16 10:05:13 +08:00
|
|
|
log4js.addAppender(log4js.logLevelFilter('ERROR', function(evt) { logEvents.push(evt); }), "logLevelTest");
|
|
|
|
logger = log4js.getLogger("logLevelTest");
|
2010-12-06 17:26:23 +08:00
|
|
|
logger.debug('this should not trigger an event');
|
|
|
|
logger.warn('neither should this');
|
|
|
|
logger.error('this should, though');
|
|
|
|
logger.fatal('so should this');
|
|
|
|
return logEvents;
|
|
|
|
},
|
|
|
|
'should only pass log events greater than or equal to its own level' : function(logEvents) {
|
|
|
|
assert.length(logEvents, 2);
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[0].data[0], 'this should, though');
|
|
|
|
assert.equal(logEvents[1].data[0], 'so should this');
|
2010-12-06 17:26:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-08 05:53:59 +08:00
|
|
|
'console' : {
|
|
|
|
topic: function() {
|
2011-07-12 11:02:48 +08:00
|
|
|
var fakeConsole = {}
|
|
|
|
, logEvents = []
|
|
|
|
, log4js;
|
|
|
|
|
|
|
|
['trace','debug','log','info','warn','error'].forEach(function(fn) {
|
|
|
|
fakeConsole[fn] = function() {
|
|
|
|
throw new Error("this should not be called.");
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js'
|
|
|
|
, {
|
|
|
|
globals: {
|
|
|
|
console: fakeConsole
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2010-12-08 05:53:59 +08:00
|
|
|
log4js.clearAppenders();
|
2011-07-12 11:02:48 +08:00
|
|
|
log4js.addAppender(function(evt) {
|
|
|
|
logEvents.push(evt);
|
|
|
|
});
|
|
|
|
|
|
|
|
fakeConsole.log("Some debug message someone put in a module");
|
|
|
|
fakeConsole.debug("Some debug");
|
|
|
|
fakeConsole.error("An error");
|
|
|
|
fakeConsole.info("some info");
|
|
|
|
fakeConsole.warn("a warning");
|
|
|
|
|
2011-07-13 16:12:29 +08:00
|
|
|
fakeConsole.log("cheese (%s) and biscuits (%s)", "gouda", "garibaldis");
|
|
|
|
fakeConsole.log({ lumpy: "tapioca" });
|
|
|
|
fakeConsole.log("count %d", 123);
|
|
|
|
fakeConsole.log("stringify %j", { lumpy: "tapioca" });
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
return logEvents;
|
|
|
|
},
|
|
|
|
'should replace console.log methods with log4js ones': function(logEvents) {
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[0].data[0], "Some debug message someone put in a module");
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(logEvents[0].level.toString(), "INFO");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[1].data[0], "Some debug");
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(logEvents[1].level.toString(), "DEBUG");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[2].data[0], "An error");
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(logEvents[2].level.toString(), "ERROR");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[3].data[0], "some info");
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(logEvents[3].level.toString(), "INFO");
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvents[4].data[0], "a warning");
|
2011-07-12 11:02:48 +08:00
|
|
|
assert.equal(logEvents[4].level.toString(), "WARN");
|
2010-12-08 05:53:59 +08:00
|
|
|
}
|
2011-01-16 10:05:13 +08:00
|
|
|
},
|
|
|
|
'configuration persistence' : {
|
|
|
|
'should maintain appenders between requires': function () {
|
2011-07-12 11:02:48 +08:00
|
|
|
var logEvent, firstLog4js = require('../lib/log4js'), secondLog4js;
|
2011-01-16 10:05:13 +08:00
|
|
|
firstLog4js.clearAppenders();
|
|
|
|
firstLog4js.addAppender(function(evt) { logEvent = evt; });
|
|
|
|
|
2011-07-12 11:02:48 +08:00
|
|
|
secondLog4js = require('../lib/log4js');
|
2011-01-16 10:05:13 +08:00
|
|
|
secondLog4js.getLogger().info("This should go to the appender defined in firstLog4js");
|
2011-07-12 11:02:48 +08:00
|
|
|
|
2011-07-13 16:12:29 +08:00
|
|
|
assert.equal(logEvent.data[0], "This should go to the appender defined in firstLog4js");
|
2011-01-16 10:05:13 +08:00
|
|
|
}
|
2010-12-04 17:47:30 +08:00
|
|
|
}
|
2010-12-06 15:38:20 +08:00
|
|
|
|
2010-12-04 17:47:30 +08:00
|
|
|
}).export(module);
|