-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
163 lines (124 loc) · 3.78 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
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
package log
import (
"strings"
"github.com/sirupsen/logrus"
"github.com/o-kit/micro-kit/misc/stack"
)
// todo 需要添加日志分割功能,使用Hook
var std = NewEx(1)
type Entry struct {
*logrus.Entry
depth int
}
func New() *Entry {
return NewEx(0)
}
func NewEx(depth int) *Entry {
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
DisableColors: false,
}
return &Entry{
Entry: logrus.NewEntry(logger),
depth: depth,
}
}
func (e *Entry) SetLevel(level string) {
switch strings.ToLower(level) {
case "debug":
e.Entry.Logger.Level = logrus.DebugLevel
case "info":
e.Entry.Logger.Level = logrus.InfoLevel
case "warn":
e.Entry.Logger.Level = logrus.WarnLevel
case "error":
e.Entry.Logger.Level = logrus.ErrorLevel
case "fatal":
e.Entry.Logger.Level = logrus.FatalLevel
}
}
func (e *Entry) Printf(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Printf(format, args...)
}
func (e *Entry) Println(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth)).Println(args...)
}
func (e *Entry) Debug(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Debug(args...)
}
func (e *Entry) Debugf(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Debugf(format, args...)
}
func (e *Entry) Info(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Info(args...)
}
func (e *Entry) Infof(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Infof(format, args...)
}
func (e *Entry) Warn(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Warn(args...)
}
func (e *Entry) Warnf(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Warnf(format, args...)
}
func (e *Entry) Error(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Error(args...)
}
func (e *Entry) Errorf(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Errorf(format, args...)
}
func (e *Entry) Fatal(args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Fatal(args...)
}
func (e *Entry) Fatalf(format string, args ...interface{}) {
e.Entry.WithField("call", stack.String(e.depth+1)).Fatalf(format, args...)
}
func (e *Entry) WithField(key string, value interface{}) *Entry {
return &Entry{e.Entry.WithField(key, value), e.depth}
}
func (e *Entry) WithFields(fields map[string]interface{}) *Entry {
return &Entry{e.Entry.WithFields(fields), e.depth}
}
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
func SetLevel(level string) {
std.SetLevel(level)
}
func Printf(format string, args ...interface{}) {
std.Printf(format, args...)
}
func Println(args ...interface{}) {
std.Println(args...)
}
func Debug(args ...interface{}) {
std.Debug(args...)
}
func Debugf(format string, args ...interface{}) {
std.Debugf(format, args...)
}
func Info(args ...interface{}) {
std.Info(args...)
}
func Infof(layout string, args ...interface{}) {
std.Infof(layout, args...)
}
func Warn(args ...interface{}) {
std.Warn(args...)
}
func Warnf(format string, args ...interface{}) {
std.Warnf(format, args...)
}
func Error(args ...interface{}) {
std.Error(args...)
}
func Errorf(format string, args ...interface{}) {
std.Errorf(format, args...)
}
func Fatal(args ...interface{}) {
std.Fatal(args...)
}
func Fatalf(format string, args ...interface{}) {
std.Fatalf(format, args...)
}