|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const fs = require('fs'); |
| 5 | +const os = require('os'); |
| 6 | + |
| 7 | +const EOL = os.EOL || '\n'; |
| 8 | + |
| 9 | +function remove(filename) { |
| 10 | + try { |
| 11 | + fs.unlinkSync(filename); |
| 12 | + } catch (e) { |
| 13 | + // doesn't really matter if it failed |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +test('log4js logLevelFilter', (batch) => { |
| 18 | + batch.test('appender', (t) => { |
| 19 | + const log4js = require('../../lib/log4js'); |
| 20 | + const logEvents = []; |
| 21 | + |
| 22 | + log4js.clearAppenders(); |
| 23 | + log4js.addAppender( |
| 24 | + require('../../lib/appenders/logLevelFilter') |
| 25 | + .appender( |
| 26 | + 'ERROR', |
| 27 | + undefined, |
| 28 | + (evt) => { |
| 29 | + logEvents.push(evt); |
| 30 | + } |
| 31 | + ), |
| 32 | + 'logLevelTest' |
| 33 | + ); |
| 34 | + |
| 35 | + const logger = log4js.getLogger('logLevelTest'); |
| 36 | + logger.debug('this should not trigger an event'); |
| 37 | + logger.warn('neither should this'); |
| 38 | + logger.error('this should, though'); |
| 39 | + logger.fatal('so should this'); |
| 40 | + |
| 41 | + t.test('should only pass log events greater than or equal to its own level', (assert) => { |
| 42 | + assert.equal(logEvents.length, 2); |
| 43 | + assert.equal(logEvents[0].data[0], 'this should, though'); |
| 44 | + assert.equal(logEvents[1].data[0], 'so should this'); |
| 45 | + assert.end(); |
| 46 | + }); |
| 47 | + t.end(); |
| 48 | + }); |
| 49 | + |
| 50 | + batch.test('configure', (t) => { |
| 51 | + const log4js = require('../../lib/log4js'); |
| 52 | + |
| 53 | + remove(`${__dirname}/logLevelFilter.log`); |
| 54 | + remove(`${__dirname}/logLevelFilter-warnings.log`); |
| 55 | + remove(`${__dirname}/logLevelFilter-debugs.log`); |
| 56 | + |
| 57 | + log4js.configure('test/tap/with-logLevelFilter.json'); |
| 58 | + const logger = log4js.getLogger('tests'); |
| 59 | + logger.debug('debug'); |
| 60 | + logger.info('info'); |
| 61 | + logger.error('error'); |
| 62 | + logger.warn('warn'); |
| 63 | + logger.debug('debug'); |
| 64 | + logger.trace('trace'); |
| 65 | + // wait for the file system to catch up |
| 66 | + setTimeout(() => { |
| 67 | + t.test('tmp-tests.log should contain all log messages', (assert) => { |
| 68 | + fs.readFile(`${__dirname}/logLevelFilter.log`, 'utf8', (err, contents) => { |
| 69 | + const messages = contents.trim().split(EOL); |
| 70 | + assert.same(messages, ['debug', 'info', 'error', 'warn', 'debug', 'trace']); |
| 71 | + assert.end(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + t.test('tmp-tests-warnings.log should contain only error and warning logs', (assert) => { |
| 75 | + fs.readFile(`${__dirname}/logLevelFilter-warnings.log`, 'utf8', (err, contents) => { |
| 76 | + const messages = contents.trim().split(EOL); |
| 77 | + assert.deepEqual(messages, ['error', 'warn']); |
| 78 | + assert.end(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + t.test('tmp-tests-debugs.log should contain only trace and debug logs', (assert) => { |
| 82 | + fs.readFile(`${__dirname}/logLevelFilter-debugs.log`, 'utf8', (err, contents) => { |
| 83 | + const messages = contents.trim().split(EOL); |
| 84 | + assert.deepEqual(messages, ['debug', 'debug', 'trace']); |
| 85 | + assert.end(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + t.end(); |
| 89 | + }, 500); |
| 90 | + }); |
| 91 | + |
| 92 | + batch.end(); |
| 93 | +}); |
0 commit comments