You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

manager.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package private
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "code.gitea.io/gitea/modules/graceful"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/private"
  12. "code.gitea.io/gitea/modules/queue"
  13. "code.gitea.io/gitea/modules/setting"
  14. "gitea.com/macaron/macaron"
  15. )
  16. // FlushQueues flushes all the Queues
  17. func FlushQueues(ctx *macaron.Context, opts private.FlushOptions) {
  18. if opts.NonBlocking {
  19. // Save the hammer ctx here - as a new one is created each time you call this.
  20. baseCtx := graceful.GetManager().HammerContext()
  21. go func() {
  22. err := queue.GetManager().FlushAll(baseCtx, opts.Timeout)
  23. if err != nil {
  24. log.Error("Flushing request timed-out with error: %v", err)
  25. }
  26. }()
  27. ctx.JSON(http.StatusAccepted, map[string]interface{}{
  28. "err": "Flushing",
  29. })
  30. return
  31. }
  32. err := queue.GetManager().FlushAll(ctx.Req.Request.Context(), opts.Timeout)
  33. if err != nil {
  34. ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{
  35. "err": fmt.Sprintf("%v", err),
  36. })
  37. }
  38. ctx.PlainText(http.StatusOK, []byte("success"))
  39. }
  40. // PauseLogging pauses logging
  41. func PauseLogging(ctx *macaron.Context) {
  42. log.Pause()
  43. ctx.PlainText(http.StatusOK, []byte("success"))
  44. }
  45. // ResumeLogging resumes logging
  46. func ResumeLogging(ctx *macaron.Context) {
  47. log.Resume()
  48. ctx.PlainText(http.StatusOK, []byte("success"))
  49. }
  50. // ReleaseReopenLogging releases and reopens logging files
  51. func ReleaseReopenLogging(ctx *macaron.Context) {
  52. if err := log.ReleaseReopen(); err != nil {
  53. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  54. "err": fmt.Sprintf("Error during release and reopen: %v", err),
  55. })
  56. return
  57. }
  58. ctx.PlainText(http.StatusOK, []byte("success"))
  59. }
  60. // RemoveLogger removes a logger
  61. func RemoveLogger(ctx *macaron.Context) {
  62. group := ctx.Params("group")
  63. name := ctx.Params("name")
  64. ok, err := log.GetLogger(group).DelLogger(name)
  65. if err != nil {
  66. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  67. "err": fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err),
  68. })
  69. return
  70. }
  71. if ok {
  72. setting.RemoveSubLogDescription(group, name)
  73. }
  74. ctx.PlainText(http.StatusOK, []byte(fmt.Sprintf("Removed %s %s", group, name)))
  75. }
  76. // AddLogger adds a logger
  77. func AddLogger(ctx *macaron.Context, opts private.LoggerOptions) {
  78. if len(opts.Group) == 0 {
  79. opts.Group = log.DEFAULT
  80. }
  81. if _, ok := opts.Config["flags"]; !ok {
  82. switch opts.Group {
  83. case "access":
  84. opts.Config["flags"] = log.FlagsFromString("")
  85. case "router":
  86. opts.Config["flags"] = log.FlagsFromString("date,time")
  87. default:
  88. opts.Config["flags"] = log.FlagsFromString("stdflags")
  89. }
  90. }
  91. if _, ok := opts.Config["colorize"]; !ok && opts.Mode == "console" {
  92. if _, ok := opts.Config["stderr"]; ok {
  93. opts.Config["colorize"] = log.CanColorStderr
  94. } else {
  95. opts.Config["colorize"] = log.CanColorStdout
  96. }
  97. }
  98. if _, ok := opts.Config["level"]; !ok {
  99. opts.Config["level"] = setting.LogLevel
  100. }
  101. if _, ok := opts.Config["stacktraceLevel"]; !ok {
  102. opts.Config["stacktraceLevel"] = setting.StacktraceLogLevel
  103. }
  104. if opts.Mode == "file" {
  105. if _, ok := opts.Config["maxsize"]; !ok {
  106. opts.Config["maxsize"] = 1 << 28
  107. }
  108. if _, ok := opts.Config["maxdays"]; !ok {
  109. opts.Config["maxdays"] = 7
  110. }
  111. if _, ok := opts.Config["compressionLevel"]; !ok {
  112. opts.Config["compressionLevel"] = -1
  113. }
  114. }
  115. bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
  116. byteConfig, err := json.Marshal(opts.Config)
  117. if err != nil {
  118. log.Error("Failed to marshal log configuration: %v %v", opts.Config, err)
  119. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  120. "err": fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err),
  121. })
  122. return
  123. }
  124. config := string(byteConfig)
  125. if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil {
  126. log.Error("Failed to create new named logger: %s %v", config, err)
  127. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  128. "err": fmt.Sprintf("Failed to create new named logger: %s %v", config, err),
  129. })
  130. return
  131. }
  132. setting.AddSubLogDescription(opts.Group, setting.SubLogDescription{
  133. Name: opts.Name,
  134. Provider: opts.Mode,
  135. Config: config,
  136. })
  137. ctx.PlainText(http.StatusOK, []byte("success"))
  138. }