2013-05-27 05:44:59 +08:00
|
|
|
"use strict";
|
2011-07-26 06:52:40 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, fs = require('fs')
|
2014-07-17 15:29:03 +08:00
|
|
|
, assert = require('assert')
|
|
|
|
, os = require('os')
|
|
|
|
, EOL = require('os').EOL || '\n';
|
2011-07-26 06:52:40 +08:00
|
|
|
|
|
|
|
function remove(filename) {
|
2013-05-27 05:44:59 +08:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(filename);
|
|
|
|
} catch (e) {
|
|
|
|
//doesn't really matter if it failed
|
|
|
|
}
|
2011-07-26 06:52:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vows.describe('log4js logLevelFilter').addBatch({
|
2013-05-27 05:44:59 +08:00
|
|
|
'appender': {
|
|
|
|
topic: function() {
|
|
|
|
var log4js = require('../lib/log4js'), logEvents = [], logger;
|
|
|
|
log4js.clearAppenders();
|
|
|
|
log4js.addAppender(
|
|
|
|
require('../lib/appenders/logLevelFilter')
|
|
|
|
.appender(
|
2014-08-15 18:06:35 +08:00
|
|
|
'ERROR',
|
2014-08-26 02:56:37 +08:00
|
|
|
undefined,
|
2013-05-27 05:44:59 +08:00
|
|
|
function(evt) { logEvents.push(evt); }
|
2014-08-15 18:06:35 +08:00
|
|
|
),
|
2013-05-27 05:44:59 +08:00
|
|
|
"logLevelTest"
|
|
|
|
);
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-05-27 05:44:59 +08:00
|
|
|
logger = log4js.getLogger("logLevelTest");
|
|
|
|
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;
|
2011-07-26 06:52:40 +08:00
|
|
|
},
|
2013-05-27 05:44:59 +08:00
|
|
|
'should only pass log events greater than or equal to its own level' : function(logEvents) {
|
|
|
|
assert.equal(logEvents.length, 2);
|
|
|
|
assert.equal(logEvents[0].data[0], 'this should, though');
|
|
|
|
assert.equal(logEvents[1].data[0], 'so should this');
|
|
|
|
}
|
|
|
|
},
|
2011-07-26 06:52:40 +08:00
|
|
|
|
2013-05-27 05:44:59 +08:00
|
|
|
'configure': {
|
|
|
|
topic: function() {
|
|
|
|
var log4js = require('../lib/log4js')
|
|
|
|
, logger;
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-05-27 05:44:59 +08:00
|
|
|
remove(__dirname + '/logLevelFilter.log');
|
|
|
|
remove(__dirname + '/logLevelFilter-warnings.log');
|
2014-08-26 02:56:37 +08:00
|
|
|
remove(__dirname + '/logLevelFilter-debugs.log');
|
2014-08-15 18:06:35 +08:00
|
|
|
|
2013-05-27 05:44:59 +08:00
|
|
|
log4js.configure('test/with-logLevelFilter.json');
|
|
|
|
logger = log4js.getLogger("tests");
|
2014-08-26 02:56:37 +08:00
|
|
|
logger.debug('debug');
|
|
|
|
logger.info('info');
|
|
|
|
logger.error('error');
|
|
|
|
logger.warn('warn');
|
|
|
|
logger.debug('debug');
|
|
|
|
logger.trace('trace');
|
2013-05-27 05:44:59 +08:00
|
|
|
//wait for the file system to catch up
|
2014-08-15 18:06:35 +08:00
|
|
|
setTimeout(this.callback, 500);
|
2013-05-27 05:44:59 +08:00
|
|
|
},
|
|
|
|
'tmp-tests.log': {
|
|
|
|
topic: function() {
|
|
|
|
fs.readFile(__dirname + '/logLevelFilter.log', 'utf8', this.callback);
|
|
|
|
},
|
2014-07-17 15:29:03 +08:00
|
|
|
'should contain all log messages': function (contents) {
|
|
|
|
var messages = contents.trim().split(EOL);
|
2014-08-26 02:56:37 +08:00
|
|
|
assert.deepEqual(messages, ['debug','info','error','warn','debug','trace']);
|
2013-05-27 05:44:59 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'tmp-tests-warnings.log': {
|
|
|
|
topic: function() {
|
|
|
|
fs.readFile(__dirname + '/logLevelFilter-warnings.log','utf8',this.callback);
|
|
|
|
},
|
|
|
|
'should contain only error and warning log messages': function(contents) {
|
2014-07-17 15:29:03 +08:00
|
|
|
var messages = contents.trim().split(EOL);
|
2014-08-26 02:56:37 +08:00
|
|
|
assert.deepEqual(messages, ['error','warn']);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'tmp-tests-debugs.log': {
|
|
|
|
topic: function() {
|
|
|
|
fs.readFile(__dirname + '/logLevelFilter-debugs.log','utf8',this.callback);
|
|
|
|
},
|
|
|
|
'should contain only trace and debug log messages': function(contents) {
|
|
|
|
var messages = contents.trim().split(EOL);
|
|
|
|
assert.deepEqual(messages, ['debug','debug','trace']);
|
2013-05-27 05:44:59 +08:00
|
|
|
}
|
2011-07-26 06:52:40 +08:00
|
|
|
}
|
2013-05-27 05:44:59 +08:00
|
|
|
}
|
2012-05-31 06:07:45 +08:00
|
|
|
}).export(module);
|