Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow manager logging to set SQL #20064

Merged
merged 6 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/manager_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ var (
Action: runAddSMTPLogger,
},
},
}, {
Name: "log-sql",
Usage: "Set LogSQL",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "debug",
}, cli.BoolFlag{
Name: "off",
6543 marked this conversation as resolved.
Show resolved Hide resolved
Usage: "Switch off SQL logging",
},
},
Action: runSetLogSQL,
},
},
}
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions models/db/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
25 changes: 16 additions & 9 deletions models/db/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package db

import (
"fmt"
"sync/atomic"

"code.gitea.io/gitea/modules/log"

Expand All @@ -14,15 +15,19 @@ import (

// XORMLogBridge a logger bridge from Logger to xorm
type XORMLogBridge struct {
showSQL bool
logger log.Logger
showSQLint *int32
zeripath marked this conversation as resolved.
Show resolved Hide resolved
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"),
}
}

Expand Down Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions modules/private/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"strconv"
"time"

"code.gitea.io/gitea/modules/json"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions routers/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions routers/private/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down