-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
78 lines (64 loc) · 2.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
package ezmq
import (
"fmt"
"path/filepath"
"runtime"
"time"
)
type Logger interface {
Debug(v ...interface{})
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
type printLogger struct {
}
func (l *printLogger) Debug(v ...interface{}) {
nv := append([]interface{}{debugLevel, time.Now().Format(time.DateTime), fileLine()}, v...)
fmt.Println(nv...)
}
func (l *printLogger) Info(v ...interface{}) {
nv := append([]interface{}{infoLevel, time.Now().Format(time.DateTime), fileLine()}, v...)
fmt.Println(nv...)
}
func (l *printLogger) Warn(v ...interface{}) {
nv := append([]interface{}{warnLevel, time.Now().Format(time.DateTime), fileLine()}, v...)
fmt.Println(nv...)
}
func (l *printLogger) Error(v ...interface{}) {
nv := append([]interface{}{errorLevel, time.Now().Format(time.DateTime), fileLine()}, v...)
fmt.Println(nv...)
}
func (l *printLogger) Debugf(f string, args ...interface{}) {
fmt.Printf(fmt.Sprintf("%s %s %s %s", debugLevel, time.Now().Format(time.DateTime), fileLine(), f), args...)
}
func (l *printLogger) Infof(f string, args ...interface{}) {
fmt.Printf(fmt.Sprintf("%s %s %s %s", infoLevel, time.Now().Format(time.DateTime), fileLine(), f), args...)
}
func (l *printLogger) Warnf(f string, args ...interface{}) {
fmt.Printf(fmt.Sprintf("%s %s %s %s", warnLevel, time.Now().Format(time.DateTime), fileLine(), f), args...)
}
func (l *printLogger) Errorf(f string, args ...interface{}) {
fmt.Printf(fmt.Sprintf("%s %s %s %s", errorLevel, time.Now().Format(time.DateTime), fileLine(), f), args...)
}
const (
debugLevel = "DBUG"
infoLevel = "INFO"
warnLevel = "WARN"
errorLevel = "EERO"
)
var filePrefixFunc = func() string {
abs, _ := filepath.Abs(".")
return filepath.Dir(abs)
}()
func fileLine() string {
pc, absPath, line, _ := runtime.Caller(3)
caller := runtime.FuncForPC(pc)
relPath := absPath[len(filePrefixFunc)+1:]
simpleCaller := caller.Name()[len("ezmq."):]
return fmt.Sprintf("%s:%v %s(): ", relPath, line, simpleCaller)
}