Skip to content

Commit f049668

Browse files
thtclaudewxiaoguangbircni
authored
fix: redirect early CLI console logger to stderr (#37507)
When running `gitea dump` with output routed to stdout (--file -), deprecation warnings from loadAvatarsFrom were written to stdout, corrupting the archive stream. Root cause: PrepareConsoleLoggerLevel (called in app.Before) sets up a console logger via SetConsoleLogger, which used WriterConsoleOption{} defaulting Stderr to false (i.e. stdout). This logger is installed before the dump subcommand can redirect logging to stderr in runDump. Fix: use WriterConsoleOption{Stderr: true} in SetConsoleLogger so all early CLI diagnostic output goes to stderr from the start. This is correct for all subcommands — diagnostic/log output should never pollute stdout. --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Nicolas <bircni@icloud.com>
1 parent 3d838ef commit f049668

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

cmd/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cl
134134
if globalBool(c, "debug") || globalBool(c, "verbose") {
135135
level = log.TRACE
136136
}
137-
log.SetConsoleLogger(log.DEFAULT, "console-default", level)
137+
log.SetupStderrLogger(log.DEFAULT, "console-stderr", level)
138138
return ctx, nil
139139
}
140140
}

modules/log/logger_global.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,23 @@ func IsLoggerEnabled(name string) bool {
7575
return GetManager().GetLogger(name).IsEnabled()
7676
}
7777

78-
func SetConsoleLogger(loggerName, writerName string, level Level) {
78+
func SetupStderrLogger(loggerName, writerName string, level Level) {
7979
writer := NewEventWriterConsole(writerName, WriterMode{
80-
Level: level,
81-
Flags: FlagsFromBits(LstdFlags),
82-
Colorize: CanColorStdout,
83-
WriterOption: WriterConsoleOption{},
80+
Level: level,
81+
Flags: FlagsFromBits(LstdFlags),
82+
Colorize: CanColorStderr,
83+
84+
// For most CLI commands, it's better to use Stderr as log output:
85+
// this logger is installed early (app.Before), before subcommands like "dump" redirect logging to stderr.
86+
// If Stdout, early log output (e.g.: warning during config loading) goes to stdout
87+
// and corrupts any command that writes data to stdout (e.g. "gitea dump --file -").
88+
//
89+
// It is inconsistent with the web server's default console logger from config
90+
// (which will be initialized later and use Stdout by default), but there is no other way at the moment:
91+
// many existing users depend on such behavior to collect web logs (e.g. fail2ban).
92+
//
93+
// Maybe need to refactor the logger system again in the future.
94+
WriterOption: WriterConsoleOption{Stderr: true},
8495
})
8596
GetManager().GetLogger(loggerName).ReplaceAllWriters(writer)
8697
}

modules/setting/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func initLoggerByName(manager *log.LoggerManager, rootCfg ConfigProvider, logger
256256
}
257257

258258
func InitSQLLoggersForCli(level log.Level) {
259-
log.SetConsoleLogger("xorm", "console", level)
259+
log.SetupStderrLogger("xorm", "console-stderr", level)
260260
}
261261

262262
func IsAccessLogEnabled() bool {

modules/setting/setting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func init() {
4141
AppVer = "dev"
4242
}
4343

44-
// We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically
44+
// FIXME: the logger shouldn't be initialized here, the app entry should initialize the logger
4545
// By default set this logger at Info - we'll change it later, but we need to start with something.
46-
log.SetConsoleLogger(log.DEFAULT, "console", log.INFO)
46+
log.SetupStderrLogger(log.DEFAULT, "console-stderr", log.INFO)
4747
}
4848

4949
// IsRunUserMatchCurrentUser returns false if configured run user does not match

0 commit comments

Comments
 (0)