log.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2020 The Gogs 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 conf
  5. import (
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/pkg/errors"
  10. "gopkg.in/ini.v1"
  11. log "unknwon.dev/clog/v2"
  12. )
  13. type loggerConf struct {
  14. Buffer int64
  15. Config interface{}
  16. }
  17. type logConf struct {
  18. RootPath string
  19. Modes []string
  20. Configs []*loggerConf
  21. }
  22. // Log settings
  23. var Log *logConf
  24. // initLogConf returns parsed logging configuration from given INI file. When the
  25. // argument "hookMode" is true, it only initializes the root path for log files.
  26. // NOTE: Because we always create a console logger as the primary logger at init time,
  27. // we need to remove it in case the user doesn't configure to use it after the logging
  28. // service is initalized.
  29. func initLogConf(cfg *ini.File, hookMode bool) (_ *logConf, hasConsole bool, _ error) {
  30. rootPath := cfg.Section("log").Key("ROOT_PATH").MustString(filepath.Join(WorkDir(), "log"))
  31. if hookMode {
  32. return &logConf{
  33. RootPath: ensureAbs(rootPath),
  34. }, false, nil
  35. }
  36. modes := strings.Split(cfg.Section("log").Key("MODE").MustString("console"), ",")
  37. lc := &logConf{
  38. RootPath: ensureAbs(rootPath),
  39. Modes: make([]string, 0, len(modes)),
  40. Configs: make([]*loggerConf, 0, len(modes)),
  41. }
  42. // Iterate over [log.*] sections to initialize individual logger.
  43. levelMappings := map[string]log.Level{
  44. "trace": log.LevelTrace,
  45. "info": log.LevelInfo,
  46. "warn": log.LevelWarn,
  47. "error": log.LevelError,
  48. "fatal": log.LevelFatal,
  49. }
  50. for i := range modes {
  51. modes[i] = strings.ToLower(strings.TrimSpace(modes[i]))
  52. secName := "log." + modes[i]
  53. sec, err := cfg.GetSection(secName)
  54. if err != nil {
  55. return nil, hasConsole, errors.Errorf("missing configuration section [%s] for %q logger", secName, modes[i])
  56. }
  57. level := levelMappings[strings.ToLower(sec.Key("LEVEL").MustString("trace"))]
  58. buffer := sec.Key("BUFFER_LEN").MustInt64(100)
  59. var c *loggerConf
  60. switch modes[i] {
  61. case log.DefaultConsoleName:
  62. hasConsole = true
  63. c = &loggerConf{
  64. Buffer: buffer,
  65. Config: log.ConsoleConfig{
  66. Level: level,
  67. },
  68. }
  69. case log.DefaultFileName:
  70. logPath := filepath.Join(lc.RootPath, "gogs.log")
  71. c = &loggerConf{
  72. Buffer: buffer,
  73. Config: log.FileConfig{
  74. Level: level,
  75. Filename: logPath,
  76. FileRotationConfig: log.FileRotationConfig{
  77. Rotate: sec.Key("LOG_ROTATE").MustBool(true),
  78. Daily: sec.Key("DAILY_ROTATE").MustBool(true),
  79. MaxSize: 1 << uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)),
  80. MaxLines: sec.Key("MAX_LINES").MustInt64(1000000),
  81. MaxDays: sec.Key("MAX_DAYS").MustInt64(7),
  82. },
  83. },
  84. }
  85. case log.DefaultSlackName:
  86. c = &loggerConf{
  87. Buffer: buffer,
  88. Config: log.SlackConfig{
  89. Level: level,
  90. URL: sec.Key("URL").String(),
  91. },
  92. }
  93. case log.DefaultDiscordName:
  94. c = &loggerConf{
  95. Buffer: buffer,
  96. Config: log.DiscordConfig{
  97. Level: level,
  98. URL: sec.Key("URL").String(),
  99. Username: sec.Key("USERNAME").String(),
  100. },
  101. }
  102. default:
  103. continue
  104. }
  105. lc.Modes = append(lc.Modes, modes[i])
  106. lc.Configs = append(lc.Configs, c)
  107. }
  108. return lc, hasConsole, nil
  109. }
  110. // InitLogging initializes the logging service of the application. When the
  111. // argument "hookMode" is true, it only initializes the root path for log files
  112. // without creating any logger.
  113. func InitLogging(hookMode bool) {
  114. logConf, hasConsole, err := initLogConf(File, hookMode)
  115. if err != nil {
  116. log.Fatal("Failed to init logging configuration: %v", err)
  117. }
  118. defer func() {
  119. if !hasConsole {
  120. log.Remove(log.DefaultConsoleName)
  121. }
  122. Log = logConf
  123. }()
  124. if hookMode {
  125. return
  126. }
  127. err = os.MkdirAll(logConf.RootPath, os.ModePerm)
  128. if err != nil {
  129. log.Fatal("Failed to create log directory: %v", err)
  130. }
  131. for i, mode := range logConf.Modes {
  132. c := logConf.Configs[i]
  133. var err error
  134. var level log.Level
  135. switch mode {
  136. case log.DefaultConsoleName:
  137. level = c.Config.(log.ConsoleConfig).Level
  138. err = log.NewConsole(c.Buffer, c.Config)
  139. case log.DefaultFileName:
  140. level = c.Config.(log.FileConfig).Level
  141. err = log.NewFile(c.Buffer, c.Config)
  142. case log.DefaultSlackName:
  143. level = c.Config.(log.SlackConfig).Level
  144. err = log.NewSlack(c.Buffer, c.Config)
  145. case log.DefaultDiscordName:
  146. level = c.Config.(log.DiscordConfig).Level
  147. err = log.NewDiscord(c.Buffer, c.Config)
  148. default:
  149. panic("unreachable")
  150. }
  151. if err != nil {
  152. log.Fatal("Failed to init %s logger: %v", mode, err)
  153. return
  154. }
  155. log.Trace("Log mode: %s (%s)", strings.Title(mode), strings.Title(strings.ToLower(level.String())))
  156. }
  157. }