Skip to content

Commit

Permalink
lib:数据库日志配置和打印规则调整
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Jun 26, 2024
1 parent ddcc80d commit a8a4cec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
32 changes: 28 additions & 4 deletions lib/db/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ package db
// enable: false
// driver_name: mysql
// auto_migrate: true
// log_level: warn
// logger:
// level: "warn"
// slow_threshold: 100
// skip_caller_lookup: true
// ignore_record_not_found_error: true
// colorful: false
// connection_pool:
// # 最大开启连接数
// max_open_conn_count: 100
Expand Down Expand Up @@ -53,9 +58,19 @@ package db
//
// auto_migrate = false
//
// # 日志级别 silent | info | warn | error
// # 日志配置
//
// log_level = "info"
// [db_conf.logger]
//
// level = "warn"
//
// slow_threshold = 100
//
// skip_caller_lookup = true
//
// ignore_record_not_found_error = true
//
// colorful = false
//
// # ::数据库连接池配置::
//
Expand Down Expand Up @@ -224,7 +239,7 @@ type Conf struct {
Enable bool `yaml:"enable" toml:"enable" json:"enable" default:"false"` //是否启用
DriverName string `yaml:"driver_name" toml:"driver_name" json:"driver_name" default:"mysql"` //数据库类型
AutoMigrate bool `yaml:"auto_migrate" toml:"auto_migrate" json:"auto_migrate" default:"false"` //是否自动同步表结构
LogLevel string `yaml:"log_level" toml:"log_level" json:"log_level" default:"info"` //日志级别
Logger Logger `yaml:"logger" toml:"logger" json:"logger"` //日志配置
ConnectionPool ConnectionPoolConf `yaml:"connection_pool" toml:"connection_pool" json:"connection_pool"` //连接池配置
Mysql MysqlConf `yaml:"mysql" toml:"mysql" json:"mysql"` //mysql配置
Postgres PostgresConf `yaml:"postgres" toml:"postgres" json:"postgres"` //postgres配置
Expand All @@ -233,6 +248,15 @@ type Conf struct {
Clickhouse ClickhouseConf `yaml:"clickhouse" toml:"clickhouse" json:"clickhouse"` //clickhouse配置
}

// Logger 日志配置
type Logger struct {
Level string `yaml:"level" toml:"level" json:"level" default:"info"` //日志级别(info,warn,error,silent)
SlowThreshold int `yaml:"slow_threshold" toml:"slow_threshold" json:"slow_threshold" default:"100"` //慢日志阈值(毫秒)
SkipCallerLookup bool `yaml:"skip_caller_lookup" toml:"skip_caller_lookup" json:"skip_caller_lookup" default:"false"` //是否跳过调用者检查
IgnoreRecordNotFoundError bool `yaml:"ignore_record_not_found_error" toml:"ignore_record_not_found_error" json:"ignore_record_not_found_error" default:"false"` //忽略未找到记录错误
Colorful bool `yaml:"colorful" toml:"colorful" json:"colorful" default:"false"` //是否带颜色打印
}

// ConnectionPoolConf 连接池配置
type ConnectionPoolConf struct {
MaxOpenConnCount int `yaml:"max_open_conn_count" toml:"max_open_conn_count" json:"max_open_conn_count" default:"100"` //最大开启连接数
Expand Down
23 changes: 16 additions & 7 deletions lib/db/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package db

import (
"errors"
"time"

"go.uber.org/zap"
"golang.org/x/net/context"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"time"
)

type ZapLoggerForGorm struct {
Expand All @@ -15,11 +16,12 @@ type ZapLoggerForGorm struct {
SlowThreshold time.Duration
SkipCallerLookup bool
IgnoreRecordNotFoundError bool
Colorful bool
}

func NewZapLoggerForGorm(zapLogger *zap.Logger, conf Conf) ZapLoggerForGorm {
var logLevel gormLogger.LogLevel
switch conf.LogLevel {
switch conf.Logger.Level {
case "info":
logLevel = gormLogger.Info
case "warn":
Expand All @@ -31,13 +33,20 @@ func NewZapLoggerForGorm(zapLogger *zap.Logger, conf Conf) ZapLoggerForGorm {
default:
logLevel = gormLogger.Info
}
return ZapLoggerForGorm{
zfg := ZapLoggerForGorm{
ZapLogger: zapLogger,
LogLevel: logLevel,
SlowThreshold: 100 * time.Millisecond,
SkipCallerLookup: false,
IgnoreRecordNotFoundError: true,
SkipCallerLookup: conf.Logger.SkipCallerLookup,
IgnoreRecordNotFoundError: conf.Logger.IgnoreRecordNotFoundError,
Colorful: conf.Logger.SkipCallerLookup,
}

if conf.Logger.SlowThreshold != 0 {
zfg.SlowThreshold = time.Duration(conf.Logger.SlowThreshold) * time.Millisecond
}

return zfg
}

func (zg ZapLoggerForGorm) SetAsDefault() {
Expand Down Expand Up @@ -79,10 +88,10 @@ func (zg ZapLoggerForGorm) Trace(_ context.Context, begin time.Time, fc func() (
switch {
case err != nil && zg.LogLevel >= gormLogger.Error && (!zg.IgnoreRecordNotFoundError || !errors.Is(err, gorm.ErrRecordNotFound)):
sql, rows := fc()
zg.ZapLogger.Error("trace", zap.Error(err), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql))
zg.ZapLogger.Error("record not found", zap.Error(err), zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql))
case zg.SlowThreshold != 0 && elapsed > zg.SlowThreshold && zg.LogLevel >= gormLogger.Warn:
sql, rows := fc()
zg.ZapLogger.Warn("trace", zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql))
zg.ZapLogger.Warn("slow sql", zap.Duration("elapsed", elapsed), zap.Duration("threshold", zg.SlowThreshold), zap.Int64("rows", rows), zap.String("sql", sql))
case zg.LogLevel >= gormLogger.Info:
sql, rows := fc()
zg.ZapLogger.Debug("trace", zap.Duration("elapsed", elapsed), zap.Int64("rows", rows), zap.String("sql", sql))
Expand Down
4 changes: 3 additions & 1 deletion orm/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ var (
Enable: true,
DriverName: "mysql",
AutoMigrate: true,
LogLevel: "debug",
Logger: db.Logger{
Level: "debug",
},
ConnectionPool: db.ConnectionPoolConf{
MaxOpenConnCount: 100,
MaxIdleConnCount: 10,
Expand Down

0 comments on commit a8a4cec

Please sign in to comment.