2020-01-29 01:01:06 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2020-07-06 00:07:07 +00:00
|
|
|
"fmt"
|
2020-01-29 01:01:06 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-01-29 01:01:06 +00:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/private"
|
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2020-07-06 00:07:07 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-03-01 21:08:10 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-01-29 01:01:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlushQueues flushes all the Queues
|
2021-01-26 15:36:53 +00:00
|
|
|
func FlushQueues(ctx *context.PrivateContext) {
|
|
|
|
opts := web.GetForm(ctx).(*private.FlushOptions)
|
2020-01-29 01:01:06 +00:00
|
|
|
if opts.NonBlocking {
|
|
|
|
// Save the hammer ctx here - as a new one is created each time you call this.
|
|
|
|
baseCtx := graceful.GetManager().HammerContext()
|
|
|
|
go func() {
|
|
|
|
err := queue.GetManager().FlushAll(baseCtx, opts.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Flushing request timed-out with error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ctx.JSON(http.StatusAccepted, map[string]interface{}{
|
|
|
|
"err": "Flushing",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
err := queue.GetManager().FlushAll(ctx.Req.Context(), opts.Timeout)
|
2020-01-29 01:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{
|
2020-07-06 00:07:07 +00:00
|
|
|
"err": fmt.Sprintf("%v", err),
|
2020-01-29 01:01:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.PlainText(http.StatusOK, []byte("success"))
|
|
|
|
}
|
2020-07-06 00:07:07 +00:00
|
|
|
|
|
|
|
// PauseLogging pauses logging
|
2021-01-26 15:36:53 +00:00
|
|
|
func PauseLogging(ctx *context.PrivateContext) {
|
2020-07-06 00:07:07 +00:00
|
|
|
log.Pause()
|
|
|
|
ctx.PlainText(http.StatusOK, []byte("success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResumeLogging resumes logging
|
2021-01-26 15:36:53 +00:00
|
|
|
func ResumeLogging(ctx *context.PrivateContext) {
|
2020-07-06 00:07:07 +00:00
|
|
|
log.Resume()
|
|
|
|
ctx.PlainText(http.StatusOK, []byte("success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseReopenLogging releases and reopens logging files
|
2021-01-26 15:36:53 +00:00
|
|
|
func ReleaseReopenLogging(ctx *context.PrivateContext) {
|
2020-07-06 00:07:07 +00:00
|
|
|
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
|
2021-01-26 15:36:53 +00:00
|
|
|
func RemoveLogger(ctx *context.PrivateContext) {
|
2020-07-06 00:07:07 +00:00
|
|
|
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
|
2021-01-26 15:36:53 +00:00
|
|
|
func AddLogger(ctx *context.PrivateContext) {
|
|
|
|
opts := web.GetForm(ctx).(*private.LoggerOptions)
|
2020-07-06 00:07:07 +00:00
|
|
|
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)
|
2021-03-01 21:08:10 +00:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-07-06 00:07:07 +00:00
|
|
|
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"))
|
|
|
|
}
|