-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
150 lines (124 loc) · 3.43 KB
/
index.ts
File metadata and controls
150 lines (124 loc) · 3.43 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
import { PassThrough } from "stream";
import { inspect } from "util";
export enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
}
export type LogContext = Record<string, unknown> | null;
export interface ILogEntry {
context: LogContext;
label: string;
level: LogLevel;
message: string;
timestamp: number;
}
export type LogFormatFunction = (input: ILogEntry) => string;
function formatLogContext(context: NonNullable<LogContext>) {
try {
return JSON.stringify(context);
} catch (_) {
return `\r${inspect(context).replaceAll("\n", "\r")}`;
}
}
function formatLog({ context, label, level, message, timestamp }: ILogEntry) {
return [
new Date(timestamp).toISOString(),
label,
LogLevel[level],
message.trim(),
context && formatLogContext(context),
]
.filter((item) => !!item)
.join("\t");
}
export class Log {
private readonly logStream = new PassThrough();
private readonly format: LogFormatFunction;
private _context: LogContext = null;
private _label: string = "";
constructor(format: LogFormatFunction = formatLog) {
if (typeof format !== "function") {
throw new TypeError("Expected `format` to be a function");
}
this.format = format;
}
get context() {
return this._context;
}
set context(context: LogContext) {
if (typeof context !== "object") {
throw new TypeError("Expected `context` to be an object");
}
this._context = context;
}
get label() {
return this._label;
}
set label(label: string) {
if (typeof label !== "string") {
throw new TypeError("Expected `label` to be a string");
}
this._label = label.trim();
}
clear() {
this.logStream.read();
}
flush() {
if (this.logStream.readable && this.logStream.readableLength > 0) {
process.stdout.write(this.logStream.read(this.logStream.readableLength));
}
}
private writeLog(level: LogLevel, message: string, context?: LogContext) {
if (typeof message !== "string") {
throw new TypeError("Expected `message` to be a string");
}
const entry =
this.format({
context:
!!context || (this._context && Object.keys(this._context).length)
? { ...this.context, ...context }
: null,
label: this._label,
level,
message,
timestamp: Date.now(),
}) + "\n";
switch (level) {
case LogLevel.ERROR:
this.flush();
// eslint-disable-next-line no-fallthrough
case LogLevel.WARN:
process.stderr.write(entry);
break;
case LogLevel.DEBUG:
if (
!process.env.DEBUG ||
(process.env.DEBUG !== "1" &&
process.env.DEBUG.toLowerCase() !== "on" &&
process.env.DEBUG.toLowerCase() !== "true" &&
process.env.DEBUG.toLowerCase() !== "yes")
) {
this.logStream.write(entry);
break;
}
// eslint-disable-next-line no-fallthrough
default:
process.stdout.write(entry);
}
}
debug(message: string, context?: LogContext) {
this.writeLog(LogLevel.DEBUG, message, context);
}
info(message: string, context?: LogContext) {
this.writeLog(LogLevel.INFO, message, context);
}
warn(message: string, context?: LogContext) {
this.writeLog(LogLevel.WARN, message, context);
}
error(message: string, context?: LogContext) {
this.writeLog(LogLevel.ERROR, message, context);
}
}
export default new Log();