initLogConf returns parsed logging configuration from given INI file. When the argument "hookMode" is true, it only initializes the root path for log files. NOTE: Because we always create a console logger as the primary logger at init time, we need to remove it in case the user doesn't configure to
(cfg *ini.File, hookMode bool)
| 30 | // we need to remove it in case the user doesn't configure to use it after the logging |
| 31 | // service is initialized. |
| 32 | func initLogConf(cfg *ini.File, hookMode bool) (_ *logConf, hasConsole bool, _ error) { |
| 33 | rootPath := cfg.Section("log").Key("ROOT_PATH").MustString(filepath.Join(WorkDir(), "log")) |
| 34 | if hookMode { |
| 35 | return &logConf{ |
| 36 | RootPath: ensureAbs(rootPath), |
| 37 | }, false, nil |
| 38 | } |
| 39 | |
| 40 | modes := strings.Split(cfg.Section("log").Key("MODE").MustString("console"), ",") |
| 41 | lc := &logConf{ |
| 42 | RootPath: ensureAbs(rootPath), |
| 43 | Modes: make([]string, 0, len(modes)), |
| 44 | Configs: make([]*loggerConf, 0, len(modes)), |
| 45 | } |
| 46 | |
| 47 | // Iterate over [log.*] sections to initialize individual logger. |
| 48 | levelMappings := map[string]log.Level{ |
| 49 | "trace": log.LevelTrace, |
| 50 | "info": log.LevelInfo, |
| 51 | "warn": log.LevelWarn, |
| 52 | "error": log.LevelError, |
| 53 | "fatal": log.LevelFatal, |
| 54 | } |
| 55 | |
| 56 | for i := range modes { |
| 57 | modes[i] = strings.ToLower(strings.TrimSpace(modes[i])) |
| 58 | secName := "log." + modes[i] |
| 59 | sec, err := cfg.GetSection(secName) |
| 60 | if err != nil { |
| 61 | return nil, hasConsole, errors.Errorf("missing configuration section [%s] for %q logger", secName, modes[i]) |
| 62 | } |
| 63 | |
| 64 | level := levelMappings[strings.ToLower(sec.Key("LEVEL").MustString("trace"))] |
| 65 | buffer := sec.Key("BUFFER_LEN").MustInt64(100) |
| 66 | var c *loggerConf |
| 67 | switch modes[i] { |
| 68 | case log.DefaultConsoleName: |
| 69 | hasConsole = true |
| 70 | c = &loggerConf{ |
| 71 | Buffer: buffer, |
| 72 | Config: log.ConsoleConfig{ |
| 73 | Level: level, |
| 74 | }, |
| 75 | } |
| 76 | |
| 77 | case log.DefaultFileName: |
| 78 | logPath := filepath.Join(lc.RootPath, "gogs.log") |
| 79 | c = &loggerConf{ |
| 80 | Buffer: buffer, |
| 81 | Config: log.FileConfig{ |
| 82 | Level: level, |
| 83 | Filename: logPath, |
| 84 | FileRotationConfig: log.FileRotationConfig{ |
| 85 | Rotate: sec.Key("LOG_ROTATE").MustBool(true), |
| 86 | Daily: sec.Key("DAILY_ROTATE").MustBool(true), |
| 87 | MaxSize: 1 << uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)), |
| 88 | MaxLines: sec.Key("MAX_LINES").MustInt64(1000000), |
| 89 | MaxDays: sec.Key("MAX_DAYS").MustInt64(7), |