-
Notifications
You must be signed in to change notification settings - Fork 0
/
logig_zap.go
83 lines (67 loc) · 2.36 KB
/
logig_zap.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
package logig_zap
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type LogigZap struct {
*zap.Logger
}
func NewLogigZap(logLevel string) *LogigZap {
c := zap.NewProductionConfig()
level := zapcore.InfoLevel
err := level.Set(logLevel)
if err != nil {
return &LogigZap{zap.NewNop()}
}
c.Level = zap.NewAtomicLevelAt(level)
c.DisableCaller = true
c.EncoderConfig.TimeKey = "time"
c.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
c.DisableStacktrace = true
l, err := c.Build()
if err != nil {
return &LogigZap{zap.NewNop()}
}
return &LogigZap{l}
}
type zapFields map[string]interface{}
func (zf zapFields) zapOption() zap.Option {
f := make([]zap.Field, len(zf))
var c int
for k, v := range zf {
f[c] = zap.Any(k, v)
c++
}
return zap.Fields(f...)
}
func (l *LogigZap) Debug(fields map[string]interface{}, str string) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Debug(str)
}
func (l *LogigZap) Info(fields map[string]interface{}, str string) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Info(str)
}
func (l *LogigZap) Warn(fields map[string]interface{}, str string) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Warn(str)
}
func (l *LogigZap) Error(fields map[string]interface{}, str string) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Error(str)
}
func (l *LogigZap) Fatal(fields map[string]interface{}, str string) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Fatal(str)
}
func (l *LogigZap) Debugf(fields map[string]interface{}, format string, args ...interface{}) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Debug(fmt.Sprintf(format, args...))
}
func (l *LogigZap) Infof(fields map[string]interface{}, format string, args ...interface{}) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Info(fmt.Sprintf(format, args...))
}
func (l *LogigZap) Warnf(fields map[string]interface{}, format string, args ...interface{}) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Warn(fmt.Sprintf(format, args...))
}
func (l *LogigZap) Errorf(fields map[string]interface{}, format string, args ...interface{}) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Error(fmt.Sprintf(format, args...))
}
func (l *LogigZap) Fatalf(fields map[string]interface{}, format string, args ...interface{}) {
l.Logger.WithOptions(zapFields(fields).zapOption()).Fatal(fmt.Sprintf(format, args...))
}