-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
53 lines (45 loc) · 1.36 KB
/
logger.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
export interface Logger {
log(message: string): void
info(message: string): void
error(message: string): void
clear(): void
}
class GeneralLoggerImpl implements Logger {
constructor(private readonly type: string, private readonly module: string | null) { }
private getTag(): string {
if (this.module) {
return `${this.type}::${this.module}`
} else {
return this.type
}
}
private multiLog(messgae: string, fn: (messgae: string) => void) {
messgae.split("\n").forEach(it => fn(it))
}
log(message: string): void {
if (message.includes("\n")) {
this.multiLog(message, this.log)
} else {
console.log(`(${Date.now()}) [${this.getTag()}] ${message}`)
}
}
info(message: string): void {
this.log(message)
}
error(message: string): void {
if (message.includes("\n")) {
this.multiLog(message, this.error)
} else {
console.error(`(${Date.now()}) [${this.getTag()}] ${message}`)
}
}
clear() {
console.clear()
}
}
export function createHostLogger(module: string | null = null): Logger {
return new GeneralLoggerImpl("Host", module)
}
export function createRemoteLogger(module: string | null = null): Logger {
return new GeneralLoggerImpl("Remote", module)
}