1 | import { config } from './config'
|
2 |
|
3 | const levels = [
|
4 | 'debug',
|
5 | 'info',
|
6 | 'warn',
|
7 | 'error',
|
8 | 'none'
|
9 | ]
|
10 |
|
11 | const levelToInt: {[level: string]: number} = {}
|
12 | const intToLevel: {[int: number]: string} = {}
|
13 |
|
14 | for (let index = 0; index < levels.length; index++) {
|
15 | const level = levels[index]
|
16 | levelToInt[level] = index
|
17 | intToLevel[index] = level
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export class Logger {
|
24 | static error(message: string) {
|
25 | if (!this.shouldLog('error')) return
|
26 | console.error(this.logMessage('error', message))
|
27 | }
|
28 |
|
29 | static warn(message: string) {
|
30 | if (!this.shouldLog('warn')) return
|
31 | console.warn(this.logMessage('warn', message))
|
32 | }
|
33 |
|
34 | static info(message: string) {
|
35 | if (!this.shouldLog('info')) return
|
36 | console.log(this.logMessage('info', message))
|
37 | }
|
38 |
|
39 | static debug(message: string) {
|
40 | if (!this.shouldLog('debug')) return
|
41 | console.log(this.logMessage('debug', message))
|
42 | }
|
43 |
|
44 | static logMessage(level: string, message: string) {
|
45 | return `[${level.toUpperCase()}] ${message}`
|
46 | }
|
47 |
|
48 | static shouldLog(level: string) {
|
49 | const currentLevel = levelToInt[config.logLevel]
|
50 | return currentLevel <= levelToInt[level]
|
51 | }
|
52 | }
|