-
Notifications
You must be signed in to change notification settings - Fork 277
/
logger.go
142 lines (122 loc) · 3.24 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
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
package logger
import (
"context"
"fmt"
"io"
"github.com/fatih/color"
)
var (
Red = color.New(color.FgRed)
Green = color.New(color.FgGreen)
Yellow = color.New(color.FgYellow)
Blue = color.New(color.FgBlue)
Magenta = color.New(color.FgMagenta)
Cyan = color.New(color.FgCyan)
White = color.New(color.FgWhite)
BlueBold = color.New(color.Bold, color.FgBlue)
MagentaBold = color.New(color.Bold, color.FgMagenta)
RedBold = color.New(color.Bold, color.FgRed)
YellowBold = color.New(color.Bold, color.FgYellow)
)
// Log Level
type Level int
const (
Silent Level = iota
Error
Warn
Info
)
// Writer log writer interface
type CustomLogger interface {
Printf(string, ...interface{})
Writer() io.Writer
}
// Logger interface
type Logger interface {
Info(context.Context, string, ...interface{})
Warn(context.Context, string, ...interface{})
Error(context.Context, string, ...interface{})
SetLevel(Level)
}
func NewLogger(customLogger CustomLogger, level Level, colorful bool) Logger {
lg := logger{
customLogger: customLogger,
Colorful: colorful,
info: make([]*content, 0),
warn: make([]*content, 0),
err: make([]*content, 0),
Level: level,
}
if colorful {
lg.info = append(lg.info, &content{"[info] ", false, Green})
lg.warn = append(lg.warn, &content{"[warn] ", false, Magenta})
lg.err = append(lg.err, &content{"[error] ", false, Red})
} else {
lg.info = append(lg.info, &content{"[info] ", false, White})
lg.warn = append(lg.warn, &content{"[warn] ", false, White})
lg.err = append(lg.err, &content{"[error] ", false, White})
}
return &lg
}
type logger struct {
customLogger CustomLogger
Colorful bool
Level Level
info, warn, err []*content
}
type content struct {
text string
newLine bool
color *color.Color
}
// Info print info
func (l *logger) Info(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Info {
l.info = append(l.info, &content{fmt.Sprintf(msg, data...), true, White})
for _, item := range l.info {
if item.color != nil {
if item.newLine {
item.color.Fprintln(l.customLogger.Writer(), item.text)
} else {
item.color.Fprint(l.customLogger.Writer(), item.text)
}
}
}
l.info = l.info[:1]
}
}
// Warn print warn messages
func (l *logger) Warn(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Warn {
l.warn = append(l.warn, &content{fmt.Sprintf(msg, data...), true, White})
for _, item := range l.warn {
if item.color != nil {
if item.newLine {
item.color.Fprintln(l.customLogger.Writer(), item.text)
} else {
item.color.Fprint(l.customLogger.Writer(), item.text)
}
}
}
l.warn = l.warn[:1]
}
}
// Error print error messages
func (l *logger) Error(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Error {
l.err = append(l.err, &content{fmt.Sprintf(msg, data...), true, White})
for _, item := range l.err {
if item.color != nil {
if item.newLine {
item.color.Fprintln(l.customLogger.Writer(), item.text)
} else {
item.color.Fprint(l.customLogger.Writer(), item.text)
}
}
}
l.err = l.err[:1]
}
}
func (l *logger) SetLevel(lv Level) {
l.Level = lv
}