1 Star 2 Fork 0

falling-ts/gower

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db.go 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
falling-ts 提交于 2024-11-28 23:06 . feat 包域名修改为 gitee.com
package logger
import (
"context"
"fmt"
"time"
"gitee.com/falling-ts/gower/services"
"go.uber.org/zap"
"gorm.io/gorm/logger"
)
type DB struct {
*Service
}
// Set 设置 DBLogger
func (d *DB) Set(arg any) services.DBLogger {
switch arg.(type) {
case *Service:
d.Service = arg.(*Service)
case services.DBLogger:
d.Service.DBLogger = arg.(services.DBLogger)
}
return d
}
func (d *DB) LogMode(logger.LogLevel) logger.Interface {
return d
}
func (d *DB) Info(ctx context.Context, msg string, data ...interface{}) {
_ = ctx
d.Service.Logger.Debug("DB Debug", zap.String("db", fmt.Sprintf(msg, data...)))
}
func (d *DB) Warn(ctx context.Context, msg string, data ...interface{}) {
_ = ctx
d.Service.Logger.Warn("DB Warn", zap.String("db", fmt.Sprintf(msg, data...)))
}
func (d *DB) Error(ctx context.Context, msg string, data ...interface{}) {
_ = ctx
d.Service.Logger.Error("DB Error",
zap.String("db", fmt.Sprintf(msg, data...)),
zap.Stack("stack"))
}
func (d *DB) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
_ = ctx
elapsed := time.Since(begin)
switch {
case err != nil && d.Service.Core().Enabled(zap.ErrorLevel):
sql, rows := fc()
d.Service.Logger.Error("Error Trace", zap.Error(err), zap.String("sql", sql), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows))
case elapsed > 200*time.Millisecond && d.Service.Core().Enabled(zap.WarnLevel):
sql, rows := fc()
d.Service.Logger.Warn("Slow Trace", zap.String("sql", sql), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows))
case d.Service.Core().Enabled(zap.DebugLevel):
sql, rows := fc()
d.Service.Logger.Debug("Debug Trace", zap.String("sql", sql), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows))
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/falling-ts/gower.git
git@gitee.com:falling-ts/gower.git
falling-ts
gower
gower
v0.9.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385