mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-04 09:19:06 +01:00
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
|
@ -174,6 +174,18 @@ var (
|
||||||
Action: runAddSMTPLogger,
|
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)
|
fmt.Fprintln(os.Stdout, msg)
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -287,3 +287,12 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) {
|
||||||
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
|
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
|
||||||
return maxID, err
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
|
||||||
|
@ -14,14 +15,18 @@ import (
|
||||||
|
|
||||||
// XORMLogBridge a logger bridge from Logger to xorm
|
// XORMLogBridge a logger bridge from Logger to xorm
|
||||||
type XORMLogBridge struct {
|
type XORMLogBridge struct {
|
||||||
showSQL bool
|
showSQLint *int32
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewXORMLogger inits a log bridge for xorm
|
// NewXORMLogger inits a log bridge for xorm
|
||||||
func NewXORMLogger(showSQL bool) xormlog.Logger {
|
func NewXORMLogger(showSQL bool) xormlog.Logger {
|
||||||
|
showSQLint := int32(0)
|
||||||
|
if showSQL {
|
||||||
|
showSQLint = 1
|
||||||
|
}
|
||||||
return &XORMLogBridge{
|
return &XORMLogBridge{
|
||||||
showSQL: showSQL,
|
showSQLint: &showSQLint,
|
||||||
logger: log.GetLogger("xorm"),
|
logger: log.GetLogger("xorm"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,14 +99,16 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) {
|
||||||
|
|
||||||
// ShowSQL set if record SQL
|
// ShowSQL set if record SQL
|
||||||
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
||||||
if len(show) > 0 {
|
showSQL := int32(1)
|
||||||
l.showSQL = show[0]
|
if len(show) > 0 && !show[0] {
|
||||||
} else {
|
showSQL = 0
|
||||||
l.showSQL = true
|
|
||||||
}
|
}
|
||||||
|
atomic.StoreInt32(l.showSQLint, showSQL)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsShowSQL if record SQL
|
// IsShowSQL if record SQL
|
||||||
func (l *XORMLogBridge) IsShowSQL() bool {
|
func (l *XORMLogBridge) IsShowSQL() bool {
|
||||||
return l.showSQL
|
showSQL := atomic.LoadInt32(l.showSQLint)
|
||||||
|
|
||||||
|
return showSQL == 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
@ -139,6 +140,24 @@ func ReleaseReopenLogging(ctx context.Context) (int, string) {
|
||||||
return http.StatusOK, "Logging Restarted"
|
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
|
// LoggerOptions represents the options for the add logger call
|
||||||
type LoggerOptions struct {
|
type LoggerOptions struct {
|
||||||
Group string
|
Group string
|
||||||
|
|
|
@ -68,6 +68,7 @@ func Routes() *web.Route {
|
||||||
r.Post("/manager/pause-logging", PauseLogging)
|
r.Post("/manager/pause-logging", PauseLogging)
|
||||||
r.Post("/manager/resume-logging", ResumeLogging)
|
r.Post("/manager/resume-logging", ResumeLogging)
|
||||||
r.Post("/manager/release-and-reopen-logging", ReleaseReopenLogging)
|
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/add-logger", bind(private.LoggerOptions{}), AddLogger)
|
||||||
r.Post("/manager/remove-logger/{group}/{name}", RemoveLogger)
|
r.Post("/manager/remove-logger/{group}/{name}", RemoveLogger)
|
||||||
r.Get("/manager/processes", Processes)
|
r.Get("/manager/processes", Processes)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/graceful"
|
"code.gitea.io/gitea/modules/graceful"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
@ -67,6 +68,12 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) {
|
||||||
ctx.PlainText(http.StatusOK, "success")
|
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
|
// RemoveLogger removes a logger
|
||||||
func RemoveLogger(ctx *context.PrivateContext) {
|
func RemoveLogger(ctx *context.PrivateContext) {
|
||||||
group := ctx.Params("group")
|
group := ctx.Params("group")
|
||||||
|
|
Loading…
Reference in a new issue