/
fileSyncAppender-test.js
285 lines (254 loc) · 7.99 KB
/
fileSyncAppender-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
const { test } = require("tap");
const fs = require("fs");
const path = require("path");
const EOL = require("os").EOL || "\n";
const log4js = require("../../lib/log4js");
function remove(filename) {
try {
fs.unlinkSync(filename);
} catch (e) {
// doesn't really matter if it failed
}
}
test("log4js fileSyncAppender", batch => {
batch.test("with default fileSyncAppender settings", t => {
const testFile = path.join(__dirname, "/fa-default-sync-test.log");
const logger = log4js.getLogger("default-settings");
remove(testFile);
t.tearDown(() => {
remove(testFile);
});
log4js.configure({
appenders: { sync: { type: "fileSync", filename: testFile } },
categories: { default: { appenders: ["sync"], level: "debug" } }
});
logger.info("This should be in the file.");
fs.readFile(testFile, "utf8", (err, fileContents) => {
t.include(fileContents, `This should be in the file.${EOL}`);
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("with a max file size and no backups", t => {
const testFile = path.join(__dirname, "/fa-maxFileSize-sync-test.log");
const logger = log4js.getLogger("max-file-size");
remove(testFile);
t.tearDown(() => {
remove(testFile);
});
// log file of 100 bytes maximum, no backups
log4js.configure({
appenders: {
sync: {
type: "fileSync",
filename: testFile,
maxLogSize: 100,
backups: 0
}
},
categories: { default: { appenders: ["sync"], level: "debug" } }
});
logger.info("This is the first log message.");
logger.info("This is an intermediate log message.");
logger.info("This is the second log message.");
t.test("log file should only contain the second message", assert => {
fs.readFile(testFile, "utf8", (err, fileContents) => {
assert.include(fileContents, `This is the second log message.${EOL}`);
assert.equal(
fileContents.indexOf("This is the first log message."),
-1
);
assert.end();
});
});
t.test("there should be one test files", assert => {
fs.readdir(__dirname, (err, files) => {
const logFiles = files.filter(file =>
file.includes("fa-maxFileSize-sync-test.log")
);
assert.equal(logFiles.length, 1);
assert.end();
});
});
t.end();
});
batch.test("with a max file size in unit mode and no backups", t => {
const testFile = path.join(__dirname, "/fa-maxFileSize-unit-sync-test.log");
const logger = log4js.getLogger("max-file-size-unit");
remove(testFile);
remove(`${testFile}.1`);
t.tearDown(() => {
remove(testFile);
remove(`${testFile}.1`);
});
// log file of 100 bytes maximum, no backups
log4js.configure({
appenders: {
sync: {
type: "fileSync",
filename: testFile,
maxLogSize: "1K",
backups: 0,
layout: { type: "messagePassThrough" }
}
},
categories: { default: { appenders: ["sync"], level: "debug" } }
});
const maxLine = 22; // 1024 max file size / 47 bytes per line
for (let i = 0; i < maxLine; i++) {
logger.info("These are the log messages for the first file."); // 46 bytes per line + '\n'
}
logger.info("This is the second log message.");
t.test("log file should only contain the second message", assert => {
fs.readFile(testFile, "utf8", (err, fileContents) => {
assert.match(fileContents, `This is the second log message.${EOL}`);
assert.notMatch(fileContents, "These are the log messages for the first file.");
assert.end();
});
});
t.test("there should be one test file", assert => {
fs.readdir(__dirname, (err, files) => {
const logFiles = files.filter(file =>
file.includes("fa-maxFileSize-unit-sync-test.log")
);
assert.equal(logFiles.length, 1);
assert.end();
});
});
t.end();
});
batch.test("with a max file size and 2 backups", t => {
const testFile = path.join(
__dirname,
"/fa-maxFileSize-with-backups-sync-test.log"
);
const logger = log4js.getLogger("max-file-size-backups");
remove(testFile);
remove(`${testFile}.1`);
remove(`${testFile}.2`);
t.tearDown(() => {
remove(testFile);
remove(`${testFile}.1`);
remove(`${testFile}.2`);
});
// log file of 50 bytes maximum, 2 backups
log4js.configure({
appenders: {
sync: {
type: "fileSync",
filename: testFile,
maxLogSize: 50,
backups: 2
}
},
categories: { default: { appenders: ["sync"], level: "debug" } }
});
logger.info("This is the first log message.");
logger.info("This is the second log message.");
logger.info("This is the third log message.");
logger.info("This is the fourth log message.");
t.test("the log files", assert => {
assert.plan(5);
fs.readdir(__dirname, (err, files) => {
const logFiles = files.filter(file =>
file.includes("fa-maxFileSize-with-backups-sync-test.log")
);
assert.equal(logFiles.length, 3, "should be 3 files");
assert.same(
logFiles,
[
"fa-maxFileSize-with-backups-sync-test.log",
"fa-maxFileSize-with-backups-sync-test.log.1",
"fa-maxFileSize-with-backups-sync-test.log.2"
],
"should be named in sequence"
);
fs.readFile(
path.join(__dirname, logFiles[0]),
"utf8",
(e, contents) => {
assert.include(contents, "This is the fourth log message.");
}
);
fs.readFile(
path.join(__dirname, logFiles[1]),
"utf8",
(e, contents) => {
assert.include(contents, "This is the third log message.");
}
);
fs.readFile(
path.join(__dirname, logFiles[2]),
"utf8",
(e, contents) => {
assert.include(contents, "This is the second log message.");
}
);
});
});
t.end();
});
batch.test("configure with fileSyncAppender", t => {
const testFile = "tmp-sync-tests.log";
remove(testFile);
t.tearDown(() => {
remove(testFile);
});
// this config defines one file appender (to ./tmp-sync-tests.log)
// and sets the log level for "tests" to WARN
log4js.configure({
appenders: {
sync: {
type: "fileSync",
filename: testFile,
layout: { type: "messagePassThrough" }
}
},
categories: {
default: { appenders: ["sync"], level: "debug" },
tests: { appenders: ["sync"], 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");
fs.readFile(testFile, "utf8", (err, contents) => {
t.include(contents, `this should be written to the file${EOL}`);
t.equal(contents.indexOf("this should not be written to the file"), -1);
t.end();
});
});
batch.test("test options", t => {
const testFile = "tmp-options-tests.log";
remove(testFile);
t.tearDown(() => {
remove(testFile);
});
// using non-standard options
log4js.configure({
appenders: {
sync: {
type: "fileSync",
filename: testFile,
layout: { type: "messagePassThrough" },
flags: "w",
encoding: "ascii",
mode: 0o666
}
},
categories: {
default: { appenders: ["sync"], level: "info" }
}
});
const logger = log4js.getLogger();
logger.warn("log message");
fs.readFile(testFile, "ascii", (err, contents) => {
t.include(contents, `log message${EOL}`);
t.end();
});
});
batch.end();
});