github.heygears.com/openimsdk/tools@v0.0.49/log/sql_logger.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package log 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/pkg/errors" 23 "gorm.io/gorm" 24 gormLogger "gorm.io/gorm/logger" 25 gormUtils "gorm.io/gorm/utils" 26 ) 27 28 const nanosecondsToMilliseconds = 1e6 29 30 type SqlLogger struct { 31 LogLevel gormLogger.LogLevel 32 IgnoreRecordNotFoundError bool 33 SlowThreshold time.Duration 34 } 35 36 func NewSqlLogger(logLevel gormLogger.LogLevel, ignoreRecordNotFoundError bool, slowThreshold time.Duration) *SqlLogger { 37 return &SqlLogger{ 38 LogLevel: logLevel, 39 IgnoreRecordNotFoundError: ignoreRecordNotFoundError, 40 SlowThreshold: slowThreshold, 41 } 42 } 43 44 func (l *SqlLogger) LogMode(logLevel gormLogger.LogLevel) gormLogger.Interface { 45 newLogger := *l 46 newLogger.LogLevel = logLevel 47 return &newLogger 48 } 49 50 func (SqlLogger) Info(ctx context.Context, msg string, args ...any) { 51 ZInfo(ctx, msg, "args", args) 52 } 53 54 func (SqlLogger) Warn(ctx context.Context, msg string, args ...any) { 55 ZWarn(ctx, msg, nil, "args", args) 56 } 57 58 func (SqlLogger) Error(ctx context.Context, msg string, args ...any) { 59 var err error = nil 60 kvList := make([]any, 0) 61 v, ok := args[0].(error) 62 if ok { 63 err = v 64 for i := 1; i < len(args); i++ { 65 kvList = append(kvList, fmt.Sprintf("args[%v]", i), args[i]) 66 } 67 } else { 68 for i := 0; i < len(args); i++ { 69 kvList = append(kvList, fmt.Sprintf("args[%v]", i), args[i]) 70 } 71 } 72 ZError(ctx, msg, err, kvList...) 73 } 74 75 func (l *SqlLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) { 76 if l.LogLevel <= gormLogger.Silent { 77 return 78 } 79 elapsed := time.Since(begin) 80 switch { 81 case err != nil && l.LogLevel >= gormLogger.Error && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError): 82 sql, rows := fc() 83 if rows == -1 { 84 ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "sql", sql) 85 } else { 86 ZError(ctx, "sql exec detail", err, "gorm", gormUtils.FileWithLineNum(), "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "rows", rows, "sql", sql) 87 } 88 case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= gormLogger.Warn: 89 sql, rows := fc() 90 slowLog := fmt.Sprintf("SLOW SQL >= %v", l.SlowThreshold) 91 if rows == -1 { 92 ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), "slow sql", slowLog, "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "sql", sql) 93 } else { 94 ZWarn(ctx, "sql exec detail", nil, "gorm", gormUtils.FileWithLineNum(), "slow sql", slowLog, "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "rows", rows, "sql", sql) 95 } 96 case l.LogLevel == gormLogger.Info: 97 sql, rows := fc() 98 if rows == -1 { 99 ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "sql", sql) 100 } else { 101 ZDebug(ctx, "sql exec detail", "gorm", gormUtils.FileWithLineNum(), "elapsed time", fmt.Sprintf("%f(ms)", float64(elapsed.Nanoseconds())/nanosecondsToMilliseconds), "rows", rows, "sql", sql) 102 } 103 } 104 }