-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathlogger.spec.ts
221 lines (197 loc) · 6.38 KB
/
logger.spec.ts
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
import { expect } from "chai";
import * as logger from "../src/logger";
describe("logger", () => {
const stdoutWrite = process.stdout.write.bind(process.stdout);
const stderrWrite = process.stderr.write.bind(process.stderr);
let lastOut: string;
let lastErr: string;
beforeEach(() => {
process.stdout.write = (msg: Buffer | string, cb?: any): boolean => {
lastOut = msg as string;
return stdoutWrite(msg, cb);
};
process.stderr.write = (msg: Buffer | string, cb?: any): boolean => {
lastErr = msg as string;
return stderrWrite(msg, cb);
};
});
afterEach(() => {
process.stdout.write = stdoutWrite;
process.stderr.write = stderrWrite;
});
function expectOutput(last: string, entry: any) {
return expect(JSON.parse(last.trim())).to.deep.eq(entry);
}
function expectStdout(entry: any) {
return expectOutput(lastOut, entry);
}
function expectStderr(entry: any) {
return expectOutput(lastErr, entry);
}
describe("logging methods", () => {
it("should coalesce arguments into the message", () => {
logger.log("hello", { middle: "obj" }, "end message");
expectStdout({
severity: "INFO",
message: "hello { middle: 'obj' } end message",
});
});
it("should merge structured data from the last argument", () => {
logger.log("hello", "world", { additional: "context" });
expectStdout({
severity: "INFO",
message: "hello world",
additional: "context",
});
});
it("should not recognize null as a structured logging object", () => {
logger.log("hello", "world", null);
expectStdout({
severity: "INFO",
message: "hello world null",
});
});
it("should overwrite a 'message' field in structured object if a message is provided", () => {
logger.log("this instead", { test: true, message: "not this" });
expectStdout({
severity: "INFO",
message: "this instead",
test: true,
});
});
it("should not overwrite a 'message' field in structured object if no other args are provided", () => {
logger.log({ test: true, message: "this" });
expectStdout({
severity: "INFO",
message: "this",
test: true,
});
});
});
describe("write", () => {
describe("structured logging", () => {
describe("write", () => {
it("should remove circular references", () => {
const circ: any = { b: "foo" };
circ.circ = circ;
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
circ,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
circ: { b: "foo", circ: "[Circular]" },
});
});
it("should remove circular references in arrays", () => {
const circ: any = { b: "foo" };
circ.circ = [circ];
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
circ,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
circ: { b: "foo", circ: ["[Circular]"] },
});
});
it("should not detect duplicate object as circular", () => {
const obj: any = { a: "foo" };
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
a: obj,
b: obj,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
a: { a: "foo" },
b: { a: "foo" },
});
});
it("should not detect duplicate object in array as circular", () => {
const obj: any = { a: "foo" };
const arr: any = [
{ a: obj, b: obj },
{ a: obj, b: obj },
];
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
a: arr,
b: arr,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
a: [
{ a: { a: "foo" }, b: { a: "foo" } },
{ a: { a: "foo" }, b: { a: "foo" } },
],
b: [
{ a: { a: "foo" }, b: { a: "foo" } },
{ a: { a: "foo" }, b: { a: "foo" } },
],
});
});
it("should not break on objects that override toJSON", () => {
const obj: any = { a: new Date("August 26, 1994 12:24:00Z") };
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing toJSON",
obj,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing toJSON",
obj: { a: "1994-08-26T12:24:00.000Z" },
});
});
it("should not alter parameters that are logged", () => {
const circ: any = { b: "foo" };
circ.array = [circ];
circ.object = circ;
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
circ,
};
logger.write(entry);
expect(circ.array[0].b).to.equal("foo");
expect(circ.object.b).to.equal("foo");
expect(circ.object.array[0].object.array[0].b).to.equal("foo");
});
for (const severity of ["DEBUG", "INFO", "NOTICE"]) {
it(`should output ${severity} severity to stdout`, () => {
const entry: logger.LogEntry = {
severity: severity as logger.LogSeverity,
message: "test",
};
logger.write(entry);
expectStdout(entry);
});
}
for (const severity of ["WARNING", "ERROR", "CRITICAL", "ALERT", "EMERGENCY"]) {
it(`should output ${severity} severity to stderr`, () => {
const entry: logger.LogEntry = {
severity: severity as logger.LogSeverity,
message: "test",
};
logger.write(entry);
expectStderr(entry);
});
}
});
});
});
});