-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.js
More file actions
166 lines (138 loc) · 4.12 KB
/
log.js
File metadata and controls
166 lines (138 loc) · 4.12 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
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import { OutputListener } from "util/output_listener.js";
import { CommandLine } from "infrastructure/command_line.js";
import { Clock } from "infrastructure/clock.js";
/**
* Logger. Writes structured logs as JSON to stdout. Provides the ability to set default values which
* are included in every log output.
*/
export class Log {
/**
* Factory method. Creates a logger.
* @returns {Log} the logger
*/
static create() {
ensure.signature(arguments, []);
return new Log(CommandLine.create(), Clock.create());
}
/**
* Factory method. Creates a logger that doesn't write to stdout.
* @returns {Log} the nulled logger
*/
static createNull() {
ensure.signature(arguments, []);
return new Log(CommandLine.createNull(), Clock.createNull());
}
/**
* @returns {string} 'debug' alert level
*/
static get DEBUG() { return "debug"; }
/**
* @returns {string} 'info' alert level
*/
static get INFO() { return "info"; }
/**
* @returns {string} 'monitor' alert level
*/
static get MONITOR() { return "monitor"; }
/**
* @returns {string} 'action' alert level
*/
static get ACTION() { return "action"; }
/**
* @returns {string} 'emergency' alert level
*/
static get EMERGENCY() { return "emergency"; }
/** Only for use by tests. (Use a factory method instead.) */
constructor(commandLine, clock, defaults = {}) {
ensure.signature(arguments, [ CommandLine, Clock, [ undefined, Object ] ]);
this._commandLine = commandLine;
this._clock = clock;
this._listener = new OutputListener();
this._defaults = defaults;
}
/**
* @returns {object} the logger's default values (properties that are written with every log)
*/
get defaults() {
return this._defaults;
}
/**
* Write a log entry with the 'debug' alert level.
* @param data the data to write.
*/
debug(data) { this.#log(Log.DEBUG, data); }
/**
* Write a log entry with the 'info' alert level.
* @param data the data to write.
*/
info(data) { this.#log(Log.INFO, data); }
/**
* Write a log entry with the 'monitor' alert level.
* @param data the data to write.
*/
monitor(data) { this.#log(Log.MONITOR, data); }
/**
* Write a log entry with the 'action' alert level.
* @param data the data to write.
*/
action(data) { this.#log(Log.ACTION, data); }
/**
* Write a log entry with the 'emergency' alert level.
* @param data the data to write.
*/
emergency(data) { this.#log(Log.EMERGENCY, data); }
/**
* Create a new logger, based on this one, which has additional default parameters. Existing defaults are
* carried over. New defaults override existing defaults with the same name. Output tracking is shared by
* both logs.
* @param defaults default parameters for the new logger
* @returns {Log} the new logger
*/
bind(defaults) {
ensure.signature(arguments, [ Object ]);
const newLog = new Log(this._commandLine, this._clock, { ...this._defaults, ...defaults });
newLog._listener = this._listener;
return newLog;
}
/**
* Track log output.
* @returns {OutputTracker} the output tracker
*/
trackOutput() {
ensure.signature(arguments, []);
return this._listener.trackOutput();
}
#log(alert, data) {
ensure.signature(arguments, [ String, Object ], [ "alert", "data" ]);
data = { alert, ...this._defaults, ...data };
const { dataToLog, dataToTrack } = normalizeErrors(data);
this._commandLine.writeStdout(`${(currentFormattedTime(this._clock))} ${JSON.stringify(dataToLog)}\n`);
this._listener.emit(dataToTrack);
}
}
function normalizeErrors(data) {
const dataToLog = {};
const dataToTrack = {};
Object.entries(data).forEach(([ name, value ]) => {
let logValue = value;
let trackValue = value;
if (value instanceof Error) {
logValue = value.stack;
trackValue = `${value.name}: ${value.message}`;
}
dataToLog[name] = logValue;
dataToTrack[name] = trackValue;
});
return { dataToLog, dataToTrack };
}
function currentFormattedTime(clock) {
const options = {
dateStyle: "medium",
timeStyle: "long",
timeZone: "UTC",
hourCycle: "h23",
};
return clock.toFormattedString(options, "en-US");
}