-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
log.ts
166 lines (144 loc) · 3.94 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
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
/**
* Logs are assigned a priority number.
* Higher numbers are less important information.
* Set your desired log level higher to get more info.
*
* -1 nothing
* 0 only errors
* 1 also warnings
* 2 also logs
* 3 also debugs
*
* Logs also come from different "sources" which can
* have different log level settings.
*
* Two ways to modify the log level settings:
*
* 1. Set an environment variable in the shell.
* This applies to all "sources" (can't be set individually)
* EARTHSTAR_LOG_LEVEL=2 npm run test
*
* 2. Use setLogLevels() to globally modify the levels:
* setLogLevels({ sync: 2 });
*
* The environment variable wins over the numbers set by setLogLevels.
*/
//================================================================================
// TYPES
type LogSource = string;
export enum LogLevel {
None = -1,
Error = 0, // default
Warn = 1,
Log = 2,
Info = 3,
Debug = 4, // most verbose
}
export const DEFAULT_LOG_LEVEL = LogLevel.Error;
type LogLevels = Record<LogSource, LogLevel>;
//================================================================================
// ENV VAR
/*
// get the single log level number from the environment, or undefined if not set
const readEnvLogLevel = (): LogLevel | undefined => {
if (process?.env?.EARTHSTAR_LOG_LEVEL) {
const parsed = parseInt(process.env.EARTHSTAR_LOG_LEVEL);
if (isNaN(parsed)) { return undefined }
if (parsed !== Math.floor(parsed)) { return undefined; }
return parsed;
}
return undefined;
}
// apply env var setting
const ENV_LOG_LEVEL = readEnvLogLevel();
*/
//================================================================================
// GLOBAL SETTINGS
// make global singleton to hold log levels
let globalLogLevels: LogLevels = {
// result is the min of (_env) and anything else
_default: DEFAULT_LOG_LEVEL,
};
export function updateLogLevels(newLogLevels: LogLevels): void {
globalLogLevels = {
...globalLogLevels,
...newLogLevels,
};
}
export function setLogLevel(source: LogSource, level: LogLevel) {
globalLogLevels[source] = level;
}
export function setDefaultLogLevel(level: LogLevel) {
globalLogLevels._default = level;
}
export function getLogLevel(source: LogSource): LogLevel {
if (source in globalLogLevels) {
return globalLogLevels[source];
} else {
return globalLogLevels._default;
}
}
export function getLogLevels(): LogLevels {
return globalLogLevels;
}
//================================================================================
// Logger class
type LogColor =
| "blue"
| "aqua"
| "darkcyan"
| "cyan"
| "dimgray"
| "slategray"
| "green"
| "springgreen"
| "grey"
| "darkMagenta"
| "magenta"
| "red"
| "orangeRed"
| "salmon"
| "lightsalmon"
| "gold"
| "yellow";
export class Logger {
source: LogSource;
color: LogColor | undefined = undefined;
constructor(source: LogSource, color?: LogColor) {
this.source = source;
this.color = color || "aqua";
}
_print(level: LogLevel, showTag: boolean, indent: string, ...args: any[]) {
if (level <= getLogLevel(this.source)) {
if (showTag) {
const tag = `[${this.source}]`;
if (this.color !== undefined) {
const tagArgs = [`%c ${tag}`, `color: ${this.color}`];
console.log(indent, ...tagArgs, ...args);
} else {
console.log(indent, tag, ...args);
}
} else {
console.log(indent, ...args);
}
}
}
error(...args: any[]) {
this._print(LogLevel.Error, true, "!!", ...args);
}
warn(...args: any[]) {
this._print(LogLevel.Warn, true, "! ", ...args);
}
log(...args: any[]) {
this._print(LogLevel.Log, true, " ", ...args);
}
info(...args: any[]) {
this._print(LogLevel.Info, true, " ", ...args);
}
debug(...args: any[]) {
this._print(LogLevel.Debug, true, " ", ...args);
}
blank() {
this._print(LogLevel.Info, false, "");
}
}