This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
217 lines (195 loc) · 5.14 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package logger
import (
"fmt"
stdlog "log"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/fatih/color"
)
// here info about code location with a string representation formatted as
// <dir>/<file>.go@<line>:<package>.<function>()
func here(skip ...int) loc {
sk := 1
if len(skip) > 0 && skip[0] > 1 {
sk = skip[0]
}
pc, fileName, fileLine, ok := runtime.Caller(sk)
fn := runtime.FuncForPC(pc)
var res loc
defer func() {
if res.long != "" {
return
}
res.long = res.FuncName
}()
if !ok {
res.FuncName = "N/A"
return res
}
res.FileName = fileName
res.FileLine = fileLine
res.FuncName = fn.Name()
fileName = filepath.Join(filepath.Base(filepath.Dir(fileName)), filepath.Base(fileName))
res.long = fmt.Sprintf("%s@%d:%s()", fileName, res.FileLine, res.FuncName)
res.short = fmt.Sprintf("%s@%d:%s()", fileName, res.FileLine, strings.TrimLeft(filepath.Ext(res.FuncName), "."))
return res
}
// loc info about code location with a string representation formatted as
// <dir>/<file>.go@<line>:<package>.<function>()
type loc struct {
FuncName string
FileName string
FileLine int
long string
short string
}
func (l loc) String() string {
return l.long
}
// flags, overwrites default flags.
const (
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Ltime // the time in the local time zone: 01:23:23
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
Llongfile // long caller description.
Lshortfile // short caller description.
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
LDebug // level debug
LInfo // level info
LWarn // level warn
LError // level error
LNoColor // do not use colors
hastime = Ldate | Ltime | Lmicroseconds | LUTC
hasloc = Llongfile | Lshortfile
)
// Logger adds word pair logs to the standard logger
type Logger struct {
*stdlog.Logger
flags int
}
// New creates a *Logger. Panics if logger is nil.
func New(logger *stdlog.Logger, flags ...int) (res *Logger) {
if logger == nil {
panic("logger can not be nil")
}
res = new(Logger)
res.Logger = logger
res.flags = logger.Flags()
logger.SetFlags(0)
for _, v := range flags {
res.flags = v | res.flags
}
if res.flags&LNoColor == 0 {
switch {
case res.flags&LError == LError:
logger.SetPrefix(color.New(color.FgHiRed).Sprintf("level=error "))
case res.flags&LWarn == LWarn:
logger.SetPrefix(color.New(color.FgHiYellow).Sprintf("level=warn "))
case res.flags&LDebug == LDebug:
logger.SetPrefix(color.New(color.FgWhite).Sprintf("level=debug "))
case res.flags&LInfo == LInfo:
fallthrough
default:
logger.SetPrefix(color.New(color.FgBlue).Sprintf("level=info "))
}
} else {
switch {
case res.flags&LError == LError:
logger.SetPrefix("level=error ")
case res.flags&LWarn == LWarn:
logger.SetPrefix("level=warn ")
case res.flags&LDebug == LDebug:
logger.SetPrefix("level=debug ")
case res.flags&LInfo == LInfo:
fallthrough
default:
logger.SetPrefix("level=info ")
}
}
return
}
// Fatalln .
func (l *Logger) Fatalln(v ...interface{}) {
l.Logger.Println(l.sprint(v...))
}
// Panicln .
func (l *Logger) Panicln(v ...interface{}) {
l.Logger.Println(l.sprint(v...))
}
// Println needs pairs.
func (l *Logger) Println(v ...interface{}) {
l.Logger.Println(l.sprint(v...))
}
func (l *Logger) sprint(v ...interface{}) string {
var parts []string
{
h := l.headers()
if h != "" {
parts = append(parts, h, "\n ")
}
}
for i := 0; i < len(v)-1; i += 2 {
k, v := fmt.Sprint(v[i]), fmt.Sprint(v[i+1])
if strings.ContainsAny(k, " \n\r\t\"") {
k = fmt.Sprintf("%q", k)
}
if strings.ContainsAny(v, " \n\r\t\"") {
v = fmt.Sprintf("%q", v)
}
parts = append(parts, fmt.Sprintf("%s=%s", k, v))
}
if len(v)&1 == 1 {
parts = append(parts, fmt.Sprintf("msg=%q", v[len(v)-1]))
}
return strings.Join(parts, " ")
}
func (l *Logger) headers() string {
var parts []string
if l.flags&hastime > 0 {
now := time.Now()
if l.flags&LUTC == LUTC {
now = now.UTC()
}
var format string
if l.flags&Ldate == Ldate {
format = "2006-01-02"
}
if l.flags&Ltime == Ltime {
if format != "" {
format += "T"
}
format += "15:04:05"
if l.flags&Lmicroseconds == Lmicroseconds {
format += ".000000"
}
}
if format == "" {
format = time.RFC3339
}
if l.flags&LNoColor == 0 {
parts = append(parts, color.New(color.FgGreen).Sprint("time="+now.Format(format)))
} else {
parts = append(parts, "time="+now.Format(format))
}
}
if l.flags&hasloc > 0 {
loc := here(2)
switch {
case l.flags&Llongfile == Llongfile:
if l.flags&LNoColor != 0 {
parts = append(parts, "loc="+loc.long)
} else {
parts = append(parts, color.New(color.FgHiMagenta).Sprint("loc="+loc.long))
}
case l.flags&Lshortfile == Lshortfile:
if l.flags&LNoColor != 0 {
parts = append(parts, "loc="+loc.short)
} else {
parts = append(parts, color.New(color.FgHiMagenta).Sprint("loc="+loc.short))
}
}
}
return strings.Join(parts, " ")
}