From 931da04dc2c7bdc078ded191b4d44d1e1f0d161b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Mon, 2 Mar 2020 22:25:28 +0800 Subject: [PATCH] cmd/serv: improve hookMode handling (#5960) - Allow remove primary logger at better location - Use more appropriate log.Error to replace log.Fatal --- internal/cmd/serv.go | 11 +++++++---- internal/conf/log.go | 15 ++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/cmd/serv.go b/internal/cmd/serv.go index 3478b7204..64e0f1d0f 100644 --- a/internal/cmd/serv.go +++ b/internal/cmd/serv.go @@ -35,14 +35,17 @@ var Serv = cli.Command{ }, } -func fail(userMessage, logMessage string, args ...interface{}) { +// fail prints user message to the Git client (i.e. os.Stderr) and +// logs error message on the server side. When not in "prod" mode, +// error message is also printed to the client for easier debugging. +func fail(userMessage, errMessage string, args ...interface{}) { fmt.Fprintln(os.Stderr, "Gogs:", userMessage) - if len(logMessage) > 0 { + if len(errMessage) > 0 { if !conf.IsProdMode() { - fmt.Fprintf(os.Stderr, logMessage+"\n", args...) + fmt.Fprintf(os.Stderr, errMessage+"\n", args...) } - log.Fatal(logMessage, args...) + log.Error(errMessage, args...) } os.Exit(1) diff --git a/internal/conf/log.go b/internal/conf/log.go index 58e17f871..e6865a41d 100644 --- a/internal/conf/log.go +++ b/internal/conf/log.go @@ -126,17 +126,15 @@ func initLogConf(cfg *ini.File, hookMode bool) (_ *logConf, hasConsole bool, _ e } // InitLogging initializes the logging service of the application. When the -// argument "hookMode" is true, it only initializes the root path for log files -// without creating any logger. +// "hookMode" is true, it only initializes the root path for log files without +// creating any logger. It will also not remove the primary logger in "hookMode" +// and is up to the caller to decide when to remove it. func InitLogging(hookMode bool) { logConf, hasConsole, err := initLogConf(File, hookMode) if err != nil { log.Fatal("Failed to init logging configuration: %v", err) } defer func() { - if !hasConsole { - log.Remove(log.DefaultConsoleName) - } Log = logConf }() @@ -177,4 +175,11 @@ func InitLogging(hookMode bool) { } log.Trace("Log mode: %s (%s)", strings.Title(mode), strings.Title(strings.ToLower(level.String()))) } + + // ⚠️ WARNING: It is only safe to remove the primary logger until + // there are other loggers that are initialized. Otherwise, the + // application will print errors to nowhere. + if !hasConsole { + log.Remove(log.DefaultConsoleName) + } }