Browse Source

Default log level to Info without hardcoding it in installer (#3041)

Piotr Orzechowski 1 year ago
parent
commit
57edc3155f
2 changed files with 10 additions and 9 deletions
  1. 9 8
      modules/setting/setting.go
  2. 1 1
      routers/install.go

+ 9 - 8
modules/setting/setting.go

@@ -331,6 +331,7 @@ var (
331 331
 	LibravatarService     *libravatar.Libravatar
332 332
 
333 333
 	// Log settings
334
+	LogLevel    string
334 335
 	LogRootPath string
335 336
 	LogModes    []string
336 337
 	LogConfigs  []string
@@ -659,6 +660,7 @@ func NewContext() {
659 660
 	}
660 661
 	homeDir = strings.Replace(homeDir, "\\", "/", -1)
661 662
 
663
+	LogLevel = getLogLevel("log", "LEVEL", "Info")
662 664
 	LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
663 665
 	forcePathSeparator(LogRootPath)
664 666
 
@@ -1192,6 +1194,11 @@ var logLevels = map[string]string{
1192 1194
 	"Critical": "5",
1193 1195
 }
1194 1196
 
1197
+func getLogLevel(section string, key string, defaultValue string) string {
1198
+	validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}
1199
+	return Cfg.Section(section).Key(key).In(defaultValue, validLevels)
1200
+}
1201
+
1195 1202
 func newLogService() {
1196 1203
 	log.Info("Gitea v%s%s", AppVer, AppBuiltWith)
1197 1204
 
@@ -1216,11 +1223,8 @@ func newLogService() {
1216 1223
 			sec, _ = Cfg.NewSection("log." + mode)
1217 1224
 		}
1218 1225
 
1219
-		validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}
1220 1226
 		// Log level.
1221
-		levelName := Cfg.Section("log."+mode).Key("LEVEL").In(
1222
-			Cfg.Section("log").Key("LEVEL").In("Trace", validLevels),
1223
-			validLevels)
1227
+		levelName := getLogLevel("log."+mode, "LEVEL", LogLevel)
1224 1228
 		level, ok := logLevels[levelName]
1225 1229
 		if !ok {
1226 1230
 			log.Fatal(4, "Unknown log level: %s", levelName)
@@ -1284,11 +1288,8 @@ func NewXORMLogService(disableConsole bool) {
1284 1288
 			sec, _ = Cfg.NewSection("log." + mode)
1285 1289
 		}
1286 1290
 
1287
-		validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}
1288 1291
 		// Log level.
1289
-		levelName := Cfg.Section("log."+mode).Key("LEVEL").In(
1290
-			Cfg.Section("log").Key("LEVEL").In("Trace", validLevels),
1291
-			validLevels)
1292
+		levelName := getLogLevel("log."+mode, "LEVEL", LogLevel)
1292 1293
 		level, ok := logLevels[levelName]
1293 1294
 		if !ok {
1294 1295
 			log.Fatal(4, "Unknown log level: %s", levelName)

+ 1 - 1
routers/install.go

@@ -310,7 +310,7 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
310 310
 	cfg.Section("session").Key("PROVIDER").SetValue("file")
311 311
 
312 312
 	cfg.Section("log").Key("MODE").SetValue("file")
313
-	cfg.Section("log").Key("LEVEL").SetValue("Info")
313
+	cfg.Section("log").Key("LEVEL").SetValue(setting.LogLevel)
314 314
 	cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
315 315
 
316 316
 	cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")