-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.js
136 lines (103 loc) · 2.63 KB
/
console.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
import { existsSync, statSync, createWriteStream, mkdirSync } from "node:fs";
const logs = [];
const reset = "\x1b[0m",
muted = "\x1b[38;5;243m";
const levels = {
note: "\x1b[38;5;216m",
debug: "\x1b[38;5;188m",
info: "\x1b[38;5;117m",
warn: "\x1b[38;5;202m",
error: "\x1b[38;5;124m",
fatal: "\x1b[38;5;196m",
};
function strip(str) {
return str.replace(/\x1b\[[\d;]+m/g, "");
}
function write(target, message) {
const stripped = strip(message);
if (logs.unshift(stripped) > 2000) {
logs.pop();
}
try {
// No colors if target is not stdout
if (target !== process.stdout) {
message = stripped;
}
target.write(`${message}\n`);
} catch (err) {
process.stdout.write(`Unable to write log: ${err}\n`);
}
}
function print(target, level, message) {
const color = levels[level] || reset;
write(target || process.stdout, `${muted}[${new Date().toISOString()}] [${color}${level.padEnd(5, " ")}${muted}] ${reset}${message}`);
}
function log(target, ...args) {
print(target, "note", args.join(" "));
}
function debug(target, ...args) {
print(target, "debug", args.join(" "));
}
function info(target, ...args) {
print(target, "info", args.join(" "));
}
function warn(target, ...args) {
print(target, "warn", args.join(" "));
}
function error(target, ...args) {
print(target, "error", args.join(" "));
}
function fatal(...args) {
print(process.stdout, "fatal", args.join(" "));
}
function resolveLogFile(name) {
if (!name || typeof name !== "string") {
return process.stdout;
}
// For development purposes windows always prints to stdout
if (process.platform === "win32") {
return process.stdout;
}
if (!existsSync("./logs")) {
mkdirSync("./logs");
}
const path = `./logs/${name}.log`;
if (existsSync(path)) {
const stat = statSync(path);
if (stat.size > 10 * 1024 * 1024) {
unlinkSync(path);
info(false, `Rotated ${path}`);
}
}
return createWriteStream(path, {
flags: "a",
});
}
export function getLogs() {
return logs;
}
export function registerErrorHandlers() {
function unhandled(err) {
fatal(err instanceof Error ? err.stack : String(err));
}
process.on("uncaughtException", unhandled);
process.on("unhandledRejection", unhandled);
}
export function registerConsole(name) {
const target = resolveLogFile(name);
console.log = (...args) => {
log(target, args.join(" "));
};
console.debug = (...args) => {
debug(target, args.join(" "));
};
console.info = (...args) => {
info(target, args.join(" "));
};
console.warn = (...args) => {
warn(target, args.join(" "));
};
console.error = (...args) => {
error(target, args.join(" "));
};
}