-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
113 lines (97 loc) · 2.79 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package goapi
import (
"fmt"
"github.com/fatih/color"
"strings"
"time"
)
type Logger interface {
Debug(format string, a ...any)
Info(format string, a ...any)
Warning(format string, a ...any)
Error(format string, a ...any)
Fatal(format string, a ...any)
}
type defaultLogger struct {
}
func (d *defaultLogger) Debug(format string, a ...any) {
format = fmt.Sprintf(format, a...)
format = strings.ReplaceAll(format, "\n", "\n"+spanFill("", 0, 10))
fmt.Printf(spanFill(colorDebug("DEBUG"), 5, 10) + "[" + timeFormat(time.Now()) + "] " + format + "\n")
}
func (d *defaultLogger) Info(format string, a ...any) {
format = fmt.Sprintf(format, a...)
format = strings.ReplaceAll(format, "\n", "\n"+spanFill("", 0, 10))
fmt.Printf(spanFill(colorInfo("INFO"), 4, 10) + "[" + timeFormat(time.Now()) + "] " + format + "\n")
}
func (d *defaultLogger) Warning(format string, a ...any) {
format = fmt.Sprintf(format, a...)
format = strings.ReplaceAll(format, "\n", "\n"+spanFill("", 0, 10))
fmt.Printf(spanFill(colorWarning("WARNING"), 7, 10) + "[" + timeFormat(time.Now()) + "] " + format + "\n")
}
func (d *defaultLogger) Error(format string, a ...any) {
format = fmt.Sprintf(format, a...)
format = strings.ReplaceAll(format, "\n", "\n"+spanFill("", 0, 10))
fmt.Printf(spanFill(colorError("ERROR"), 5, 10) + "[" + timeFormat(time.Now()) + "] " + format + "\n")
}
func (d *defaultLogger) Fatal(format string, a ...any) {
format = fmt.Sprintf(format, a...)
format = strings.ReplaceAll(format, "\n", "\n"+spanFill("", 0, 10))
fmt.Printf(spanFill(colorFatal("FATAL"), 5, 10) + "[" + timeFormat(time.Now()) + "] " + format + "\n")
}
type levelHandleLogger struct {
log Logger
}
func (d *levelHandleLogger) Debug(format string, a ...any) {
if d.log == nil {
return
}
if logLevel&LogDebug == 0 {
return
}
d.log.Debug(format, a...)
}
func (d *levelHandleLogger) Info(format string, a ...any) {
if d.log == nil {
return
}
if logLevel&LogInfo == 0 {
return
}
d.log.Info(format, a...)
}
func (d *levelHandleLogger) Warning(format string, a ...any) {
if d.log == nil {
return
}
if logLevel&LogWarning == 0 {
return
}
d.log.Warning(format, a...)
}
func (d *levelHandleLogger) Error(format string, a ...any) {
if d.log == nil {
return
}
if logLevel&LogError == 0 {
return
}
d.log.Error(format, a...)
}
func (d *levelHandleLogger) Fatal(format string, a ...any) {
if d.log == nil {
return
}
if logLevel&LogFail == 0 {
return
}
d.log.Fatal(format, a...)
}
var colorInfo = color.New(color.FgGreen).SprintFunc()
var colorDebug = color.New(color.FgCyan).SprintFunc()
var colorWarning = color.New(color.FgHiYellow).SprintFunc()
var colorError = color.New(color.FgRed).SprintFunc()
var colorFatal = color.New(color.BgRed, color.FgWhite).SprintFunc()
func SetLogLevel(level LogLevel) {
logLevel = level
}