Files
gitea/modules/log/logger_global.go
ThomasL f049668c21 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>
2026-05-02 15:45:31 +00:00

98 lines
2.5 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package log
import (
"fmt"
"os"
)
// FallbackErrorf is the last chance to show an error if the logger has internal errors
func FallbackErrorf(format string, args ...any) {
_, _ = fmt.Fprintf(os.Stderr, format+"\n", args...)
}
func GetLevel() Level {
return GetLogger(DEFAULT).GetLevel()
}
func Log(skip int, level Level, format string, v ...any) {
// codeql[disable-next-line=go/clear-text-logging]
GetLogger(DEFAULT).Log(skip+1, &Event{Level: level}, format, v...)
}
func Trace(format string, v ...any) {
Log(1, TRACE, format, v...)
}
func IsTrace() bool {
return GetLevel() <= TRACE
}
func Debug(format string, v ...any) {
Log(1, DEBUG, format, v...)
}
func IsDebug() bool {
return GetLevel() <= DEBUG
}
func Info(format string, v ...any) {
Log(1, INFO, format, v...)
}
func Warn(format string, v ...any) {
Log(1, WARN, format, v...)
}
func Error(format string, v ...any) {
Log(1, ERROR, format, v...)
}
func ErrorWithSkip(skip int, format string, v ...any) {
Log(skip+1, ERROR, format, v...)
}
func Critical(format string, v ...any) {
Log(1, ERROR, format, v...)
}
var OsExiter = os.Exit
// Fatal records fatal log and exit process
func Fatal(format string, v ...any) {
Log(1, FATAL, format, v...)
GetManager().Close()
OsExiter(1)
}
func GetLogger(name string) Logger {
return GetManager().GetLogger(name)
}
func IsLoggerEnabled(name string) bool {
return GetManager().GetLogger(name).IsEnabled()
}
func SetupStderrLogger(loggerName, writerName string, level Level) {
writer := NewEventWriterConsole(writerName, WriterMode{
Level: level,
Flags: FlagsFromBits(LstdFlags),
Colorize: CanColorStderr,
// For most CLI commands, it's better to use Stderr as log output:
// this logger is installed early (app.Before), before subcommands like "dump" redirect logging to stderr.
// If Stdout, early log output (e.g.: warning during config loading) goes to stdout
// and corrupts any command that writes data to stdout (e.g. "gitea dump --file -").
//
// It is inconsistent with the web server's default console logger from config
// (which will be initialized later and use Stdout by default), but there is no other way at the moment:
// many existing users depend on such behavior to collect web logs (e.g. fail2ban).
//
// Maybe need to refactor the logger system again in the future.
WriterOption: WriterConsoleOption{Stderr: true},
})
GetManager().GetLogger(loggerName).ReplaceAllWriters(writer)
}