diff --git a/fs/log/log.go b/fs/log/log.go index ed1e1cbbb..6eb714dbd 100644 --- a/fs/log/log.go +++ b/fs/log/log.go @@ -3,7 +3,6 @@ package log import ( "context" - "fmt" "io" "log" "os" @@ -12,7 +11,6 @@ import ( "strings" systemd "github.com/iguanesolutions/go-systemd/v5" - sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald" "github.com/rclone/rclone/fs" "github.com/sirupsen/logrus" ) @@ -156,27 +154,3 @@ func InitLogging() { func Redirected() bool { return Opt.UseSyslog || Opt.File != "" } - -var logLevelToStringSystemd = []string{ - fs.LogLevelEmergency: sysdjournald.EmergPrefix, - fs.LogLevelAlert: sysdjournald.AlertPrefix, - fs.LogLevelCritical: sysdjournald.CritPrefix, - fs.LogLevelError: sysdjournald.ErrPrefix, - fs.LogLevelWarning: sysdjournald.WarningPrefix, - fs.LogLevelNotice: sysdjournald.NoticePrefix, - fs.LogLevelInfo: sysdjournald.InfoPrefix, - fs.LogLevelDebug: sysdjournald.DebugPrefix, -} - -// Starts systemd logging -func startSystemdLog() { - log.SetFlags(0) - fs.LogPrint = func(level fs.LogLevel, text string) { - var prefix string - if level < fs.LogLevel(len(logLevelToStringSystemd)) { - prefix = logLevelToStringSystemd[level] - } - text = fmt.Sprintf("%s%-6s: %s", prefix, level, text) - _ = log.Output(4, text) - } -} diff --git a/fs/log/systemd.go b/fs/log/systemd.go new file mode 100644 index 000000000..840b505f2 --- /dev/null +++ b/fs/log/systemd.go @@ -0,0 +1,16 @@ +// Systemd interface for non-Unix variants only + +// +build windows nacl plan9 + +package log + +import ( + "log" + "runtime" +) + +// Enables systemd logs if configured or if auto detected +func startSystemdLog() bool { + log.Fatalf("--log-systemd not supported on %s platform", runtime.GOOS) + return false +} diff --git a/fs/log/systemd_unix.go b/fs/log/systemd_unix.go new file mode 100644 index 000000000..fe001b56d --- /dev/null +++ b/fs/log/systemd_unix.go @@ -0,0 +1,50 @@ +// Systemd interface for Unix variants only + +// +build !windows,!nacl,!plan9 + +package log + +import ( + "fmt" + "log" + "strings" + + sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald" + "github.com/rclone/rclone/fs" +) + +// Enables systemd logs if configured or if auto detected +func startSystemdLog() bool { + flagsStr := "," + Opt.Format + "," + var flags int + if strings.Contains(flagsStr, ",longfile,") { + flags |= log.Llongfile + } + if strings.Contains(flagsStr, ",shortfile,") { + flags |= log.Lshortfile + } + log.SetFlags(flags) + fs.LogPrint = func(level fs.LogLevel, text string) { + text = fmt.Sprintf("%s%-6s: %s", systemdLogPrefix(level), level, text) + _ = log.Output(4, text) + } + return true +} + +var logLevelToSystemdPrefix = []string{ + fs.LogLevelEmergency: sysdjournald.EmergPrefix, + fs.LogLevelAlert: sysdjournald.AlertPrefix, + fs.LogLevelCritical: sysdjournald.CritPrefix, + fs.LogLevelError: sysdjournald.ErrPrefix, + fs.LogLevelWarning: sysdjournald.WarningPrefix, + fs.LogLevelNotice: sysdjournald.NoticePrefix, + fs.LogLevelInfo: sysdjournald.InfoPrefix, + fs.LogLevelDebug: sysdjournald.DebugPrefix, +} + +func systemdLogPrefix(l fs.LogLevel) string { + if l >= fs.LogLevel(len(logLevelToSystemdPrefix)) { + return "" + } + return logLevelToSystemdPrefix[l] +}