-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
239 lines (199 loc) · 5.43 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package tools
import (
"fmt"
"reflect"
"runtime"
"runtime/debug"
"time"
)
const (
logLevelInfo = "info"
logLevelDebug = "debug"
logLevelWarn = "warn"
logLevelError = "error"
)
// 调用SetLogger方法设置logger
var logger Logger
var baseLogBlock func(timeStr string, level string, funcName string, line int) (format string, args []interface{})
// Logger go-tools支持集成其他logger(调用SetLogger方法设置). 前提是要支持下方定义的Logger接口.
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// Log 带行号输出
func Log(args ...interface{}) {
log("", false, func() {
fmt.Print(args...)
}, args...)
}
// Logln 带行号换行输出
func Logln(args ...interface{}) {
log("", true, func() {
fmt.Println(args...)
}, args...)
}
// Logf 带行号格式输出
func Logf(format string, args ...interface{}) {
log(format, false, func() {
fmt.Printf(format, args...)
}, args...)
}
func logStackInfo(args ...interface{}) (condition bool, newA []interface{}, pc uintptr, line int, ok bool, logLevel string) {
newA = args
currentSkip := 3
condition = true
o := new(logOptions)
o.logLevel = logLevelInfo
o.LogCondition = &condition
for i := 0; i < len(newA); i++ {
obj := newA[i]
objType := reflect.TypeOf(obj)
// 不是logger用来做判定的类型
if obj == nil || objType != logOptionType {
continue
}
obj.(LogOptionFunc)(o)
if *o.LogCondition {
if o.LogCallerSkip > 0 && pc == 0 {
if line == 0 {
pc, _, line, ok = runtime.Caller(o.LogCallerSkip + currentSkip)
} else {
pc, _, _, ok = runtime.Caller(o.LogCallerSkip + currentSkip)
}
} else if o.LogLineSkip > 0 && line == 0 {
if pc == 0 {
pc, _, line, _ = runtime.Caller(o.LogLineSkip + currentSkip)
} else {
_, _, line, _ = runtime.Caller(o.LogLineSkip + currentSkip)
}
}
}
newA = append(newA[:i], newA[i+1:]...)
i--
}
condition = *o.LogCondition
logLevel = o.logLevel
if condition && pc == 0 && line == 0 {
pc, _, line, ok = runtime.Caller(currentSkip)
}
return
}
func logFormat(args ...interface{}) string {
formatStr := ""
dataArr, ok := args[0].([]interface{})
if !ok {
formatStr += "%v, "
}
for _, data := range dataArr {
switch data.(type) {
case nil, bool, int, int8, int16, int32, int64, string, error, float32, float64:
formatStr += "%v, "
default:
formatStr += "%#v, "
}
}
return formatStr
}
func formatWithValues(pc uintptr, level string, codeLine int, format string, args ...interface{}) (string, []interface{}) {
funName := runtime.FuncForPC(pc).Name()
timeStr := time.Now().Format("2006-01-02 15:04:05")
baseFormat := "%s__%s__%s__第%d行__: "
slice := []interface{}{timeStr, level, funName, codeLine}
if baseLogBlock != nil {
baseFormat, slice = baseLogBlock(timeStr, level, funName, codeLine)
}
slice = append(slice, args...)
return baseFormat + format, slice
}
// SetLogger 设置日志输出实例
func SetLogger(yourLogger Logger) {
logger = yourLogger
}
// SetBaseFormat 设置基本信息相关format格式, 默认为: "%s__%s__%s__第%d行__: " 和 []interface{}{timeStr, level, funName, codeLine}
func SetBaseFormat(block func(timeStr string, level string, funcName string, line int) (format string, args []interface{})) {
baseLogBlock = block
}
func log(format string, ln bool, ifErr func(), a ...interface{}) {
condition, newA, pc, codeLine, ok, level := logStackInfo(a...)
if !condition {
return
}
if !ok {
ifErr()
return
}
if format == "" {
format = logFormat(newA) + format
}
if ln {
format = format + "\n"
}
finalFormat, slice := formatWithValues(pc, level, codeLine, format, newA...)
if logger == nil {
fmt.Printf(finalFormat, slice...)
if level == logLevelError {
debug.PrintStack()
}
return
}
switch level {
case logLevelInfo:
logger.Infof(finalFormat, slice...)
case logLevelDebug:
logger.Debugf(finalFormat, slice...)
case logLevelWarn:
logger.Warnf(finalFormat, slice...)
case logLevelError:
logger.Errorf(finalFormat, slice...)
}
}
// Debug debug
func Debug(args ...interface{}) {
log("", true, func() {
fmt.Println(args...)
}, append(args, logLevel(logLevelDebug))...)
}
// Info info
func Info(args ...interface{}) {
log("", true, func() {
fmt.Println(args...)
}, append(args, logLevel(logLevelInfo))...)
}
// Warn warn
func Warn(args ...interface{}) {
log("", true, func() {
fmt.Println(args...)
}, append(args, logLevel(logLevelWarn))...)
}
// Error error
func Error(args ...interface{}) {
log("", true, func() {
fmt.Println(args...)
}, append(args, logLevel(logLevelError))...)
}
// Debugf debug with template
func Debugf(template string, args ...interface{}) {
log(template, false, func() {
fmt.Printf(template, args...)
}, append(args, logLevel(logLevelDebug))...)
}
// Infof info with template
func Infof(template string, args ...interface{}) {
log(template, false, func() {
fmt.Printf(template, args...)
}, append(args, logLevel(logLevelInfo))...)
}
// Warnf warn with template
func Warnf(template string, args ...interface{}) {
log(template, false, func() {
fmt.Printf(template, args...)
}, append(args, logLevel(logLevelWarn))...)
}
// Errorf error with template
func Errorf(template string, args ...interface{}) {
log(template, false, func() {
fmt.Printf(template, args...)
}, append(args, logLevel(logLevelError))...)
}