This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgz.go
189 lines (167 loc) · 5.11 KB
/
gz.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gormzap
import (
"context"
"errors"
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/utils"
)
// Logger logger for gorm2
type Logger struct {
log *zap.Logger
logger.Config
customFields []func(ctx context.Context) zap.Field
}
// Option logger/recover option
type Option func(l *Logger)
// WithCustomFields optional custom field
func WithCustomFields(fields ...func(ctx context.Context) zap.Field) Option {
return func(l *Logger) {
l.customFields = fields
}
}
// WithConfig optional custom logger.Config
func WithConfig(cfg logger.Config) Option {
return func(l *Logger) {
l.Config = cfg
}
}
// SetGormDBLogger set db logger
func SetGormDBLogger(db *gorm.DB, l logger.Interface) {
db.Logger = l
}
// New logger form gorm2
func New(zapLogger *zap.Logger, opts ...Option) logger.Interface {
l := &Logger{
log: zapLogger,
Config: logger.Config{
SlowThreshold: 200 * time.Millisecond,
Colorful: false,
IgnoreRecordNotFoundError: false,
LogLevel: logger.Warn,
},
}
for _, opt := range opts {
opt(l)
}
return l
}
// LogMode log mode
func (l *Logger) LogMode(level logger.LogLevel) logger.Interface {
newLogger := *l
newLogger.LogLevel = level
return &newLogger
}
// Info print info
func (l Logger) Info(ctx context.Context, msg string, args ...interface{}) {
if l.LogLevel >= logger.Info {
l.log.Sugar().Debugf(msg, append([]interface{}{utils.FileWithLineNum()}, args...)...)
}
}
// Warn print warn messages
func (l Logger) Warn(ctx context.Context, msg string, args ...interface{}) {
if l.LogLevel >= logger.Warn {
l.log.Sugar().Warnf(msg, append([]interface{}{utils.FileWithLineNum()}, args...)...)
}
}
// Error print error messages
func (l Logger) Error(ctx context.Context, msg string, args ...interface{}) {
if l.LogLevel >= logger.Error {
l.log.Sugar().Errorf(msg, append([]interface{}{utils.FileWithLineNum()}, args...)...)
}
}
// Trace print sql message
func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
if l.LogLevel <= logger.Silent {
return
}
fields := make([]zap.Field, 0, 6+len(l.customFields))
elapsed := time.Since(begin)
switch {
case err != nil && l.LogLevel >= logger.Error && (!l.IgnoreRecordNotFoundError || !errors.Is(err, gorm.ErrRecordNotFound)):
for _, customField := range l.customFields {
fields = append(fields, customField(ctx))
}
fields = append(fields,
zap.Error(err),
zap.String("file", utils.FileWithLineNum()),
zap.Duration("latency", elapsed),
)
sql, rows := fc()
if rows == -1 {
fields = append(fields, zap.String("rows", "-"))
} else {
fields = append(fields, zap.Int64("rows", rows))
}
fields = append(fields, zap.String("sql", sql))
l.log.Error("trace", fields...)
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= logger.Warn:
for _, customField := range l.customFields {
fields = append(fields, customField(ctx))
}
fields = append(fields,
zap.Error(err),
zap.String("file", utils.FileWithLineNum()),
zap.String("slow!!!", fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)),
zap.Duration("latency", elapsed),
)
sql, rows := fc()
if rows == -1 {
fields = append(fields, zap.String("rows", "-"))
} else {
fields = append(fields, zap.Int64("rows", rows))
}
fields = append(fields, zap.String("sql", sql))
l.log.Warn("trace", fields...)
case l.LogLevel == logger.Info:
for _, customField := range l.customFields {
fields = append(fields, customField(ctx))
}
fields = append(fields,
zap.Error(err),
zap.String("file", utils.FileWithLineNum()),
zap.Duration("latency", elapsed),
)
sql, rows := fc()
if rows == -1 {
fields = append(fields, zap.String("rows", "-"))
} else {
fields = append(fields, zap.Int64("rows", rows))
}
fields = append(fields, zap.String("sql", sql))
l.log.Info("trace", fields...)
}
}
// Immutable custom immutable field
// Deprecated: use Any instead
func Immutable(key string, value interface{}) func(ctx context.Context) zap.Field {
return Any(key, value)
}
// Any custom immutable any field
func Any(key string, value interface{}) func(ctx context.Context) zap.Field {
field := zap.Any(key, value)
return func(ctx context.Context) zap.Field { return field }
}
// String custom immutable string field
func String(key string, value string) func(ctx context.Context) zap.Field {
field := zap.String(key, value)
return func(ctx context.Context) zap.Field { return field }
}
// Int64 custom immutable int64 field
func Int64(key string, value int64) func(ctx context.Context) zap.Field {
field := zap.Int64(key, value)
return func(ctx context.Context) zap.Field { return field }
}
// Uint64 custom immutable uint64 field
func Uint64(key string, value uint64) func(ctx context.Context) zap.Field {
field := zap.Uint64(key, value)
return func(ctx context.Context) zap.Field { return field }
}
// Float64 custom immutable float32 field
func Float64(key string, value float64) func(ctx context.Context) zap.Field {
field := zap.Float64(key, value)
return func(ctx context.Context) zap.Field { return field }
}