log: fix deadlock when using systemd logging - fixes #8621

In this commit the logging system was re-worked

dfa4d948279f3e36 fs: Remove github.com/sirupsen/logrus and replace with log/slog

Unfortunately the systemd logging was still using the plain log
package and this caused a deadlock as it was recursively calling the
logging package.

The fix was to use the dedicated systemd journal logging routines in
the process removing a TODO!
This commit is contained in:
Nick Craig-Wood 2025-06-20 12:00:40 +01:00
parent b6b8526fb4
commit 3cae373064

View File

@ -5,10 +5,7 @@
package log package log
import ( import (
"fmt"
"log"
"log/slog" "log/slog"
"strconv"
"github.com/coreos/go-systemd/v22/journal" "github.com/coreos/go-systemd/v22/journal"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
@ -18,10 +15,8 @@ import (
func startSystemdLog(handler *OutputHandler) bool { func startSystemdLog(handler *OutputHandler) bool {
handler.clearFormatFlags(logFormatDate | logFormatTime | logFormatMicroseconds | logFormatUTC | logFormatLongFile | logFormatShortFile | logFormatPid) handler.clearFormatFlags(logFormatDate | logFormatTime | logFormatMicroseconds | logFormatUTC | logFormatLongFile | logFormatShortFile | logFormatPid)
handler.setFormatFlags(logFormatNoLevel) handler.setFormatFlags(logFormatNoLevel)
// TODO: Use the native journal.Print approach rather than a custom implementation
handler.SetOutput(func(level slog.Level, text string) { handler.SetOutput(func(level slog.Level, text string) {
text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text) _ = journal.Print(slogLevelToSystemdPriority(level), "%-6s: %s\n", level, text)
_ = log.Output(4, text)
}) })
return true return true
} }
@ -37,12 +32,12 @@ var slogLevelToSystemdPrefix = map[slog.Level]journal.Priority{
slog.LevelDebug: journal.PriDebug, slog.LevelDebug: journal.PriDebug,
} }
func systemdLogPrefix(l slog.Level) string { func slogLevelToSystemdPriority(l slog.Level) journal.Priority {
prio, ok := slogLevelToSystemdPrefix[l] prio, ok := slogLevelToSystemdPrefix[l]
if !ok { if !ok {
return "" return journal.PriInfo
} }
return strconv.Itoa(int(prio)) return prio
} }
func isJournalStream() bool { func isJournalStream() bool {