/
logging-test.js
268 lines (236 loc) · 7.39 KB
/
logging-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
const { test } = require("tap");
const sandbox = require("@log4js-node/sandboxed-module");
const util = require("util");
const recording = require("../../lib/appenders/recording");
test("log4js", batch => {
batch.test("shutdown should return appenders and categories back to initial state", t => {
const stringifyMap = (map) => JSON.stringify(Array.from(map));
const deepCopyMap = (map) => new Map(JSON.parse(stringifyMap(map)));
const log4js = require("../../lib/log4js");
const appenders = require("../../lib/appenders");
const categories = require("../../lib/categories");
const initialAppenders = deepCopyMap(appenders);
const initialCategories = deepCopyMap(categories);
log4js.configure({
appenders: { recorder: { type: "recording" } },
categories: { default: { appenders: ["recorder"], level: "DEBUG" } }
});
const configuredAppenders = deepCopyMap(appenders);
const configuredCategories = deepCopyMap(categories);
t.notEqual(
stringifyMap(configuredAppenders),
stringifyMap(initialAppenders),
"appenders should be different from initial state"
);
t.notEqual(
stringifyMap(configuredCategories),
stringifyMap(initialCategories),
"categories should be different from initial state"
);
log4js.shutdown(() => {
const finalAppenders = deepCopyMap(appenders);
const finalCategories = deepCopyMap(categories);
t.equal(
stringifyMap(finalAppenders),
stringifyMap(initialAppenders),
"appenders should revert back to initial state"
);
t.equal(
stringifyMap(finalCategories),
stringifyMap(initialCategories),
"categories should revert back to initial state"
);
t.end();
});
});
batch.test("getLogger", t => {
const log4js = require("../../lib/log4js");
log4js.configure({
appenders: { recorder: { type: "recording" } },
categories: { default: { appenders: ["recorder"], level: "DEBUG" } }
});
const logger = log4js.getLogger("tests");
t.test("should take a category and return a logger", assert => {
assert.equal(logger.category, "tests");
assert.equal(logger.level.toString(), "DEBUG");
assert.type(logger.debug, "function");
assert.type(logger.info, "function");
assert.type(logger.warn, "function");
assert.type(logger.error, "function");
assert.type(logger.fatal, "function");
assert.end();
});
t.test("log events", assert => {
recording.reset();
logger.debug("Debug event");
logger.trace("Trace event 1");
logger.trace("Trace event 2");
logger.warn("Warning event");
logger.error("Aargh!", new Error("Pants are on fire!"));
logger.error("Simulated CouchDB problem", {
err: 127,
cause: "incendiary underwear"
});
const events = recording.replay();
assert.equal(events[0].level.toString(), "DEBUG");
assert.equal(events[0].data[0], "Debug event");
assert.type(events[0].startTime, "Date");
assert.equal(events.length, 4, "should not emit events of a lower level");
assert.equal(events[1].level.toString(), "WARN");
assert.type(
events[2].data[1],
"Error",
"should include the error if passed in"
);
assert.equal(events[2].data[1].message, "Pants are on fire!");
assert.end();
});
t.end();
});
batch.test("when shutdown is called", t => {
const events = {
shutdownCalled: []
};
const log4js = sandbox.require("../../lib/log4js", {
requires: {
"./appenders/file": {
name: "file",
configure() {
function thing(evt) {
events.event = evt;
return null;
}
thing.shutdown = function(cb) {
events.shutdownCalled.push(true);
cb();
};
return thing;
}
}
}
});
const config = {
appenders: {
file: {
type: "file",
filename: "cheesy-wotsits.log",
maxLogSize: 1024,
backups: 3
},
alsoFile: {
type: "file"
}
},
categories: {
default: { appenders: ["file", "alsoFile"], level: "DEBUG" }
}
};
log4js.configure(config);
const logger = log4js.getLogger();
log4js.shutdown(() => {
t.equal(
events.shutdownCalled.length,
2,
"should invoke appender shutdowns"
);
logger.info("this should not go to the appenders");
t.notOk(events.event);
t.end();
});
});
batch.test("configuration when passed as filename", t => {
let appenderConfig;
let configFilename;
const log4js = sandbox.require("../../lib/log4js", {
ignoreMissing: true,
requires: {
fs: {
statSync() {
return { mtime: Date.now() };
},
readFileSync(filename) {
configFilename = filename;
return JSON.stringify({
appenders: {
file: {
type: "file",
filename: "whatever.log"
}
},
categories: { default: { appenders: ["file"], level: "DEBUG" } }
});
},
readdirSync() {
return ["file"];
}
},
"./file": {
configure(configuration) {
appenderConfig = configuration;
return function() {};
}
}
}
});
log4js.configure("/path/to/cheese.json");
t.equal(
configFilename,
"/path/to/cheese.json",
"should read the config from a file"
);
t.equal(
appenderConfig.filename,
"whatever.log",
"should pass config to appender"
);
t.end();
});
batch.test("with configure not called", t => {
const fakeStdoutAppender = {
configure() {
this.required = true;
return function(evt) {
fakeStdoutAppender.evt = evt;
};
}
};
const log4js = sandbox.require("../../lib/log4js", {
requires: {
"./appenders/stdout": fakeStdoutAppender
}
});
const logger = log4js.getLogger("some-logger");
logger.debug("This is a test");
t.ok(fakeStdoutAppender.required, "stdout should be required");
t.notOk(fakeStdoutAppender.evt, "should not log anything");
t.end();
});
batch.test("with configure called with empty values", t => {
[null, undefined, "", " ", []].forEach(config => {
const log4js = require("../../lib/log4js");
const expectedError = `Problem reading config from file "${util.inspect(
config
)}". Error was ENOENT: no such file or directory`;
t.throws(() => log4js.configure(config), expectedError);
});
t.end();
});
batch.test("configuration persistence", t => {
const firstLog4js = require("../../lib/log4js");
firstLog4js.configure({
appenders: { recorder: { type: "recording" } },
categories: { default: { appenders: ["recorder"], level: "DEBUG" } }
});
recording.reset();
const secondLog4js = require("../../lib/log4js");
secondLog4js
.getLogger()
.info("This should go to the appender defined in firstLog4js");
t.equal(
recording.replay()[0].data[0],
"This should go to the appender defined in firstLog4js"
);
t.end();
});
batch.end();
});