-
Notifications
You must be signed in to change notification settings - Fork 2
/
FlagshipLogManager.ts
89 lines (75 loc) · 3.02 KB
/
FlagshipLogManager.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
import { FLAGSHIP_SDK, LogLevel } from '../enum/index.ts'
export interface IFlagshipLogManager {
/**
*System is unusable.
* @param message
* @param tag
*/
emergency(message: string, tag: string): void;
alert(message: string, tag: string): void;
critical(message: string, tag: string): void;
error(message: string, tag: string): void;
warning(message: string, tag: string): void;
notice(message: string, tag: string): void;
info(message: string, tag: string): void;
debug(message: string, tag: string): void;
log(level: LogLevel, message: string, tag: string): void;
}
export class FlagshipLogManager implements IFlagshipLogManager {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
consoleError: { (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
consoleWarn: { (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
consoleDebug: { (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }
constructor () {
this.consoleError = console.error ?? console.log
this.consoleWarn = console.warn ?? console.log
this.consoleDebug = console.debug ?? console.log
}
emergency (message: string, tag: string): void {
this.consoleError(this.formatOutput(LogLevel.EMERGENCY, message, tag))
}
alert (message: string, tag: string): void {
this.consoleError(this.formatOutput(LogLevel.ALERT, message, tag))
}
critical (message: string, tag: string): void {
this.consoleError(this.formatOutput(LogLevel.CRITICAL, message, tag))
}
error (message: string, tag: string): void {
this.consoleError(this.formatOutput(LogLevel.ERROR, message, tag))
}
warning (message: string, tag: string): void {
this.consoleWarn(this.formatOutput(LogLevel.WARNING, message, tag))
}
notice (message: string, tag: string): void {
this.log(LogLevel.NOTICE, message, tag)
}
info (message: string, tag: string): void {
console.info(this.formatOutput(LogLevel.INFO, message, tag))
}
debug (message: string, tag: string): void {
this.consoleDebug(this.formatOutput(LogLevel.DEBUG, message, tag))
}
log (level: LogLevel, message: string, tag: string): void {
console.log(this.formatOutput(level, message, tag))
}
protected formatOutput (level: LogLevel, message: string, tag: string):string {
const now = new Date(Date.now())
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getTwoDigit = (value: any) => {
return value.toString().length === 1 ? `0${value}` : value
}
return `[${getTwoDigit(now.getFullYear())}-${
getTwoDigit(
now.getMonth() + 1
)
}-${getTwoDigit(now.getDate())} ${
getTwoDigit(
now.getHours()
)
}:${getTwoDigit(now.getMinutes())}:${getTwoDigit(now.getSeconds())}.${getTwoDigit(now.getMilliseconds())}] [${FLAGSHIP_SDK}] [${
LogLevel[level]
}] [${tag}] : ${message}`
}
}