mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-04 17:15:58 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
# Conflicts: # cmd/api/main.go # internal/api/a2r/api2rpc.go # internal/apiresp/resp.go # internal/msgtransfer/online_history_msg_handler.go # internal/push/consumer_init.go # pkg/common/db/controller/conversation.go # pkg/common/mw/gin.go # pkg/statistics/statistics.go
This commit is contained in:
@@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"strings"
|
||||
gormUtils "gorm.io/gorm/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -39,16 +40,9 @@ func newMysqlGormDB() (*gorm.DB, error) {
|
||||
}
|
||||
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
||||
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
|
||||
newLogger := logger.New(
|
||||
Writer{},
|
||||
logger.Config{
|
||||
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
|
||||
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
},
|
||||
)
|
||||
sqlLogger := NewSqlLogger(logger.LogLevel(config.Config.Mysql.LogLevel), true, time.Duration(config.Config.Mysql.SlowThreshold)*time.Millisecond)
|
||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
Logger: sqlLogger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -84,14 +78,65 @@ func IsMysqlDuplicateKey(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type Writer struct{}
|
||||
type SqlLogger struct {
|
||||
LogLevel logger.LogLevel
|
||||
IgnoreRecordNotFoundError bool
|
||||
SlowThreshold time.Duration
|
||||
}
|
||||
|
||||
func (w Writer) Printf(format string, args ...interface{}) {
|
||||
s := fmt.Sprintf(format, args...)
|
||||
l := strings.Split(s, "\n")
|
||||
if len(l) == 2 {
|
||||
log.ZDebug(context.Background(), "sql exec detail", "gorm", l[0], "sql", l[1])
|
||||
} else {
|
||||
log.ZDebug(context.Background(), "sql exec detail", "sql", s)
|
||||
func NewSqlLogger(logLevel logger.LogLevel, ignoreRecordNotFoundError bool, slowThreshold time.Duration) *SqlLogger {
|
||||
return &SqlLogger{
|
||||
LogLevel: logLevel,
|
||||
IgnoreRecordNotFoundError: ignoreRecordNotFoundError,
|
||||
SlowThreshold: slowThreshold,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SqlLogger) LogMode(logLevel logger.LogLevel) logger.Interface {
|
||||
newLogger := *l
|
||||
newLogger.LogLevel = logLevel
|
||||
return &newLogger
|
||||
}
|
||||
|
||||
func (SqlLogger) Info(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZInfo(ctx, msg, args)
|
||||
}
|
||||
|
||||
func (SqlLogger) Warn(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZWarn(ctx, msg, nil, args)
|
||||
}
|
||||
|
||||
func (SqlLogger) Error(ctx context.Context, msg string, args ...interface{}) {
|
||||
log.ZError(ctx, msg, nil, args)
|
||||
}
|
||||
|
||||
func (l SqlLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
|
||||
if l.LogLevel <= logger.Silent {
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(begin)
|
||||
switch {
|
||||
case err != nil && l.LogLevel >= logger.Error && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError):
|
||||
sql, rows := fc()
|
||||
if rows == -1 {
|
||||
log.ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= logger.Warn:
|
||||
sql, rows := fc()
|
||||
slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold)
|
||||
if rows == -1 {
|
||||
log.ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), nil, "slow sql", slowLog, "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), nil, "slow sql", slowLog, "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
case l.LogLevel == logger.Info:
|
||||
sql, rows := fc()
|
||||
if rows == -1 {
|
||||
log.ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "sql", sql)
|
||||
} else {
|
||||
log.ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "time(ms)", float64(elapsed.Nanoseconds())/1e6, "rows", rows, "sql", sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user