Skip to content

Commit 3273fb9

Browse files
a1012112796zeripathlafriks
authored
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>
1 parent 82d1a7f commit 3273fb9

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

modules/setting/log.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type defaultLogOptions struct {
9494

9595
func newDefaultLogOptions() defaultLogOptions {
9696
return defaultLogOptions{
97-
levelName: LogLevel,
97+
levelName: LogLevel.String(),
9898
flags: "stdflags",
9999
filename: filepath.Join(LogRootPath, "gitea.log"),
100100
bufferLength: 10000,
@@ -115,9 +115,9 @@ type LogDescription struct {
115115
SubLogDescriptions []SubLogDescription
116116
}
117117

118-
func getLogLevel(section *ini.Section, key string, defaultValue string) string {
119-
value := section.Key(key).MustString("info")
120-
return log.FromString(value).String()
118+
func getLogLevel(section *ini.Section, key string, defaultValue log.Level) log.Level {
119+
value := section.Key(key).MustString(defaultValue.String())
120+
return log.FromString(value)
121121
}
122122

123123
func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string) string {
@@ -126,8 +126,7 @@ func getStacktraceLogLevel(section *ini.Section, key string, defaultValue string
126126
}
127127

128128
func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
129-
levelName = getLogLevel(sec, "LEVEL", LogLevel)
130-
level := log.FromString(levelName)
129+
level := getLogLevel(sec, "LEVEL", LogLevel)
131130
stacktraceLevelName := getStacktraceLogLevel(sec, "STACKTRACE_LEVEL", StacktraceLogLevel)
132131
stacktraceLevel := log.FromString(stacktraceLevelName)
133132
mode = name

modules/setting/setting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ var (
304304
}
305305

306306
// Log settings
307-
LogLevel string
307+
LogLevel log.Level
308308
StacktraceLogLevel string
309309
LogRootPath string
310310
DisableRouterLog bool
@@ -553,7 +553,7 @@ func NewContext() {
553553
}
554554
homeDir = strings.ReplaceAll(homeDir, "\\", "/")
555555

556-
LogLevel = getLogLevel(Cfg.Section("log"), "LEVEL", "Info")
556+
LogLevel = getLogLevel(Cfg.Section("log"), "LEVEL", log.INFO)
557557
StacktraceLogLevel = getStacktraceLogLevel(Cfg.Section("log"), "STACKTRACE_LEVEL", "None")
558558
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
559559
forcePathSeparator(LogRootPath)

routers/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func InstallPost(ctx *context.Context) {
373373
cfg.Section("session").Key("PROVIDER").SetValue("file")
374374

375375
cfg.Section("log").Key("MODE").SetValue("console")
376-
cfg.Section("log").Key("LEVEL").SetValue(setting.LogLevel)
376+
cfg.Section("log").Key("LEVEL").SetValue(setting.LogLevel.String())
377377
cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
378378
cfg.Section("log").Key("ROUTER").SetValue("console")
379379

0 commit comments

Comments
 (0)