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:
zeripath 2022-06-24 11:49:47 +01:00 committed by GitHub
parent afea63f4e5
commit 4909493a9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 9 deletions

View file

@ -174,6 +174,18 @@ var (
Action: runAddSMTPLogger,
},
},
}, {
Name: "log-sql",
Usage: "Set LogSQL",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "debug",
}, cli.BoolFlag{
Name: "off",
Usage: "Switch off SQL logging",
},
},
Action: runSetLogSQL,
},
},
}
@ -381,3 +393,18 @@ func runReleaseReopenLogging(c *cli.Context) error {
fmt.Fprintln(os.Stdout, msg)
return nil
}
func runSetLogSQL(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()
setup("manager", c.Bool("debug"))
statusCode, msg := private.SetLogSQL(ctx, !c.Bool("off"))
switch statusCode {
case http.StatusInternalServerError:
return fail("InternalServerError", msg)
}
fmt.Fprintln(os.Stdout, msg)
return nil
}

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"strconv"
"time"
"code.gitea.io/gitea/modules/json"
@ -139,6 +140,24 @@ func ReleaseReopenLogging(ctx context.Context) (int, string) {
return http.StatusOK, "Logging Restarted"
}
// SetLogSQL sets database logging
func SetLogSQL(ctx context.Context, on bool) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return resp.StatusCode, decodeJSONError(resp).Err
}
return http.StatusOK, "Log SQL setting set"
}
// LoggerOptions represents the options for the add logger call
type LoggerOptions struct {
Group string

View file

@ -68,6 +68,7 @@ func Routes() *web.Route {
r.Post("/manager/pause-logging", PauseLogging)
r.Post("/manager/resume-logging", ResumeLogging)
r.Post("/manager/release-and-reopen-logging", ReleaseReopenLogging)
r.Post("/manager/set-log-sql", SetLogSQL)
r.Post("/manager/add-logger", bind(private.LoggerOptions{}), AddLogger)
r.Post("/manager/remove-logger/{group}/{name}", RemoveLogger)
r.Get("/manager/processes", Processes)

View file

@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
@ -67,6 +68,12 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) {
ctx.PlainText(http.StatusOK, "success")
}
// SetLogSQL re-sets database SQL logging
func SetLogSQL(ctx *context.PrivateContext) {
db.SetLogSQL(ctx, ctx.FormBool("on"))
ctx.PlainText(http.StatusOK, "success")
}
// RemoveLogger removes a logger
func RemoveLogger(ctx *context.PrivateContext) {
group := ctx.Params("group")