From e9423790780ddbb7f54ad14c08014230c46b0d1c Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 20 Jun 2022 23:10:38 +0100 Subject: [PATCH 1/3] Allow manager logging to set SQL 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 no-log-sql ``` Signed-off-by: Andrew Thornton --- cmd/manager_logging.go | 31 +++++++++++++++++++++++++++++++ models/db/log.go | 25 ++++++++++++++++--------- modules/private/manager.go | 19 +++++++++++++++++++ routers/private/internal.go | 1 + routers/private/manager.go | 14 ++++++++++++++ 5 files changed, 81 insertions(+), 9 deletions(-) diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go index 0043ea1e52ad4..29b48b1f171f8 100644 --- a/cmd/manager_logging.go +++ b/cmd/manager_logging.go @@ -174,6 +174,14 @@ var ( Action: runAddSMTPLogger, }, }, + }, { + Name: "log-sql", + Usage: "Set LogSQL", + Action: runSetLogSQL, + }, { + Name: "no-log-sql", + Usage: "Switch off LogSQL", + Action: runUnSetLogSQL, }, }, } @@ -381,3 +389,26 @@ func runReleaseReopenLogging(c *cli.Context) error { fmt.Fprintln(os.Stdout, msg) return nil } + +func runSetLogSQL(c *cli.Context) error { + return runSetLogSQLFlag(c, true) +} + +func runUnSetLogSQL(c *cli.Context) error { + return runSetLogSQLFlag(c, false) +} + +func runSetLogSQLFlag(c *cli.Context, set bool) error { + ctx, cancel := installSignals() + defer cancel() + setup("manager", c.Bool("debug")) + + statusCode, msg := private.SetLogSQL(ctx, set) + switch statusCode { + case http.StatusInternalServerError: + return fail("InternalServerError", msg) + } + + fmt.Fprintln(os.Stdout, msg) + return nil +} diff --git a/models/db/log.go b/models/db/log.go index f9febf440e2b2..4c497fdfd72c3 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 8405bf2c83d88..ba51260ebbee9 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 6ba87d67bf542..061c7f3c822af 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 a3b9a16f79a3f..c1aa197403b98 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" @@ -16,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" + "xorm.io/xorm" ) // FlushQueues flushes all the Queues @@ -67,6 +69,18 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) { ctx.PlainText(http.StatusOK, "success") } +// SetLogSQL re-sets database SQL logging +func SetLogSQL(ctx *context.PrivateContext) { + on := ctx.FormBool("on") + e := db.GetEngine(ctx) + if x, ok := e.(*xorm.Engine); ok { + x.ShowSQL(on) + } else if sess, ok := e.(*xorm.Session); ok { + sess.Engine().ShowSQL(on) + } + ctx.PlainText(http.StatusOK, "success") +} + // RemoveLogger removes a logger func RemoveLogger(ctx *context.PrivateContext) { group := ctx.Params("group") From f641a779f594b80c0dcea88109c6ac1ab3ad7ed9 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 21 Jun 2022 18:48:34 +0100 Subject: [PATCH 2/3] as per review Signed-off-by: Andrew Thornton --- cmd/manager_logging.go | 27 ++++++++++++--------------- models/db/engine.go | 9 +++++++++ routers/private/manager.go | 9 +-------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go index 29b48b1f171f8..4695ed4c997c5 100644 --- a/cmd/manager_logging.go +++ b/cmd/manager_logging.go @@ -175,13 +175,17 @@ var ( }, }, }, { - Name: "log-sql", - Usage: "Set LogSQL", + Name: "log-sql", + Usage: "Set LogSQL", + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "debug", + }, cli.BoolFlag{ + Name: "off", + Usage: "Switch off SQL logging", + }, + }, Action: runSetLogSQL, - }, { - Name: "no-log-sql", - Usage: "Switch off LogSQL", - Action: runUnSetLogSQL, }, }, } @@ -391,19 +395,12 @@ func runReleaseReopenLogging(c *cli.Context) error { } func runSetLogSQL(c *cli.Context) error { - return runSetLogSQLFlag(c, true) -} - -func runUnSetLogSQL(c *cli.Context) error { - return runSetLogSQLFlag(c, false) -} - -func runSetLogSQLFlag(c *cli.Context, set bool) error { ctx, cancel := installSignals() defer cancel() setup("manager", c.Bool("debug")) - statusCode, msg := private.SetLogSQL(ctx, set) + off := c.Bool("off") + statusCode, msg := private.SetLogSQL(ctx, !off) switch statusCode { case http.StatusInternalServerError: return fail("InternalServerError", msg) diff --git a/models/db/engine.go b/models/db/engine.go index 93cf5ad8bc06b..2c329300e3af2 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/routers/private/manager.go b/routers/private/manager.go index c1aa197403b98..e7f08ac455462 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -17,7 +17,6 @@ import ( "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" - "xorm.io/xorm" ) // FlushQueues flushes all the Queues @@ -71,13 +70,7 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) { // SetLogSQL re-sets database SQL logging func SetLogSQL(ctx *context.PrivateContext) { - on := ctx.FormBool("on") - e := db.GetEngine(ctx) - if x, ok := e.(*xorm.Engine); ok { - x.ShowSQL(on) - } else if sess, ok := e.(*xorm.Session); ok { - sess.Engine().ShowSQL(on) - } + db.SetLogSQL(ctx, ctx.FormBool("on")) ctx.PlainText(http.StatusOK, "success") } From 6795ecc23ce3efc2159018682cab9f3b58d82a1c Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 21 Jun 2022 19:13:56 +0100 Subject: [PATCH 3/3] Update cmd/manager_logging.go --- cmd/manager_logging.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go index 4695ed4c997c5..761edf654c8ac 100644 --- a/cmd/manager_logging.go +++ b/cmd/manager_logging.go @@ -399,8 +399,7 @@ func runSetLogSQL(c *cli.Context) error { defer cancel() setup("manager", c.Bool("debug")) - off := c.Bool("off") - statusCode, msg := private.SetLogSQL(ctx, !off) + statusCode, msg := private.SetLogSQL(ctx, !c.Bool("off")) switch statusCode { case http.StatusInternalServerError: return fail("InternalServerError", msg)