use level config in main section when subsection not set level (#15176)

in previouse if a log subsetcion not set level
it will use ``info`` as default value.

this pr will make default value (``[log] -> LEVEL``) useable.

example config:
```INI
[log]
MODE = console
LEVEL = Trace

[log.console]
LEVEL =
STDERR = false
```

previous result:
```JSON
// console:
{
  "level": "info",
  ...................
}
```

after change:

```JSON
// console:
{
  "level": "track",
  ...................
}
```

Signed-off-by: a1012112796 <1012112796@qq.com>

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
a1012112796 2021-03-29 03:08:19 +08:00 committed by GitHub
parent 82d1a7fb17
commit 3273fb9af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 9 deletions

View File

@ -94,7 +94,7 @@ type defaultLogOptions struct {
func newDefaultLogOptions() defaultLogOptions { func newDefaultLogOptions() defaultLogOptions {
return defaultLogOptions{ return defaultLogOptions{
levelName: LogLevel, levelName: LogLevel.String(),
flags: "stdflags", flags: "stdflags",
filename: filepath.Join(LogRootPath, "gitea.log"), filename: filepath.Join(LogRootPath, "gitea.log"),
bufferLength: 10000, bufferLength: 10000,
@ -115,9 +115,9 @@ type LogDescription struct {
SubLogDescriptions []SubLogDescription SubLogDescriptions []SubLogDescription
} }
func getLogLevel(section *ini.Section, key string, defaultValue string) string { func getLogLevel(section *ini.Section, key string, defaultValue log.Level) log.Level {
value := section.Key(key).MustString("info") value := section.Key(key).MustString(defaultValue.String())
return log.FromString(value).String() return log.FromString(value)
} }
func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string) string { func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string) string {
@ -126,8 +126,7 @@ func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string
} }
func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) { func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
levelName = getLogLevel(sec, "LEVEL", LogLevel) level := getLogLevel(sec, "LEVEL", LogLevel)
level := log.FromString(levelName)
stacktraceLevelName := getStacktraceLogLevel(sec, "STACKTRACE_LEVEL", StacktraceLogLevel) stacktraceLevelName := getStacktraceLogLevel(sec, "STACKTRACE_LEVEL", StacktraceLogLevel)
stacktraceLevel := log.FromString(stacktraceLevelName) stacktraceLevel := log.FromString(stacktraceLevelName)
mode = name mode = name

View File

@ -304,7 +304,7 @@ var (
} }
// Log settings // Log settings
LogLevel string LogLevel log.Level
StacktraceLogLevel string StacktraceLogLevel string
LogRootPath string LogRootPath string
DisableRouterLog bool DisableRouterLog bool
@ -553,7 +553,7 @@ func NewContext() {
} }
homeDir = strings.ReplaceAll(homeDir, "\\", "/") homeDir = strings.ReplaceAll(homeDir, "\\", "/")
LogLevel = getLogLevel(Cfg.Section("log"), "LEVEL", "Info") LogLevel = getLogLevel(Cfg.Section("log"), "LEVEL", log.INFO)
StacktraceLogLevel = getStacktraceLogLevel(Cfg.Section("log"), "STACKTRACE_LEVEL", "None") StacktraceLogLevel = getStacktraceLogLevel(Cfg.Section("log"), "STACKTRACE_LEVEL", "None")
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log")) LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
forcePathSeparator(LogRootPath) forcePathSeparator(LogRootPath)

View File

@ -373,7 +373,7 @@ func InstallPost(ctx *context.Context) {
cfg.Section("session").Key("PROVIDER").SetValue("file") cfg.Section("session").Key("PROVIDER").SetValue("file")
cfg.Section("log").Key("MODE").SetValue("console") cfg.Section("log").Key("MODE").SetValue("console")
cfg.Section("log").Key("LEVEL").SetValue(setting.LogLevel) cfg.Section("log").Key("LEVEL").SetValue(setting.LogLevel.String())
cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath) cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
cfg.Section("log").Key("ROUTER").SetValue("console") cfg.Section("log").Key("ROUTER").SetValue("console")