/
logger.go
225 lines (206 loc) · 5.2 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
218
219
220
221
222
223
224
225
package fwncs
import (
"fmt"
"html/template"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"
)
type ILogger interface {
Debug(v ...interface{})
Info(v ...interface{})
Warning(v ...interface{})
Error(v ...interface{})
Critical(v ...interface{})
Write(b []byte) (int, error)
// Copy is logger copy instance
Copy() ILogger
// Skip is logger skip plus instance
Skip(n int) ILogger
ChangeFormatType(formatType LogFormatType) ILogger
}
type logger struct {
logTpl *template.Template
timeFmt string
writer io.Writer
logLevel LogLevel
skip int
}
type LogTemplate struct {
Time string
Funcname string
Filename string
Level string
Goroutine string
LineNumber string
Message string
}
const (
// FormatShort time:{{.Time}}\tlevel:{{.Level}}\tmessage:{{.Message}}\n
FormatShort LogFormatType = "time:{{.Time}}\tlevel:{{.Level}}\tmessage:{{.Message}}\n"
// FormatStandard time:{{.Time}}\tlevel:{{.Level}}\tfilename:{{.Filename}}:{{.LineNumber}}\tmessage:{{.Message}}\n
FormatStandard LogFormatType = "time:{{.Time}}\tlevel:{{.Level}}\tfilename:{{.Filename}}:{{.LineNumber}}\tmessage:{{.Message}}\n"
// FormatLong time:{{.Time}}\tlevel:{{.Level}}\tfilename:{{.Filename}}:{{.LineNumber}}\tfuncname:{{.Funcname}}\tmessage:{{.Message}}\n
FormatLong LogFormatType = "time:{{.Time}}\tlevel:{{.Level}}\tfilename:{{.Filename}}:{{.LineNumber}}\tfuncname:{{.Funcname}}\tmessage:{{.Message}}\n"
// FormatDate 2006/01/02
FormatDate LogFormatType = "2006/01/02"
// FormatDatetime 2006/01/02 15:04:05
FormatDatetime LogFormatType = "2006/01/02 15:04:05"
// FormatMillisec 2006/01/02 15:04:05.00000
FormatMillisec LogFormatType = "2006/01/02 15:04:05.00000"
)
type LogFormatType string
func (f LogFormatType) String() string {
return string(f)
}
func ConvertLogFmt(fmt string) LogFormatType {
switch strings.ToLower(fmt) {
case "short":
return FormatShort
case "standard":
return FormatStandard
case "long":
return FormatLong
default:
return FormatStandard
}
}
func ConvertTimeFmt(fmt string) LogFormatType {
switch strings.ToLower(fmt) {
case "date":
return FormatDate
case "datetime":
return FormatDatetime
case "millisec":
return FormatMillisec
default:
return FormatDatetime
}
}
var Log ILogger = &logger{}
var _ io.Writer = &logger{}
var DefaultLogger = NewLogger(os.Stderr, FormatShort, FormatDatetime)
func NewLogger(writer io.Writer, formatType LogFormatType, timeFmtTyp LogFormatType) ILogger {
logLevel := ConvertLevel(os.Getenv("LOG_LEVEL"))
t, err := template.New("log").Parse(formatType.String())
if err != nil {
panic(err)
}
return &logger{
logTpl: t,
timeFmt: timeFmtTyp.String(),
writer: writer,
logLevel: logLevel,
skip: 2,
}
}
type LogLevel int
func (l LogLevel) String() string {
switch l {
case Critical:
return "CRIT"
case Error:
return "ERR "
case Warn:
return "WARN"
case Info:
return "INFO"
case Debug:
return "DBG "
}
return "!!PANIC"
}
func ConvertLevel(level string) LogLevel {
switch strings.ToLower(level) {
case "critical", "crit":
return Critical
case "error", "err":
return Error
case "warn", "warnign":
return Warn
case "info", "prod":
return Info
case "debug", "dbg":
return Debug
default:
return Warn
}
}
const (
Critical LogLevel = iota
Error
Warn
Info
Debug
)
func (l *logger) isEnabledLevel(level LogLevel) bool {
return level <= l.logLevel
}
func (l *logger) print(loglevel LogLevel, v ...interface{}) {
if l.isEnabledLevel(loglevel) {
pc, fileName, lineNumber, ok := runtime.Caller(l.skip)
if !ok {
return
}
funcName := runtime.FuncForPC(pc).Name()
funcName = funcName[strings.LastIndex(funcName, ".")+1:]
fileName = fileName[strings.LastIndex(fileName, "/")+1:]
now := time.Now().Format(l.timeFmt)
print := fmt.Sprint(v...)
prints := strings.Split(print, "\n")
for _, p := range prints {
if strings.TrimSpace(p) != "" {
d := &LogTemplate{
Time: now,
Funcname: funcName,
Filename: fileName,
Level: loglevel.String(),
LineNumber: strconv.Itoa(lineNumber),
Goroutine: strconv.Itoa(runtime.NumGoroutine()),
Message: p,
}
l.logTpl.Execute(l.writer, d)
}
}
}
}
func (l *logger) Debug(v ...interface{}) { l.print(Debug, v...) }
func (l *logger) Info(v ...interface{}) { l.print(Info, v...) }
func (l *logger) Warning(v ...interface{}) { l.print(Warn, v...) }
func (l *logger) Error(v ...interface{}) { l.print(Error, v...) }
func (l *logger) Critical(v ...interface{}) { l.print(Critical, v...) }
func (l *logger) Write(b []byte) (int, error) { return l.writer.Write(b) }
func (l *logger) Skip(n int) ILogger {
return &logger{
logTpl: l.logTpl,
timeFmt: l.timeFmt,
writer: l.writer,
logLevel: l.logLevel,
skip: l.skip + n,
}
}
func (l *logger) Copy() ILogger {
return &logger{
logTpl: l.logTpl,
timeFmt: l.timeFmt,
writer: l.writer,
logLevel: l.logLevel,
skip: l.skip + 1,
}
}
func (l *logger) ChangeFormatType(formatType LogFormatType) ILogger {
t, err := template.New("log").Parse(formatType.String())
if err != nil {
t = l.logTpl
}
return &logger{
logTpl: t,
timeFmt: l.timeFmt,
writer: l.writer,
logLevel: l.logLevel,
skip: l.skip,
}
}