-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
62 lines (49 loc) · 1.07 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
54
55
56
57
58
59
60
61
62
interface ILogEntry {
msg: string
args: unknown[]
timestamp: number
wasTimerActive: boolean
}
interface ILoggerConfig {
highlightColor: string
active: boolean
includeDates: boolean
}
class Logger {
private static resetColor = '\u001b[0m'
config: ILoggerConfig
history: ILogEntry[]
constructor(config: ILoggerConfig = {
highlightColor: '\u001b[1;36m',
active: true,
includeDates: true
}) {
this.config = config
this.history = []
}
log(msg: string, ...args: unknown[]) {
const timestamp = Date.now()
const dateString = new Date(timestamp).toTimeString()
if (this.config.includeDates)
msg = `${dateString}${msg}`
for (let i=0; i<args.length; i++) {
msg = msg.replaceAll(
`$${i}`,
`${this.config.highlightColor}${args[i].toString()}${Logger.resetColor}`
)
}
this.history.push({
wasTimerActive: this.config.active,
args,
msg,
timestamp
})
if (this.config.active)
console.log(msg)
}
}
export {
ILogEntry,
ILoggerConfig,
Logger
}