proven that category filter no longer needed

This commit is contained in:
Gareth Jones
2013-08-23 08:42:51 +10:00
parent 6c09a6fb71
commit 9afbbb580e
+83 -70
View File
@@ -1,83 +1,96 @@
'use strict'; 'use strict';
var vows = require('vows') var async = require('async')
, should = require('should')
, fs = require('fs') , fs = require('fs')
, path = require('path')
, assert = require('assert'); , assert = require('assert');
function remove(filename) { function remove() {
try { var files = Array.prototype.slice.call(arguments);
fs.unlinkSync(filename); return function(done) {
} catch (e) { async.forEach(
//doesn't really matter if it failed files.map(function(file) { return path.join(__dirname, file); }),
} fs.unlink.bind(fs),
function() { done(); }
);
};
} }
vows.describe('log4js categoryFilter').addBatch({ describe('log4js', function() {
'appender': {
topic: function() { before(
remove(
'test-category-filter-web.log',
'test-category-filter-all.log'
)
);
after(
remove(
'test-category-filter-web.log',
'test-category-filter-all.log'
)
);
describe('category filtering', function() {
before(function() {
var log4js = require('../lib/log4js')
, webLogger = log4js.getLogger("web")
, appLogger = log4js.getLogger("app");
var log4js = require('../lib/log4js'), logEvents = [], webLogger, appLogger; log4js.configure({
log4js.clearAppenders(); appenders: {
var appender = require('../lib/appenders/categoryFilter') rest: {
.appender( type: "file",
['app'], layout: { type: "messagePassThrough" },
function(evt) { logEvents.push(evt); } filename: path.join(__dirname, "test-category-filter-all.log")
); },
log4js.addAppender(appender, ["app","web"]); web: {
type: "file",
webLogger = log4js.getLogger("web"); layout: { type: "messagePassThrough"},
appLogger = log4js.getLogger("app"); filename: path.join(__dirname, "test-category-filter-web.log")
}
},
categories: {
"default": { level: "debug", appenders: [ "rest" ] },
web: { level: "debug", appenders: [ "web" ] }
}
});
webLogger.debug('This should get logged'); webLogger.debug('This should get logged');
appLogger.debug('This should not'); appLogger.debug('This should not');
webLogger.debug('Hello again'); webLogger.debug('Hello again');
log4js.getLogger('db').debug('This shouldn\'t be included by the appender anyway'); log4js.getLogger('db').debug('This shouldn\'t be included by the appender anyway');
});
return logEvents;
}, it('should only pass matching category', function(done) {
'should only pass matching category' : function(logEvents) { setTimeout(function() {
assert.equal(logEvents.length, 2); fs.readFile(
assert.equal(logEvents[0].data[0], 'This should get logged'); path.join(__dirname, 'test-category-filter-web.log'),
assert.equal(logEvents[1].data[0], 'Hello again'); 'utf8',
} function(err, contents) {
}, var lines = contents.trim().split('\n');
lines.should.eql(["This should get logged", "Hello again"]);
'configure': { done(err);
topic: function() { }
var log4js = require('../lib/log4js') );
, logger, weblogger; }, 50);
});
remove(__dirname + '/categoryFilter-web.log');
remove(__dirname + '/categoryFilter-noweb.log'); it('should send everything else to default appender', function(done) {
setTimeout(function() {
log4js.configure('test/with-categoryFilter.json'); fs.readFile(
logger = log4js.getLogger("app"); path.join(__dirname, 'test-category-filter-all.log'),
weblogger = log4js.getLogger("web"); 'utf8',
function(err, contents) {
logger.info('Loading app'); var lines = contents.trim().split('\n');
logger.debug('Initialising indexes'); lines.should.eql(["This should not", "This shouldn't be included by the appender anyway"]);
weblogger.info('00:00:00 GET / 200'); done(err);
weblogger.warn('00:00:00 GET / 500'); }
//wait for the file system to catch up );
setTimeout(this.callback, 100); }, 50);
}, });
'tmp-tests.log': {
topic: function() { });
fs.readFile(__dirname + '/categoryFilter-noweb.log', 'utf8', this.callback); });
},
'should contain all log messages': function(contents) {
var messages = contents.trim().split('\n');
assert.deepEqual(messages, ['Loading app','Initialising indexes']);
}
},
'tmp-tests-web.log': {
topic: function() {
fs.readFile(__dirname + '/categoryFilter-web.log','utf8',this.callback);
},
'should contain only error and warning log messages': function(contents) {
var messages = contents.trim().split('\n');
assert.deepEqual(messages, ['00:00:00 GET / 200','00:00:00 GET / 500']);
}
}
}
}).export(module);