This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlogger.go
99 lines (81 loc) · 1.86 KB
/
logger.go
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
package golive
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/logrusorgru/aurora/v3"
)
const (
LogTrace = iota - 1
LogDebug
LogInfo
LogWarn
LogError
LogFatal
LogPanic
)
type Log func(level int, message string, extra map[string]interface{})
type logEx map[string]interface{}
type LoggerBasic struct {
Level int
Prefix string
TimeFormat string
}
func NewLoggerBasic() *LoggerBasic {
var l LoggerBasic
l.Level = LogWarn
l.Prefix = aurora.BrightBlue("GL ").String()
l.TimeFormat = aurora.BrightBlack("15:04:05").String()
return &l
}
func (l *LoggerBasic) Log(level int, message string, extra map[string]interface{}) {
// Level filter with override for fatal and panic
if level < l.Level && level != LogFatal && level != LogPanic {
return
}
b := strings.Builder{}
b.WriteString(l.Prefix)
b.WriteString(time.Now().Format(l.TimeFormat))
switch level {
case LogTrace:
b.WriteString(aurora.Magenta(" TRC ").String())
case LogDebug:
b.WriteString(aurora.Yellow(" DBG ").String())
case LogInfo:
b.WriteString(aurora.Green(" INF ").String())
case LogWarn:
b.WriteString(aurora.Red(" WRN ").String())
case LogError:
b.WriteString(aurora.Red(" ERR ").Bold().String())
case LogFatal:
b.WriteString(aurora.Red(" FTL ").Bold().String())
case LogPanic:
b.WriteString(aurora.Red(" PNC ").Bold().String())
default:
b.WriteString(aurora.Bold(" ??? ").String())
}
b.WriteString(message)
if len(extra) != 0 {
var keys []string
for key := range extra {
keys = append(keys, key)
}
sort.Strings(keys)
for i := 0; i < len(keys); i++ {
key := keys[i]
b.WriteString(fmt.Sprintf(" %s%+v", aurora.BrightBlack(key+"=").String(), extra[key]))
}
}
switch level {
case LogPanic:
fmt.Println(b.String())
panic(b.String())
case LogFatal:
fmt.Println(b.String())
os.Exit(1)
default:
fmt.Println(b.String())
}
}