-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.mjs
68 lines (58 loc) · 1.5 KB
/
logger.mjs
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
'use strict';
import chalk from 'chalk'
import config from './config'
const LogLevels = {
All: 0,
Trace: 15,
Debug: 33,
Info: 50,
Warn: 75,
Error: 90,
Fatal: 100,
}
const colorMap = {}
colorMap[LogLevels.All] = chalk.gray;
colorMap[LogLevels.Trace] = chalk.blue;
colorMap[LogLevels.Debug] = chalk.green;
colorMap[LogLevels.Info] = chalk.whiteBright;
colorMap[LogLevels.Warn] = chalk.yellowBright;
colorMap[LogLevels.Error] = chalk.magentaBright;
colorMap[LogLevels.Fatal] = chalk.bold.redBright;
class Logger {
constructor(classname, lvl) {
this.classname = classname
this.currentLogLevel = lvl
}
printmsg(level, msgObj) {
if (level >= this.currentLogLevel) {
var timestamp = new Date().toISOString()
var className = this.classname
var msg = msgObj
var msg = `${timestamp} - ${className} - ${msg}`;
msg = colorMap[level](msg)
console.log(msg)
}
}
Trace(msgObj) {
this.printmsg(LogLevels.Trace, msgObj)
}
Debug(msgObj) {
this.printmsg(LogLevels.Debug, msgObj)
}
Info(msgObj) {
this.printmsg(LogLevels.Info, msgObj)
}
Warn(msgObj) {
this.printmsg(LogLevels.Warn, msgObj)
}
Error(msgObj) {
this.printmsg(LogLevels.Error, msgObj)
}
Fatal(msgObj) {
this.printmsg(LogLevels.Fatal, msgObj)
}
}
const NewLog = (name) => {
return new Logger(name, config.LogLevel)
}
export default NewLog