Allow manager logging to set SQL (#20064)
This PR adds a new manager command to switch on SQL logging and to turn it off. ``` gitea manager logging log-sql gitea manager logging log-sql --off ``` Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
parent
afea63f4e5
commit
4909493a9f
6 changed files with 79 additions and 9 deletions
|
@ -287,3 +287,12 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) {
|
|||
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
|
||||
return maxID, err
|
||||
}
|
||||
|
||||
func SetLogSQL(ctx context.Context, on bool) {
|
||||
e := GetEngine(ctx)
|
||||
if x, ok := e.(*xorm.Engine); ok {
|
||||
x.ShowSQL(on)
|
||||
} else if sess, ok := e.(*xorm.Session); ok {
|
||||
sess.Engine().ShowSQL(on)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package db
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
|
@ -14,15 +15,19 @@ import (
|
|||
|
||||
// XORMLogBridge a logger bridge from Logger to xorm
|
||||
type XORMLogBridge struct {
|
||||
showSQL bool
|
||||
logger log.Logger
|
||||
showSQLint *int32
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// NewXORMLogger inits a log bridge for xorm
|
||||
func NewXORMLogger(showSQL bool) xormlog.Logger {
|
||||
showSQLint := int32(0)
|
||||
if showSQL {
|
||||
showSQLint = 1
|
||||
}
|
||||
return &XORMLogBridge{
|
||||
showSQL: showSQL,
|
||||
logger: log.GetLogger("xorm"),
|
||||
showSQLint: &showSQLint,
|
||||
logger: log.GetLogger("xorm"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,14 +99,16 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) {
|
|||
|
||||
// ShowSQL set if record SQL
|
||||
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
||||
if len(show) > 0 {
|
||||
l.showSQL = show[0]
|
||||
} else {
|
||||
l.showSQL = true
|
||||
showSQL := int32(1)
|
||||
if len(show) > 0 && !show[0] {
|
||||
showSQL = 0
|
||||
}
|
||||
atomic.StoreInt32(l.showSQLint, showSQL)
|
||||
}
|
||||
|
||||
// IsShowSQL if record SQL
|
||||
func (l *XORMLogBridge) IsShowSQL() bool {
|
||||
return l.showSQL
|
||||
showSQL := atomic.LoadInt32(l.showSQLint)
|
||||
|
||||
return showSQL == 1
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue