/
log.go
55 lines (45 loc) · 1.24 KB
/
log.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
package eureka
import (
. "log"
"runtime"
"strings"
"fmt"
)
const (
LevelDebug = 1
LevelInfo = 2
LevelError = 3
)
type LogFunc func(level int, format string, a ...interface{})
var log LogFunc
func SetLogger(logFunc LogFunc) {
log = logFunc
}
func (t LogFunc) Debugf(format string, a ...interface{}) {
t(LevelDebug, format, a...)
}
func (t LogFunc) Infof(format string, a ...interface{}) {
t(LevelInfo, format, a...)
}
func (t LogFunc) Errorf(format string, a ...interface{}) {
t(LevelError, format, a...)
}
func init() {
if log == nil {
log = func(level int, format string, a ...interface{}) {
switch level {
case LevelDebug:
format = "[debug] " + format
case LevelInfo:
format = "[info] " + format
case LevelError:
format = "[error] " + format
}
funcName, file, line, _ := runtime.Caller(2)
fullFuncName := runtime.FuncForPC(funcName).Name()
arr := strings.Split(fullFuncName, "/")
arrFile := strings.Split(file, "/")
Printf(fmt.Sprintf("%s %s:%d ", arr[len(arr)-1], arrFile[len(arrFile)-1], line)+format, a...)
}
}
}