-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlogging.ts
45 lines (38 loc) · 1.04 KB
/
logging.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
// logging utilities.
// help print messages when environment variable DEBUG is passed to the docker.
export const DEBUG: boolean = Boolean(process.env.DEBUG)
export type LoggingMessageType =
| string
| number
| boolean
| object
| null
export function debug (msg: LoggingMessageType): void {
if (!DEBUG) return
switch (typeof msg) {
case "object":
console.log("\x1b[36m[DEBUG]\x1b[0m")
console.dir(msg, { "depth": null })
break
case "string":
case "number":
case "boolean":
console.log("\x1b[36m[DEBUG]\x1b[0m", msg)
break
default:
console.log("\x1b[36m[DEBUG]\x1b[0m", "null")
break
}
}
export function debugEach (arr?: Array<LoggingMessageType>): void {
if (!DEBUG) return
arr?.forEach(msg => {
debug(msg)
})
}
export function debugWhen (cond: boolean, msg: LoggingMessageType): void {
cond && debug(msg)
}
export function debugEither (cond: boolean, msgTrue: LoggingMessageType, msgFalse: LoggingMessageType): void {
cond ? debug(msgTrue) : debug(msgFalse)
}