-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogx.go
76 lines (59 loc) · 1.67 KB
/
logx.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
package zlog
import (
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"go.uber.org/zap"
)
type Field = logx.LogField
func ErrorField(err error) Field {
return logx.Field("error", err)
}
func DataField(data interface{}) Field {
return logx.Field("data", data)
}
func ParamField(data interface{}) Field {
return logx.Field("param", data)
}
type ZapWriter struct {
logger *zap.Logger
}
func NewZapWriter(logger *zap.Logger) logx.Writer {
return &ZapWriter{
logger: logger,
}
}
func (w *ZapWriter) Alert(v interface{}) {
w.logger.Error(fmt.Sprint(v))
}
func (w *ZapWriter) Close() error {
return w.logger.Sync()
}
func (w *ZapWriter) Debug(v interface{}, fields ...logx.LogField) {
w.logger.Debug(fmt.Sprint(v), toZapFields(fields...)...)
}
func (w *ZapWriter) Error(v interface{}, fields ...logx.LogField) {
w.logger.Error(fmt.Sprint(v), toZapFields(fields...)...)
}
func (w *ZapWriter) Info(v interface{}, fields ...logx.LogField) {
w.logger.Info(fmt.Sprint(v), toZapFields(fields...)...)
}
func (w *ZapWriter) Severe(v interface{}) {
w.logger.Panic(fmt.Sprint(v))
}
// Severef writes v with format into severe log.
func (w *ZapWriter) Slow(v interface{}, fields ...logx.LogField) {
w.logger.Warn(fmt.Sprint(v), toZapFields(fields...)...)
}
func (w *ZapWriter) Stack(v interface{}) {
w.logger.Error(fmt.Sprint(v), zap.Stack("stack"))
}
func (w *ZapWriter) Stat(v interface{}, fields ...logx.LogField) {
w.logger.Info(fmt.Sprint(v), toZapFields(fields...)...)
}
func toZapFields(fields ...logx.LogField) []zap.Field {
zapFields := make([]zap.Field, 0, len(fields))
for _, f := range fields {
zapFields = append(zapFields, zap.Any(f.Key, f.Value))
}
return zapFields
}