/
dateFileAppender-test.js
303 lines (268 loc) · 8 KB
/
dateFileAppender-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* eslint max-classes-per-file: ["error", 3] */
const { test } = require('tap');
const path = require('path');
const fs = require('fs');
const EOL = require('os').EOL || '\n';
const format = require('date-format');
const sandbox = require('@log4js-node/sandboxed-module');
const log4js = require('../../lib/log4js');
const osDelay = process.platform === 'win32' ? 400 : 200;
function removeFile(filename) {
try {
fs.unlinkSync(path.join(__dirname, filename));
} catch (e) {
// doesn't matter
}
}
test('../../lib/appenders/dateFile', (batch) => {
batch.test('with default settings', (t) => {
const testFile = path.join(__dirname, 'date-appender-default.log');
log4js.configure({
appenders: { date: { type: 'dateFile', filename: testFile } },
categories: { default: { appenders: ['date'], level: 'DEBUG' } },
});
const logger = log4js.getLogger('default-settings');
logger.info('This should be in the file.');
t.teardown(() => {
removeFile('date-appender-default.log');
});
setTimeout(() => {
fs.readFile(testFile, 'utf8', (err, contents) => {
t.match(contents, 'This should be in the file');
t.match(
contents,
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();
});
}, osDelay);
});
batch.test('configure with dateFileAppender', (t) => {
log4js.configure({
appenders: {
date: {
type: 'dateFile',
filename: 'test/tap/date-file-test.log',
pattern: '-yyyy-MM-dd',
layout: { type: 'messagePassThrough' },
},
},
categories: { default: { appenders: ['date'], level: 'WARN' } },
});
const logger = log4js.getLogger('tests');
logger.info('this should not be written to the file');
logger.warn('this should be written to the file');
log4js.shutdown(() => {
fs.readFile(
path.join(__dirname, 'date-file-test.log'),
'utf8',
(err, contents) => {
t.match(contents, `this should be written to the file${EOL}`);
t.equal(
contents.indexOf('this should not be written to the file'),
-1
);
t.end();
}
);
});
t.teardown(() => {
removeFile('date-file-test.log');
});
});
batch.test('configure with options.alwaysIncludePattern', (t) => {
const options = {
appenders: {
date: {
category: 'tests',
type: 'dateFile',
filename: 'test/tap/date-file-test',
pattern: 'yyyy-MM-dd.log',
alwaysIncludePattern: true,
layout: {
type: 'messagePassThrough',
},
},
},
categories: { default: { appenders: ['date'], level: 'debug' } },
};
const thisTime = format.asString(
options.appenders.date.pattern,
new Date()
);
const testFile = `date-file-test.${thisTime}`;
const existingFile = path.join(__dirname, testFile);
fs.writeFileSync(existingFile, `this is existing data${EOL}`, 'utf8');
log4js.configure(options);
const logger = log4js.getLogger('tests');
logger.warn('this should be written to the file with the appended date');
t.teardown(() => {
removeFile(testFile);
});
// wait for filesystem to catch up
log4js.shutdown(() => {
fs.readFile(existingFile, 'utf8', (err, contents) => {
t.match(
contents,
'this is existing data',
'should not overwrite the file on open (issue #132)'
);
t.match(
contents,
'this should be written to the file with the appended date'
);
t.end();
});
});
});
batch.test('should flush logs on shutdown', (t) => {
const testFile = path.join(__dirname, 'date-appender-flush.log');
log4js.configure({
appenders: { test: { type: 'dateFile', filename: testFile } },
categories: { default: { appenders: ['test'], level: 'trace' } },
});
const logger = log4js.getLogger('default-settings');
logger.info('1');
logger.info('2');
logger.info('3');
t.teardown(() => {
removeFile('date-appender-flush.log');
});
log4js.shutdown(() => {
fs.readFile(testFile, 'utf8', (err, fileContents) => {
// 3 lines of output, plus the trailing newline.
t.equal(fileContents.split(EOL).length, 4);
t.match(
fileContents,
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();
});
});
});
batch.test('should map maxLogSize to maxSize', (t) => {
const fakeStreamroller = {};
class DateRollingFileStream {
constructor(filename, pattern, options) {
fakeStreamroller.filename = filename;
fakeStreamroller.pattern = pattern;
fakeStreamroller.options = options;
}
on() {} // eslint-disable-line class-methods-use-this
}
fakeStreamroller.DateRollingFileStream = DateRollingFileStream;
const dateFileAppenderModule = sandbox.require(
'../../lib/appenders/dateFile',
{
requires: { streamroller: fakeStreamroller },
}
);
dateFileAppenderModule.configure(
{
filename: 'cheese.log',
pattern: 'yyyy',
maxLogSize: 100,
},
{ basicLayout: () => {} }
);
t.equal(fakeStreamroller.options.maxSize, 100);
t.end();
});
batch.test('handling of writer.writable', (t) => {
const output = [];
let writable = true;
const DateRollingFileStream = class {
write(loggingEvent) {
output.push(loggingEvent);
this.written = true;
return true;
}
// eslint-disable-next-line class-methods-use-this
on() {}
// eslint-disable-next-line class-methods-use-this
get writable() {
return writable;
}
};
const dateFileAppender = sandbox.require('../../lib/appenders/dateFile', {
requires: {
streamroller: {
DateRollingFileStream,
},
},
});
const appender = dateFileAppender.configure(
{ filename: 'test1.log', maxLogSize: 100 },
{
basicLayout(loggingEvent) {
return loggingEvent.data;
},
}
);
t.test('should log when writer.writable=true', (assert) => {
writable = true;
appender({ data: 'something to log' });
assert.ok(output.length, 1);
assert.match(output[output.length - 1], 'something to log');
assert.end();
});
t.test('should not log when writer.writable=false', (assert) => {
writable = false;
appender({ data: 'this should not be logged' });
assert.ok(output.length, 1);
assert.notMatch(output[output.length - 1], 'this should not be logged');
assert.end();
});
t.end();
});
batch.test('when underlying stream errors', (t) => {
let consoleArgs;
let errorHandler;
const DateRollingFileStream = class {
end() {
this.ended = true;
}
on(evt, cb) {
if (evt === 'error') {
this.errored = true;
errorHandler = cb;
}
}
write() {
this.written = true;
return true;
}
};
const dateFileAppender = sandbox.require('../../lib/appenders/dateFile', {
globals: {
console: {
error(...args) {
consoleArgs = args;
},
},
},
requires: {
streamroller: {
DateRollingFileStream,
},
},
});
dateFileAppender.configure(
{ filename: 'test1.log', maxLogSize: 100 },
{ basicLayout() {} }
);
errorHandler({ error: 'aargh' });
t.test('should log the error to console.error', (assert) => {
assert.ok(consoleArgs);
assert.equal(
consoleArgs[0],
'log4js.dateFileAppender - Writing to file %s, error happened '
);
assert.equal(consoleArgs[1], 'test1.log');
assert.equal(consoleArgs[2].error, 'aargh');
assert.end();
});
t.end();
});
batch.end();
});