Skip to content

Commit a67a8f3

Browse files
zeripathSysoev, Vladimir
authored and
Sysoev, Vladimir
committed
Allow manager logging to set SQL (go-gitea#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 <[email protected]>
1 parent 6fc5d2b commit a67a8f3

File tree

6 files changed

+79
-9
lines changed

6 files changed

+79
-9
lines changed

cmd/manager_logging.go

+27
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ var (
174174
Action: runAddSMTPLogger,
175175
},
176176
},
177+
}, {
178+
Name: "log-sql",
179+
Usage: "Set LogSQL",
180+
Flags: []cli.Flag{
181+
cli.BoolFlag{
182+
Name: "debug",
183+
}, cli.BoolFlag{
184+
Name: "off",
185+
Usage: "Switch off SQL logging",
186+
},
187+
},
188+
Action: runSetLogSQL,
177189
},
178190
},
179191
}
@@ -381,3 +393,18 @@ func runReleaseReopenLogging(c *cli.Context) error {
381393
fmt.Fprintln(os.Stdout, msg)
382394
return nil
383395
}
396+
397+
func runSetLogSQL(c *cli.Context) error {
398+
ctx, cancel := installSignals()
399+
defer cancel()
400+
setup("manager", c.Bool("debug"))
401+
402+
statusCode, msg := private.SetLogSQL(ctx, !c.Bool("off"))
403+
switch statusCode {
404+
case http.StatusInternalServerError:
405+
return fail("InternalServerError", msg)
406+
}
407+
408+
fmt.Fprintln(os.Stdout, msg)
409+
return nil
410+
}

models/db/engine.go

+9
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,12 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) {
287287
_, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID)
288288
return maxID, err
289289
}
290+
291+
func SetLogSQL(ctx context.Context, on bool) {
292+
e := GetEngine(ctx)
293+
if x, ok := e.(*xorm.Engine); ok {
294+
x.ShowSQL(on)
295+
} else if sess, ok := e.(*xorm.Session); ok {
296+
sess.Engine().ShowSQL(on)
297+
}
298+
}

models/db/log.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package db
66

77
import (
88
"fmt"
9+
"sync/atomic"
910

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

@@ -14,15 +15,19 @@ import (
1415

1516
// XORMLogBridge a logger bridge from Logger to xorm
1617
type XORMLogBridge struct {
17-
showSQL bool
18-
logger log.Logger
18+
showSQLint *int32
19+
logger log.Logger
1920
}
2021

2122
// NewXORMLogger inits a log bridge for xorm
2223
func NewXORMLogger(showSQL bool) xormlog.Logger {
24+
showSQLint := int32(0)
25+
if showSQL {
26+
showSQLint = 1
27+
}
2328
return &XORMLogBridge{
24-
showSQL: showSQL,
25-
logger: log.GetLogger("xorm"),
29+
showSQLint: &showSQLint,
30+
logger: log.GetLogger("xorm"),
2631
}
2732
}
2833

@@ -94,14 +99,16 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) {
9499

95100
// ShowSQL set if record SQL
96101
func (l *XORMLogBridge) ShowSQL(show ...bool) {
97-
if len(show) > 0 {
98-
l.showSQL = show[0]
99-
} else {
100-
l.showSQL = true
102+
showSQL := int32(1)
103+
if len(show) > 0 && !show[0] {
104+
showSQL = 0
101105
}
106+
atomic.StoreInt32(l.showSQLint, showSQL)
102107
}
103108

104109
// IsShowSQL if record SQL
105110
func (l *XORMLogBridge) IsShowSQL() bool {
106-
return l.showSQL
111+
showSQL := atomic.LoadInt32(l.showSQLint)
112+
113+
return showSQL == 1
107114
}

modules/private/manager.go

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/http"
1212
"net/url"
13+
"strconv"
1314
"time"
1415

1516
"code.gitea.io/gitea/modules/json"
@@ -139,6 +140,24 @@ func ReleaseReopenLogging(ctx context.Context) (int, string) {
139140
return http.StatusOK, "Logging Restarted"
140141
}
141142

143+
// SetLogSQL sets database logging
144+
func SetLogSQL(ctx context.Context, on bool) (int, string) {
145+
reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
146+
147+
req := newInternalRequest(ctx, reqURL, "POST")
148+
resp, err := req.Response()
149+
if err != nil {
150+
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
151+
}
152+
defer resp.Body.Close()
153+
154+
if resp.StatusCode != http.StatusOK {
155+
return resp.StatusCode, decodeJSONError(resp).Err
156+
}
157+
158+
return http.StatusOK, "Log SQL setting set"
159+
}
160+
142161
// LoggerOptions represents the options for the add logger call
143162
type LoggerOptions struct {
144163
Group string

routers/private/internal.go

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Routes() *web.Route {
6868
r.Post("/manager/pause-logging", PauseLogging)
6969
r.Post("/manager/resume-logging", ResumeLogging)
7070
r.Post("/manager/release-and-reopen-logging", ReleaseReopenLogging)
71+
r.Post("/manager/set-log-sql", SetLogSQL)
7172
r.Post("/manager/add-logger", bind(private.LoggerOptions{}), AddLogger)
7273
r.Post("/manager/remove-logger/{group}/{name}", RemoveLogger)
7374
r.Get("/manager/processes", Processes)

routers/private/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net/http"
1010

11+
"code.gitea.io/gitea/models/db"
1112
"code.gitea.io/gitea/modules/context"
1213
"code.gitea.io/gitea/modules/graceful"
1314
"code.gitea.io/gitea/modules/json"
@@ -67,6 +68,12 @@ func ReleaseReopenLogging(ctx *context.PrivateContext) {
6768
ctx.PlainText(http.StatusOK, "success")
6869
}
6970

71+
// SetLogSQL re-sets database SQL logging
72+
func SetLogSQL(ctx *context.PrivateContext) {
73+
db.SetLogSQL(ctx, ctx.FormBool("on"))
74+
ctx.PlainText(http.StatusOK, "success")
75+
}
76+
7077
// RemoveLogger removes a logger
7178
func RemoveLogger(ctx *context.PrivateContext) {
7279
group := ctx.Params("group")

0 commit comments

Comments
 (0)