|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const test = require('tap').test;
|
4 |
| -const log4js = require('../../lib/log4js'); |
| 4 | +// const log4js = require('../../lib/log4js'); |
5 | 5 | const sandbox = require('sandboxed-module');
|
6 | 6 |
|
7 | 7 | function setupLogging(category, options) {
|
8 |
| - const msgs = []; |
9 |
| - |
10 |
| - const redisCredentials = { |
11 |
| - type: options.type, |
12 |
| - host: options.host, |
13 |
| - port: options.port, |
14 |
| - pass: options.pass, |
15 |
| - channel: options.channel, |
16 |
| - layout: options.layout |
17 |
| - }; |
18 |
| - |
19 | 8 | const fakeRedis = {
|
| 9 | + msgs: [], |
20 | 10 | createClient: function (port, host, optionR) {
|
21 | 11 | this.port = port;
|
22 | 12 | this.host = host;
|
23 |
| - this.optionR = {}; |
24 |
| - this.optionR.auth_pass = optionR.pass; |
| 13 | + this.optionR = optionR; |
25 | 14 |
|
26 | 15 | return {
|
27 | 16 | on: function (event, callback) {
|
28 |
| - callback('throw redis error #1'); |
| 17 | + fakeRedis.errorCb = callback; |
29 | 18 | },
|
30 | 19 | publish: function (channel, message, callback) {
|
31 |
| - msgs.push(message); |
32 |
| - callback(null, {status: 'sent'}); |
| 20 | + fakeRedis.msgs.push({ channel: channel, message: message }); |
| 21 | + fakeRedis.publishCb = callback; |
33 | 22 | }
|
34 | 23 | };
|
35 | 24 | }
|
36 | 25 | };
|
37 | 26 |
|
38 |
| - const fakeLayouts = { |
39 |
| - layout: function (type, config) { |
40 |
| - this.type = type; |
41 |
| - this.config = config; |
42 |
| - return log4js.layouts.messagePassThroughLayout; |
43 |
| - }, |
44 |
| - basicLayout: log4js.layouts.basicLayout, |
45 |
| - coloredLayout: log4js.layouts.coloredLayout, |
46 |
| - messagePassThroughLayout: log4js.layouts.messagePassThroughLayout |
47 |
| - }; |
48 |
| - |
49 |
| - const fakeUtil = { |
50 |
| - inspect: function (item) { |
51 |
| - return JSON.stringify(item); |
52 |
| - } |
53 |
| - }; |
54 |
| - |
55 | 27 | const fakeConsole = {
|
56 | 28 | errors: [],
|
57 |
| - logs: [], |
58 |
| - error: function (msg, value) { |
59 |
| - this.errors.push({ msg: msg, value: value }); |
60 |
| - }, |
61 |
| - log: function (msg, value) { |
62 |
| - this.logs.push({ msg: msg, value: value }); |
| 29 | + error: function (msg) { |
| 30 | + this.errors.push(msg); |
63 | 31 | }
|
64 | 32 | };
|
65 | 33 |
|
66 |
| - const redisModule = sandbox.require('../../lib/appenders/redis', { |
| 34 | + const log4js = sandbox.require('../../lib/log4js', { |
67 | 35 | requires: {
|
68 |
| - 'redis': fakeRedis, |
69 |
| - '../layouts': fakeLayouts, |
70 |
| - 'util': fakeUtil |
| 36 | + redis: fakeRedis |
71 | 37 | },
|
72 | 38 | globals: {
|
73 | 39 | console: fakeConsole
|
74 | 40 | }
|
75 | 41 | });
|
76 |
| - |
77 |
| - log4js.addAppender(redisModule.configure(options), category); |
| 42 | + log4js.configure({ |
| 43 | + appenders: { redis: options }, |
| 44 | + categories: { default: { appenders: ['redis'], level: 'trace' } } |
| 45 | + }); |
78 | 46 |
|
79 | 47 | return {
|
80 | 48 | logger: log4js.getLogger(category),
|
81 |
| - redis: fakeRedis, |
82 |
| - layouts: fakeLayouts, |
83 |
| - console: fakeConsole, |
84 |
| - messages: msgs, |
85 |
| - credentials: redisCredentials |
| 49 | + fakeRedis: fakeRedis, |
| 50 | + fakeConsole: fakeConsole |
86 | 51 | };
|
87 | 52 | }
|
88 | 53 |
|
89 |
| -function checkMessages(assert, result) { |
90 |
| - for (let i = 0; i < result.messages.length; i++) { |
91 |
| - assert.ok(new RegExp(`Log event #${i + 1}`).test(result.messages[i])); |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 |
| -log4js.clearAppenders(); |
96 |
| - |
97 | 54 | test('log4js redisAppender', (batch) => {
|
98 | 55 | batch.test('redis setup', (t) => {
|
99 | 56 | const result = setupLogging('redis setup', {
|
100 |
| - host: '127.0.0.1', |
101 |
| - port: 6739, |
| 57 | + host: '123.123.123.123', |
| 58 | + port: 1234, |
102 | 59 | pass: '123456',
|
103 | 60 | channel: 'log',
|
104 | 61 | type: 'redis',
|
105 | 62 | layout: {
|
106 | 63 | type: 'pattern',
|
107 |
| - pattern: '%d{yyyy-MM-dd hh:mm:ss:SSS}#%p#%m' |
| 64 | + pattern: 'cheese %m' |
108 | 65 | }
|
109 | 66 | });
|
| 67 | + |
| 68 | + result.logger.info('Log event #1'); |
| 69 | + result.fakeRedis.publishCb(); |
| 70 | + |
110 | 71 | t.test('redis credentials should match', (assert) => {
|
111 |
| - assert.equal(result.credentials.host, '127.0.0.1'); |
112 |
| - assert.equal(result.credentials.port, 6739); |
113 |
| - assert.equal(result.credentials.pass, '123456'); |
114 |
| - assert.equal(result.credentials.channel, 'log'); |
115 |
| - assert.equal(result.credentials.type, 'redis'); |
116 |
| - assert.equal(result.credentials.layout.type, 'pattern'); |
117 |
| - assert.equal(result.credentials.layout.pattern, '%d{yyyy-MM-dd hh:mm:ss:SSS}#%p#%m'); |
| 72 | + assert.equal(result.fakeRedis.host, '123.123.123.123'); |
| 73 | + assert.equal(result.fakeRedis.port, 1234); |
| 74 | + assert.equal(result.fakeRedis.optionR.auth_pass, '123456'); |
| 75 | + assert.equal(result.fakeRedis.msgs.length, 1, 'should be one message only'); |
| 76 | + assert.equal(result.fakeRedis.msgs[0].channel, 'log'); |
| 77 | + assert.equal(result.fakeRedis.msgs[0].message, 'cheese Log event #1'); |
118 | 78 | assert.end();
|
119 | 79 | });
|
120 | 80 |
|
121 | 81 | t.end();
|
122 | 82 | });
|
123 | 83 |
|
124 |
| - batch.test('basic usage', (t) => { |
125 |
| - const setup = setupLogging('basic usage', { |
126 |
| - host: '127.0.0.1', |
127 |
| - port: 6739, |
128 |
| - pass: '', |
129 |
| - channel: 'log', |
| 84 | + batch.test('default values', (t) => { |
| 85 | + const setup = setupLogging('defaults', { |
130 | 86 | type: 'redis',
|
131 |
| - layout: { |
132 |
| - type: 'pattern', |
133 |
| - pattern: '%d{yyyy-MM-dd hh:mm:ss:SSS}#%p#%m' |
134 |
| - } |
| 87 | + channel: 'thing' |
135 | 88 | });
|
136 | 89 |
|
137 |
| - setup.logger.info('Log event #1'); |
138 |
| - |
139 |
| - t.equal(setup.messages.length, 1, 'should be one message only'); |
140 |
| - checkMessages(t, setup); |
141 |
| - t.end(); |
142 |
| - }); |
| 90 | + setup.logger.info('just testing'); |
| 91 | + setup.fakeRedis.publishCb(); |
143 | 92 |
|
| 93 | + t.test('should use localhost', (assert) => { |
| 94 | + assert.equal(setup.fakeRedis.host, '127.0.0.1'); |
| 95 | + assert.equal(setup.fakeRedis.port, 6379); |
| 96 | + assert.same(setup.fakeRedis.optionR, {}); |
| 97 | + assert.end(); |
| 98 | + }); |
144 | 99 |
|
145 |
| - batch.test('config with layout', (t) => { |
146 |
| - const result = setupLogging('config with layout', { |
147 |
| - layout: { |
148 |
| - type: 'redis' |
149 |
| - } |
| 100 | + t.test('should use message pass through layout', (assert) => { |
| 101 | + assert.equal(setup.fakeRedis.msgs.length, 1); |
| 102 | + assert.equal(setup.fakeRedis.msgs[0].channel, 'thing'); |
| 103 | + assert.equal(setup.fakeRedis.msgs[0].message, 'just testing'); |
| 104 | + assert.end(); |
150 | 105 | });
|
151 |
| - t.equal(result.layouts.type, 'redis', 'should configure layout'); |
| 106 | + |
152 | 107 | t.end();
|
153 | 108 | });
|
154 | 109 |
|
155 |
| - batch.test('separate notification for each event', (t) => { |
156 |
| - const setup = setupLogging('separate notification for each event', { |
157 |
| - host: '127.0.0.1', |
158 |
| - port: 6739, |
159 |
| - pass: '', |
160 |
| - channel: 'log', |
161 |
| - type: 'redis', |
162 |
| - layout: { |
163 |
| - type: 'pattern', |
164 |
| - pattern: '%d{yyyy-MM-dd hh:mm:ss:SSS}#%p#%m' |
165 |
| - } |
| 110 | + batch.test('redis errors', (t) => { |
| 111 | + const setup = setupLogging('errors', { type: 'redis', channel: 'testing' }); |
| 112 | + |
| 113 | + setup.fakeRedis.errorCb('oh no, error on connect'); |
| 114 | + setup.logger.info('something something'); |
| 115 | + setup.fakeRedis.publishCb('oh no, error on publish'); |
| 116 | + |
| 117 | + t.test('should go to the console', (assert) => { |
| 118 | + assert.equal(setup.fakeConsole.errors.length, 2); |
| 119 | + assert.equal(setup.fakeConsole.errors[0], 'log4js.redisAppender - 127.0.0.1:6379 Error: \'oh no, error on connect\''); |
| 120 | + assert.equal(setup.fakeConsole.errors[1], 'log4js.redisAppender - 127.0.0.1:6379 Error: \'oh no, error on publish\''); |
| 121 | + assert.end(); |
166 | 122 | });
|
167 |
| - setTimeout(() => { |
168 |
| - setup.logger.info('Log event #1'); |
169 |
| - }, 0); |
170 |
| - setTimeout(() => { |
171 |
| - setup.logger.info('Log event #2'); |
172 |
| - }, 500); |
173 |
| - setTimeout(() => { |
174 |
| - setup.logger.info('Log event #3'); |
175 |
| - }, 1100); |
176 |
| - setTimeout(() => { |
177 |
| - t.equal(setup.messages.length, 3, 'should be three messages'); |
178 |
| - checkMessages(t, setup); |
179 |
| - t.end(); |
180 |
| - }, 3000); |
| 123 | + t.end(); |
181 | 124 | });
|
182 | 125 |
|
183 | 126 | batch.end
36B9
();
|
|
0 commit comments