code.gitea.io/gitea@v1.21.7/models/db/log.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package db 5 6 import ( 7 "fmt" 8 "sync/atomic" 9 10 "code.gitea.io/gitea/modules/log" 11 12 xormlog "xorm.io/xorm/log" 13 ) 14 15 // XORMLogBridge a logger bridge from Logger to xorm 16 type XORMLogBridge struct { 17 showSQL atomic.Bool 18 logger log.Logger 19 } 20 21 // NewXORMLogger inits a log bridge for xorm 22 func NewXORMLogger(showSQL bool) xormlog.Logger { 23 l := &XORMLogBridge{logger: log.GetLogger("xorm")} 24 l.showSQL.Store(showSQL) 25 return l 26 } 27 28 const stackLevel = 8 29 30 // Log a message with defined skip and at logging level 31 func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...any) { 32 l.logger.Log(skip+1, level, format, v...) 33 } 34 35 // Debug show debug log 36 func (l *XORMLogBridge) Debug(v ...any) { 37 l.Log(stackLevel, log.DEBUG, "%s", fmt.Sprint(v...)) 38 } 39 40 // Debugf show debug log 41 func (l *XORMLogBridge) Debugf(format string, v ...any) { 42 l.Log(stackLevel, log.DEBUG, format, v...) 43 } 44 45 // Error show error log 46 func (l *XORMLogBridge) Error(v ...any) { 47 l.Log(stackLevel, log.ERROR, "%s", fmt.Sprint(v...)) 48 } 49 50 // Errorf show error log 51 func (l *XORMLogBridge) Errorf(format string, v ...any) { 52 l.Log(stackLevel, log.ERROR, format, v...) 53 } 54 55 // Info show information level log 56 func (l *XORMLogBridge) Info(v ...any) { 57 l.Log(stackLevel, log.INFO, "%s", fmt.Sprint(v...)) 58 } 59 60 // Infof show information level log 61 func (l *XORMLogBridge) Infof(format string, v ...any) { 62 l.Log(stackLevel, log.INFO, format, v...) 63 } 64 65 // Warn show warning log 66 func (l *XORMLogBridge) Warn(v ...any) { 67 l.Log(stackLevel, log.WARN, "%s", fmt.Sprint(v...)) 68 } 69 70 // Warnf show warnning log 71 func (l *XORMLogBridge) Warnf(format string, v ...any) { 72 l.Log(stackLevel, log.WARN, format, v...) 73 } 74 75 // Level get logger level 76 func (l *XORMLogBridge) Level() xormlog.LogLevel { 77 switch l.logger.GetLevel() { 78 case log.TRACE, log.DEBUG: 79 return xormlog.LOG_DEBUG 80 case log.INFO: 81 return xormlog.LOG_INFO 82 case log.WARN: 83 return xormlog.LOG_WARNING 84 case log.ERROR: 85 return xormlog.LOG_ERR 86 case log.NONE: 87 return xormlog.LOG_OFF 88 } 89 return xormlog.LOG_UNKNOWN 90 } 91 92 // SetLevel set the logger level 93 func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) { 94 } 95 96 // ShowSQL set if record SQL 97 func (l *XORMLogBridge) ShowSQL(show ...bool) { 98 if len(show) == 0 { 99 show = []bool{true} 100 } 101 l.showSQL.Store(show[0]) 102 } 103 104 // IsShowSQL if record SQL 105 func (l *XORMLogBridge) IsShowSQL() bool { 106 return l.showSQL.Load() 107 }