Skip to content

Commit

Permalink
Merge pull request #21 from askuy/feature/slowlog20240625
Browse files Browse the repository at this point in the history
去除无数据的warning日志
  • Loading branch information
askuy authored Jun 27, 2024
2 parents 8b48aa1 + 618154b commit ffda456
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con
next(db)
cost := time.Since(beg)

loggerKeys := transport.CustomContextKeys()

var fields = make([]elog.Field, 0, 15+transport.CustomContextKeysLength())
event := "normal"
fields = append(fields,
Expand All @@ -85,14 +83,13 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con
if config.EnableAccessInterceptorRes {
fields = append(fields, elog.Any("res", db.Statement.Dest))
}

// 开启了链路,那么就记录链路id
if etrace.IsGlobalTracerRegistered() {
fields = append(fields, elog.FieldTid(etrace.ExtractTraceID(db.Statement.Context)))
}

// 支持自定义log
for _, key := range loggerKeys {
for _, key := range transport.CustomContextKeys() {
if value := getContextValue(db.Statement.Context, key); value != "" {
fields = append(fields, elog.FieldCustomKeyValue(key, value))
}
Expand All @@ -113,10 +110,17 @@ func metricInterceptor(compName string, dsn *manager.DSN, op string, config *con
elog.FieldErr(db.Error),
)
if errors.Is(db.Error, ErrRecordNotFound) {
logger.Warn("access", fields...)
// 这种日志可能很多,也没必要,只有开启的时候,或者慢日志的时候记录
if config.EnableAccessInterceptor || isSlowLog {
logger.Warn("access", fields...)
}
emetric.ClientHandleCounter.Inc(emetric.TypeGorm, compName, dsn.DBName+"."+db.Statement.Table, dsn.Addr, "Empty")
return
}
// 如果用户没开启req,那么错误必记录Req
if !config.EnableAccessInterceptorReq {
fields = append(fields, elog.String("req", logSQL(db, true)))
}
logger.Error("access", fields...)
emetric.ClientHandleCounter.Inc(emetric.TypeGorm, compName, dsn.DBName+"."+db.Statement.Table, dsn.Addr, "Error")
return
Expand Down

0 comments on commit ffda456

Please sign in to comment.