-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.ts
71 lines (69 loc) · 1.73 KB
/
log.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
import { config } from "../config";
import { Notyf } from "notyf";
import "notyf/notyf.min.css";
import log from "loglevel";
/** Record log statements and show some to the user via overlays. */
export function initLog(): void {
const notyf = new Notyf({
duration: 10000,
types: [
{
type: "warn",
backgroundColor: "orange",
icon: {
className: "material-icons",
tagName: "i",
text: "warning",
},
},
{
type: "info",
backgroundColor: "green",
icon: {
className: "material-icons",
tagName: "i",
text: "info",
},
},
{
type: "debug",
backgroundColor: "blue",
icon: {
className: "material-icons",
tagName: "i",
text: "debug",
},
},
{
type: "trace",
backgroundColor: "darkgray",
icon: {
className: "material-icons",
tagName: "i",
text: "trace",
},
},
],
});
log.setLevel(config.logLevelConsole);
const levels = ["error", "warn", "info", "debug", "trace"]; // no point in decorating silent
const value = { silent: 0, error: 1, warn: 2, info: 3, debug: 4, trace: 5 };
// wrap the loglevel functions in a "decorator" that adds logging to memory and to display
for (const level of levels) {
const tmp = log[level]; // save that particular original logging function of the loglevel library
log[level] = (
message // overwrite that function with a new function
) => {
tmp(message); // execute the saved original function, which writes to console
if (value[level] <= value[config.logLevelMemory]) {
if (!log["logs"]) {
log["logs"] = [];
}
log["logs"].push(message);
}
if (value[level] <= value[config.logLevelDisplay]) {
notyf.open({ type: level, message: message });
}
};
}
}