diff options
author | zeripath <art27@cantab.net> | 2020-07-06 01:07:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-05 20:07:07 -0400 |
commit | c5b08f6d5a73e6ba84da84e804d05a8dd3d651be (patch) | |
tree | 7e0f72b9c62b8764d000614714c91a01ebbee223 /routers/private | |
parent | 38fb087d1983ca8320fb1a8c90150ae7956b358d (diff) | |
download | gitea-c5b08f6d5a73e6ba84da84e804d05a8dd3d651be.tar.gz gitea-c5b08f6d5a73e6ba84da84e804d05a8dd3d651be.zip |
Pause, Resume, Release&Reopen, Add and Remove Logging from command line (#11777)
* Make LogDescriptions race safe
* Add manager commands for pausing, resuming, adding and removing loggers
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Placate lint
* Ensure that file logger is run!
* Add support for smtp and conn
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Add release-and-reopen
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'routers/private')
-rw-r--r-- | routers/private/internal.go | 6 | ||||
-rw-r--r-- | routers/private/manager.go | 117 |
2 files changed, 121 insertions, 2 deletions
diff --git a/routers/private/internal.go b/routers/private/internal.go index 5bc01b0aea..821cf62a61 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -42,6 +42,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/manager/shutdown", Shutdown) m.Post("/manager/restart", Restart) m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues) - + m.Post("/manager/pause-logging", PauseLogging) + m.Post("/manager/resume-logging", ResumeLogging) + m.Post("/manager/release-and-reopen-logging", ReleaseReopenLogging) + m.Post("/manager/add-logger", bind(private.LoggerOptions{}), AddLogger) + m.Post("/manager/remove-logger/:group/:name", RemoveLogger) }, CheckInternalToken) } diff --git a/routers/private/manager.go b/routers/private/manager.go index 1238ff2d28..67bd92003f 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -5,12 +5,15 @@ package private import ( + "encoding/json" + "fmt" "net/http" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/queue" + "code.gitea.io/gitea/modules/setting" "gitea.com/macaron/macaron" ) @@ -34,8 +37,120 @@ func FlushQueues(ctx *macaron.Context, opts private.FlushOptions) { err := queue.GetManager().FlushAll(ctx.Req.Request.Context(), opts.Timeout) if err != nil { ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{ - "err": err, + "err": fmt.Sprintf("%v", err), }) } ctx.PlainText(http.StatusOK, []byte("success")) } + +// PauseLogging pauses logging +func PauseLogging(ctx *macaron.Context) { + log.Pause() + ctx.PlainText(http.StatusOK, []byte("success")) +} + +// ResumeLogging resumes logging +func ResumeLogging(ctx *macaron.Context) { + log.Resume() + ctx.PlainText(http.StatusOK, []byte("success")) +} + +// ReleaseReopenLogging releases and reopens logging files +func ReleaseReopenLogging(ctx *macaron.Context) { + if err := log.ReleaseReopen(); err != nil { + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": fmt.Sprintf("Error during release and reopen: %v", err), + }) + return + } + ctx.PlainText(http.StatusOK, []byte("success")) +} + +// RemoveLogger removes a logger +func RemoveLogger(ctx *macaron.Context) { + group := ctx.Params("group") + name := ctx.Params("name") + ok, err := log.GetLogger(group).DelLogger(name) + if err != nil { + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err), + }) + return + } + if ok { + setting.RemoveSubLogDescription(group, name) + } + ctx.PlainText(http.StatusOK, []byte(fmt.Sprintf("Removed %s %s", group, name))) +} + +// AddLogger adds a logger +func AddLogger(ctx *macaron.Context, opts private.LoggerOptions) { + if len(opts.Group) == 0 { + opts.Group = log.DEFAULT + } + if _, ok := opts.Config["flags"]; !ok { + switch opts.Group { + case "access": + opts.Config["flags"] = log.FlagsFromString("") + case "router": + opts.Config["flags"] = log.FlagsFromString("date,time") + default: + opts.Config["flags"] = log.FlagsFromString("stdflags") + } + } + + if _, ok := opts.Config["colorize"]; !ok && opts.Mode == "console" { + if _, ok := opts.Config["stderr"]; ok { + opts.Config["colorize"] = log.CanColorStderr + } else { + opts.Config["colorize"] = log.CanColorStdout + } + } + + if _, ok := opts.Config["level"]; !ok { + opts.Config["level"] = setting.LogLevel + } + + if _, ok := opts.Config["stacktraceLevel"]; !ok { + opts.Config["stacktraceLevel"] = setting.StacktraceLogLevel + } + + if opts.Mode == "file" { + if _, ok := opts.Config["maxsize"]; !ok { + opts.Config["maxsize"] = 1 << 28 + } + if _, ok := opts.Config["maxdays"]; !ok { + opts.Config["maxdays"] = 7 + } + if _, ok := opts.Config["compressionLevel"]; !ok { + opts.Config["compressionLevel"] = -1 + } + } + + bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000) + byteConfig, err := json.Marshal(opts.Config) + if err != nil { + log.Error("Failed to marshal log configuration: %v %v", opts.Config, err) + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err), + }) + return + } + config := string(byteConfig) + + if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil { + log.Error("Failed to create new named logger: %s %v", config, err) + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "err": fmt.Sprintf("Failed to create new named logger: %s %v", config, err), + }) + return + } + + setting.AddSubLogDescription(opts.Group, setting.SubLogDescription{ + Name: opts.Name, + Provider: opts.Mode, + Config: config, + }) + + ctx.PlainText(http.StatusOK, []byte("success")) +} |