log4js-node/test/vows/logLevelFilter-test.js
e-cloud 3e4f3a66f3 style: format test code
a error occur in `loggin-test.js` - 'invalid configuration'.

It may reveal some internal defects of this library.

Another problem occurs in `configureNoLevels-test.js`. I mark a
**todo** there.
2016-12-12 22:23:39 +08:00

100 lines
3.0 KiB
JavaScript

'use strict';
const vows = require('vows');
const fs = require('fs');
const assert = require('assert');
const os = require('os');
const EOL = os.EOL || '\n';
function remove(filename) {
try {
fs.unlinkSync(filename);
} catch (e) {
// doesn't really matter if it failed
}
}
vows.describe('log4js logLevelFilter').addBatch({
appender: {
topic: function () {
const log4js = require('../../lib/log4js');
const logEvents = [];
log4js.clearAppenders();
log4js.addAppender(
require('../../lib/appenders/logLevelFilter')
.appender(
'ERROR',
undefined,
(evt) => {
logEvents.push(evt);
}
),
'logLevelTest'
);
const 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;
},
'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');
}
},
configure: {
topic: function () {
const log4js = require('../../lib/log4js');
remove(`${__dirname}/logLevelFilter.log`);
remove(`${__dirname}/logLevelFilter-warnings.log`);
remove(`${__dirname}/logLevelFilter-debugs.log`);
log4js.configure('test/vows/with-logLevelFilter.json');
const logger = log4js.getLogger('tests');
logger.debug('debug');
logger.info('info');
logger.error('error');
logger.warn('warn');
logger.debug('debug');
logger.trace('trace');
// wait for the file system to catch up
setTimeout(this.callback, 500);
},
'tmp-tests.log': {
topic: function () {
fs.readFile(`${__dirname}/logLevelFilter.log`, 'utf8', this.callback);
},
'should contain all log messages': function (contents) {
const messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['debug', 'info', 'error', 'warn', 'debug', 'trace']);
}
},
'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) {
const messages = contents.trim().split(EOL);
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) {
const messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['debug', 'debug', 'trace']);
}
}
}
}).export(module);