2017-02-20 16:11:13 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-20 16:11:13 +08:00
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
package db
|
2017-02-20 16:11:13 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-24 18:49:47 +08:00
|
|
|
"sync/atomic"
|
2017-02-20 16:11:13 +08:00
|
|
|
|
2019-05-14 15:04:07 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
xormlog "xorm.io/xorm/log"
|
2017-02-20 16:11:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// XORMLogBridge a logger bridge from Logger to xorm
|
|
|
|
type XORMLogBridge struct {
|
2022-06-24 18:49:47 +08:00
|
|
|
showSQLint *int32
|
|
|
|
logger log.Logger
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
2019-05-14 15:04:07 +08:00
|
|
|
// NewXORMLogger inits a log bridge for xorm
|
2020-03-22 23:12:55 +08:00
|
|
|
func NewXORMLogger(showSQL bool) xormlog.Logger {
|
2022-06-24 18:49:47 +08:00
|
|
|
showSQLint := int32(0)
|
|
|
|
if showSQL {
|
|
|
|
showSQLint = 1
|
|
|
|
}
|
2019-05-14 15:04:07 +08:00
|
|
|
return &XORMLogBridge{
|
2022-06-24 18:49:47 +08:00
|
|
|
showSQLint: &showSQLint,
|
|
|
|
logger: log.GetLogger("xorm"),
|
2017-02-22 19:44:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 15:41:22 +08:00
|
|
|
const stackLevel = 8
|
|
|
|
|
2019-04-02 15:48:31 +08:00
|
|
|
// Log a message with defined skip and at logging level
|
2019-05-14 15:04:07 +08:00
|
|
|
func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error {
|
|
|
|
return l.logger.Log(skip+1, level, format, v...)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug show debug log
|
|
|
|
func (l *XORMLogBridge) Debug(v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.DEBUG, fmt.Sprint(v...))
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf show debug log
|
|
|
|
func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.DEBUG, format, v...)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error show error log
|
|
|
|
func (l *XORMLogBridge) Error(v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.ERROR, fmt.Sprint(v...))
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf show error log
|
|
|
|
func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.ERROR, format, v...)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info show information level log
|
|
|
|
func (l *XORMLogBridge) Info(v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.INFO, fmt.Sprint(v...))
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infof show information level log
|
|
|
|
func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.INFO, format, v...)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 08:52:01 +08:00
|
|
|
// Warn show warning log
|
2017-02-20 16:11:13 +08:00
|
|
|
func (l *XORMLogBridge) Warn(v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.WARN, fmt.Sprint(v...))
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf show warnning log
|
|
|
|
func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
|
2021-04-05 15:41:22 +08:00
|
|
|
_ = l.Log(stackLevel, log.WARN, format, v...)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Level get logger level
|
2020-03-22 23:12:55 +08:00
|
|
|
func (l *XORMLogBridge) Level() xormlog.LogLevel {
|
2019-05-14 15:04:07 +08:00
|
|
|
switch l.logger.GetLevel() {
|
|
|
|
case log.TRACE, log.DEBUG:
|
2020-03-22 23:12:55 +08:00
|
|
|
return xormlog.LOG_DEBUG
|
2019-05-14 15:04:07 +08:00
|
|
|
case log.INFO:
|
2020-03-22 23:12:55 +08:00
|
|
|
return xormlog.LOG_INFO
|
2019-05-14 15:04:07 +08:00
|
|
|
case log.WARN:
|
2020-03-22 23:12:55 +08:00
|
|
|
return xormlog.LOG_WARNING
|
2019-05-14 15:04:07 +08:00
|
|
|
case log.ERROR, log.CRITICAL:
|
2020-03-22 23:12:55 +08:00
|
|
|
return xormlog.LOG_ERR
|
2019-04-02 15:48:31 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
return xormlog.LOG_OFF
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 15:48:31 +08:00
|
|
|
// SetLevel set the logger level
|
2020-03-22 23:12:55 +08:00
|
|
|
func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) {
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShowSQL set if record SQL
|
|
|
|
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
2022-06-24 18:49:47 +08:00
|
|
|
showSQL := int32(1)
|
|
|
|
if len(show) > 0 && !show[0] {
|
|
|
|
showSQL = 0
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
2022-06-24 18:49:47 +08:00
|
|
|
atomic.StoreInt32(l.showSQLint, showSQL)
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsShowSQL if record SQL
|
|
|
|
func (l *XORMLogBridge) IsShowSQL() bool {
|
2022-06-24 18:49:47 +08:00
|
|
|
showSQL := atomic.LoadInt32(l.showSQLint)
|
|
|
|
|
|
|
|
return showSQL == 1
|
2017-02-20 16:11:13 +08:00
|
|
|
}
|