|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const log4js = require('../../lib/log4js'); |
| 5 | +const sandbox = require('sandboxed-module'); |
| 6 | + |
| 7 | +function setupLogging(category, options) { |
| 8 | + const msgs = []; |
| 9 | + |
| 10 | + const redisHost = { |
| 11 | + host: options.host, |
| 12 | + port: options.port, |
| 13 | + pass: options.pass, |
| 14 | + channel: options.channel |
| 15 | + }; |
| 16 | + |
| 17 | + const fakeRedis = (function (port, post, optionR) { |
| 18 | + return { |
| 19 | + createClient: function(port, post, optionR) { |
| 20 | + return { |
| 21 | + on: function(event, callback) {}, |
| 22 | + publish: function(channel, message, callback) { |
| 23 | + msgs.push({ channel: channel, message: message }); |
| 24 | + callback(false, { status: 'sent' }); |
| 25 | + }), |
| 26 | + }; |
| 27 | + } |
| 28 | + }; |
| 29 | + }); |
| 30 | + |
| 31 | + const fakeLayouts = { |
| 32 | + layout: function (type, config) { |
| 33 | + this.type = type; |
| 34 | + this.config = config; |
| 35 | + return log4js.layouts.messagePassThroughLayout; |
| 36 | + }, |
| 37 | + basicLayout: log4js.layouts.basicLayout, |
| 38 | + coloredLayout: log4js.layouts.coloredLayout, |
| 39 | + messagePassThroughLayout: log4js.layouts.messagePassThroughLayout |
| 40 | + }; |
| 41 | + |
| 42 | + const fakeConsole = { |
| 43 | + errors: [], |
| 44 | + logs: [], |
| 45 | + error: function (msg, value) { |
| 46 | + this.errors.push({ msg: msg, value: value }); |
| 47 | + }, |
| 48 | + log: function (msg, value) { |
| 49 | + this.logs.push({ msg: msg, value: value }); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + const redisModule = sandbox.require('../../lib/appenders/redis', { |
| 54 | + requires: { |
| 55 | + 'redis': fakeRedis, |
| 56 | + '../layouts': fakeLayouts |
| 57 | + }, |
| 58 | + globals: { |
| 59 | + console: fakeConsole |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + log4js.addAppender(redisModule.configure(options), category); |
| 64 | + |
| 65 | + return { |
| 66 | + logger: log4js.getLogger(category), |
| 67 | + redis: fakeRedis, |
| 68 | + layouts: fakeLayouts, |
| 69 | + console: fakeConsole, |
| 70 | + messages: msgs, |
| 71 | + redishost: redisHost |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function checkMessages(assert, result) { |
| 76 | + for (let i = 0; i < result.messages.length; ++i) { |
| 77 | + assert.equal(result.messages[i].channel, 'log'); |
| 78 | + assert.ok(new RegExp(`.+Log event #${i + 1}`).test(result.messages[i].text)); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +log4js.clearAppenders(); |
| 83 | + |
| 84 | +test('log4js redisAppender', (batch) => { |
| 85 | + batch.test('redis setup', (t) => { |
| 86 | + const result = setupLogging('redis setup', { |
| 87 | + host: '127.0.0.1', |
| 88 | + port: 6739, |
| 89 | + pass: '', |
| 90 | + channel: 'log' |
| 91 | + }); |
| 92 | + |
| 93 | + t.test('redis credentials should match', (assert) => { |
| 94 | + assert.equal(result.redishost.host, '127.0.0.1'); |
| 95 | + assert.equal(result.redishost.port, 6739); |
| 96 | + assert.equal(result.redishost.pass, ''); |
| 97 | + assert.equal(result.redishost.channel, 'log'); |
| 98 | + assert.end(); |
| 99 | + }); |
| 100 | + t.end(); |
| 101 | + }); |
| 102 | + |
| 103 | + batch.test('basic usage', (t) => { |
| 104 | + const setup = setupLogging('basic usage', { |
| 105 | + host: '127.0.0.1', |
| 106 | + port: 6739, |
| 107 | + pass: '', |
| 108 | + channel: 'log', |
| 109 | + }); |
| 110 | + |
| 111 | + setup.logger.info('Log event #1'); |
| 112 | + |
| 113 | + t.equal(setup.messages.length, 1, 'should be one message only'); |
| 114 | + checkMessages(t, setup); |
| 115 | + t.end(); |
| 116 | + }); |
| 117 | + |
| 118 | + batch.test('config with layout', (t) => { |
| 119 | + const result = setupLogging('config with layout', { |
| 120 | + layout: { |
| 121 | + type: 'redis' |
| 122 | + } |
| 123 | + }); |
| 124 | + t.equal(result.layouts.type, 'redis', 'should configure layout'); |
| 125 | + t.end(); |
| 126 | + }); |
| 127 | + |
| 128 | + batch.test('separate notification for each event', (t) => { |
| 129 | + const setup = setupLogging('separate notification for each event', { |
| 130 | + host: '127.0.0.1', |
| 131 | + port: 6739, |
| 132 | + pass: '', |
| 133 | + channel: 'log', |
| 134 | + }); |
| 135 | + setTimeout(() => { |
| 136 | + setup.logger.info('Log event #1'); |
| 137 | + }, 0); |
| 138 | + setTimeout(() => { |
| 139 | + setup.logger.info('Log event #2'); |
| 140 | + }, 500); |
| 141 | + setTimeout(() => { |
| 142 | + setup.logger.info('Log event #3'); |
| 143 | + }, 1100); |
| 144 | + setTimeout(() => { |
| 145 | + t.equal(setup.messages.length, 3, 'should be three messages'); |
| 146 | + checkMessages(t, setup); |
| 147 | + t.end(); |
| 148 | + }, 3000); |
| 149 | + }); |
| 150 | + |
| 151 | + batch.end(); |
| 152 | +}); |
0 commit comments