diff options
author | zeripath <art27@cantab.net> | 2022-06-24 11:49:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-24 12:49:47 +0200 |
commit | 4909493a9f75ad188f044b8577e7357f122fb445 (patch) | |
tree | 56d3c97df67b0a0846bbb9a92ebd4fb1e90ab4e4 /models/db | |
parent | afea63f4e5a4b7e3da03a7d5fc6590569976e7d3 (diff) | |
download | gitea-4909493a9f75ad188f044b8577e7357f122fb445.tar.gz gitea-4909493a9f75ad188f044b8577e7357f122fb445.zip |
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>
Diffstat (limited to 'models/db')
-rwxr-xr-x | models/db/engine.go | 9 | ||||
-rw-r--r-- | models/db/log.go | 25 |
2 files changed, 25 insertions, 9 deletions
diff --git a/models/db/engine.go b/models/db/engine.go index 93cf5ad8bc..2c329300e3 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -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) + } +} diff --git a/models/db/log.go b/models/db/log.go index f9febf440e..4c497fdfd7 100644 --- a/models/db/log.go +++ b/models/db/log.go @@ -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 } |