From 4909493a9f75ad188f044b8577e7357f122fb445 Mon Sep 17 00:00:00 2001 From: zeripath Date: Fri, 24 Jun 2022 11:49:47 +0100 Subject: [PATCH] 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 --- cmd/manager_logging.go | 27 +++++++++++++++++++++++++++ models/db/engine.go | 9 +++++++++ models/db/log.go | 25 ++++++++++++++++--------- modules/private/manager.go | 19 +++++++++++++++++++ routers/private/internal.go | 1 + routers/private/manager.go | 7 +++++++ 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go index 0043ea1e52..761edf654c 100644 --- a/cmd/manager_logging.go +++ b/cmd/manager_logging.go @@ -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 +} 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 } diff --git a/modules/private/manager.go b/modules/private/manager.go index 8405bf2c83..ba51260ebb 100644 --- a/modules/private/manager.go +++ b/modules/private/manager.go @@ -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 diff --git a/routers/private/internal.go b/routers/private/internal.go index 6ba87d67bf..061c7f3c82 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -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) diff --git a/routers/private/manager.go b/routers/private/manager.go index a3b9a16f79..e7f08ac455 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -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")