-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
213 lines (193 loc) · 6.1 KB
/
index.ts
File metadata and controls
213 lines (193 loc) · 6.1 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Simple log written in TypeScript.
* @copyright 2021 Sampsa Lohi (https://github.com/sam-19)
* @license MIT
*/
import { SimpleLogType, ValidLevel } from './index.d'
/**
* A static logger for application events.
*
* All events are recorded, but only events of sufficient importance
* are printed directly to console.
*
* Includes levels for debug (0), info (1), warning (2), and error (3) messages.
*
* @example
* import SimpleLog from './log'
* SimpleLog.setLevel(2) // Only log warnings (= 2) and errors (= 3) to console
* SimpleLog.info('Application loading') // Does not print to console (info = 1)
* SimpleLog.warn('Disabling log printing to console') // Prints to console (warning = 2)
* SimpleLog.setLevel(4) // Don't print anything
*/
class SimpleLog implements SimpleLogType {
// Static properties
/**
* Verbosity of the logger (what messages end up in the console)
* ```
* 0 = log everything
* 1 = log info messages and above
* 2 = log warning and errors
* 3 = log only errors
* 4 = disable logging
* ```
*/
static readonly LEVELS = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
DISABLE: 4,
} as { [name: string ]: ValidLevel }
// Instance properties
static events: LogEvent[] = []
static level: ValidLevel = 0
static prevTimestamp: number | null = null
/**
* Add a new message to the log at the given level.
* @param level SimpleLog.LEVEL
* @param message
*/
static add (level: ValidLevel, message: string) {
if (Object.values(SimpleLog.LEVELS).indexOf(level) === -1) {
// Not a valid logging level
console.warn(`Did not add an event with an invalid level to log: (${level}) ${message}`)
} else {
let logEvent = new LogEvent(level, message)
if (level >= SimpleLog.level) {
this.print(logEvent)
}
SimpleLog.events.push(logEvent)
SimpleLog.prevTimestamp = logEvent.time.toTime()
}
}
/**
* Add a message at debug level to the log
* @param message
*/
static debug (message: string) {
SimpleLog.add(SimpleLog.LEVELS.DEBUG, message)
}
/**
* Add a message at error level to the log
* @param message
*/
static error (message: string) {
SimpleLog.add(SimpleLog.LEVELS.ERROR, message)
}
/**
* Get current logging level
*/
static getLevel () {
return SimpleLog.level
}
/**
* Add a message at info level to the log
* @param message
*/
static info (message: string) {
SimpleLog.add(SimpleLog.LEVELS.INFO, message)
}
/**
* Print a log event's message to console
* @param logEvent
*/
static print (logEvent: LogEvent) {
let message = []
if (logEvent.scope) {
message.push(`[${logEvent.scope}]`)
}
message = message.concat([logEvent.time.toString(), logEvent.message])
if (logEvent.level === SimpleLog.LEVELS.DEBUG) {
message.unshift('DEBUG')
console.debug(message.join(' '))
} else if (logEvent.level === SimpleLog.LEVELS.INFO) {
// Keep the first part of the message always the same length
message.unshift('INFO ')
console.info(message.join(' '))
} else if (logEvent.level === SimpleLog.LEVELS.WARN) {
message.unshift('WARN ')
console.warn(message.join(' '))
} else if (logEvent.level === SimpleLog.LEVELS.ERROR) {
message.unshift('ERROR')
console.error(message.join(' '))
}
}
/**
* Set the level of logging events to display in console.
* @param level new logging level
*/
static setLevel (level: ValidLevel) {
if (Object.values(SimpleLog.LEVELS).indexOf(level) !== -1) {
SimpleLog.level = level
} else {
SimpleLog.warn(`Did not set invalid logging level ${level}`)
}
}
/**
* Add a message at warning level to the log
* @param message
*/
static warn (message: string) {
SimpleLog.add(SimpleLog.LEVELS.WARN, message)
}
}
// Auxiliary classes that are not meant to be exported
/**
* A single event in the game log.
*/
class LogEvent {
level: number
message: string
printed: boolean
scope: string | undefined
time: LogTimestamp
constructor (level: number, message: string, scope?: string) {
this.level = level
this.message = message
this.printed = false
this.scope = scope
this.time = new LogTimestamp()
}
}
/**
* SimpleLog event timestamp.
*/
class LogTimestamp {
date: Date
delta: number | null
constructor () {
this.date = new Date()
this.delta = SimpleLog.prevTimestamp ? this.date.getTime() - SimpleLog.prevTimestamp : null
}
/**
* Get a standard length datetime string from this timestamp
* @param utc return as UTC time (default false)
* @return YYYY-MM-DD hh:mm:ss
*/
toString (utc=false) {
let Y, M, D, h, m, s
if (utc) {
Y = this.date.getFullYear()
M = (this.date.getMonth() + 1).toString().padStart(2, '0')
D = this.date.getDate().toString().padStart(2, '0')
h = this.date.getHours().toString().padStart(2, '0')
m = this.date.getMinutes().toString().padStart(2, '0')
s = this.date.getSeconds().toString().padStart(2, '0')
} else {
Y = this.date.getUTCFullYear()
M = (this.date.getUTCMonth() + 1).toString().padStart(2, '0')
D = this.date.getUTCDate().toString().padStart(2, '0')
h = this.date.getUTCHours().toString().padStart(2, '0')
m = this.date.getUTCMinutes().toString().padStart(2, '0')
s = this.date.getUTCSeconds().toString().padStart(2, '0')
}
return `${Y}-${M}-${D} ${h}:${m}:${s}`
}
/**
* Return timestamp as milliseconds
*/
toTime () {
return this.date.getTime()
}
}
export default SimpleLog