-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_log_test.js
More file actions
177 lines (137 loc) · 4.1 KB
/
_log_test.js
File metadata and controls
177 lines (137 loc) · 4.1 KB
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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import assert from "util/assert.js";
import { Log } from "infrastructure/log.js";
import { CommandLine } from "infrastructure/command_line.js";
import { Clock } from "infrastructure/clock.js";
// dependency_analysis: ./_log_test_output_runner
// dependency_analysis: ./_log_test_null_output_runner
describe("Log", () => {
it("writes current time and structured data to stdout", async () => {
const { log, stdout } = createLog({ now: 0 });
log.info({ output: "my output" });
assert.deepEqual(stdout.data, [
'Jan 1, 1970, 00:00:00 UTC {"alert":"info","output":"my output"}\n',
]);
});
it("outputs full stack trace for errors", () => {
const { log, stdout } = createLog({ now: 0 });
const data = {
output: new Error("my error"),
};
log.info(data);
assert.match(stdout.data[0], /"output":"Error: my error\\n at /);
});
it("provides multiple alert levels", () => {
const { log, stdout } = createLog({ now: 0 });
log.debug({});
log.info({});
log.monitor({});
log.action({});
log.emergency({});
assert.deepEqual(stdout.data, [
'Jan 1, 1970, 00:00:00 UTC {"alert":"debug"}\n',
'Jan 1, 1970, 00:00:00 UTC {"alert":"info"}\n',
'Jan 1, 1970, 00:00:00 UTC {"alert":"monitor"}\n',
'Jan 1, 1970, 00:00:00 UTC {"alert":"action"}\n',
'Jan 1, 1970, 00:00:00 UTC {"alert":"emergency"}\n',
]);
});
describe("tracking", () => {
it("tracks output", () => {
const { log, output } = createLog();
log.info({
output: "my output",
});
assert.deepEqual(output.data, [{
alert: "info",
output: "my output",
}]);
});
it("strips stack traces from errors", () => {
const { log, output } = createLog();
log.info({
output: new Error("my error"),
});
assert.deepEqual(output.data, [{
alert: "info",
output: "Error: my error",
}]);
});
});
describe("binding", () => {
it("adds default fields to every message", () => {
const { log, output } = createLog();
const boundLog = log.bind({ "my-default": "my-value" });
boundLog.info({ message: "my-message" });
assert.deepEqual(output.data, [{
alert: "info",
"my-default": "my-value",
message: "my-message",
}]);
});
it("exposes defaults", () => {
const { log } = createLog();
const boundLog = log.bind({ "my-default": "my-value" });
assert.deepEqual(boundLog.defaults, { "my-default": "my-value" }, "bound log should have defaults");
assert.deepEqual(log.defaults, {}, "parent log shouldn't have any defaults");
});
it("can bind multiple times (and later bindings override earlier bindings)", () => {
const { log } = createLog();
const bind1 = log.bind({ binding: "one", bind1: true });
assert.deepEqual(bind1.defaults, {
binding: "one",
bind1: true,
}, "bind1");
const bind2 = bind1.bind({ binding: "two", bind2: true });
assert.deepEqual(bind2.defaults, {
binding: "two",
bind1: true,
bind2: true,
}, "bind2");
const bind3 = bind2.bind({ binding: "three", bind3: true });
assert.deepEqual(bind3.defaults, {
binding: "three",
bind1: true,
bind2: true,
bind3: true,
}, "bind3");
});
it("bound logs share a single tracker", () => {
const { log, output } = createLog();
const bind1 = log.bind({ bind1: true });
const bind2 = bind1.bind({ bind2: true });
log.info({ message: "original log" });
bind1.info({ message: "bind1" });
bind2.info({ message: "bind2" });
assert.deepEqual(output.data, [
{
alert: "info",
message: "original log",
}, {
alert: "info",
bind1: true,
message: "bind1",
}, {
alert: "info",
bind1: true,
bind2: true,
message: "bind2",
},
]);
});
});
});
function createLog({
now = 4200000,
} = {}) {
ensure.signature(arguments, [[ undefined, {
now: [ undefined, Number ],
}]]);
const commandLine = CommandLine.createNull();
const stdout = commandLine.trackStdout();
const clock = Clock.createNull({ now });
const log = new Log(commandLine, clock);
const output = log.trackOutput();
return { log, stdout, output };
}