Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

log: fix enabling systemd logging when using --log-file

Nick Craig-Woodc98dd8755cb9
diff --git a/cmd/cmd.go b/cmd/cmd.go
index fd42f8891..c5667b9ac 100644
--- a/cmd/cmd.go
+++ b/cmd/cmd.go
@@ -21,7 +21,6 @@ import (
"sync"
"time"
- systemd "github.com/iguanesolutions/go-systemd/v5"
"github.com/pkg/errors"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/accounting"
@@ -376,11 +375,6 @@ func StartStats() func() {
func initConfig() {
ctx := context.Background()
ci := fs.GetConfig(ctx)
- // Activate logger systemd support if systemd invocation ID is detected
- _, sysdLaunch := systemd.GetInvocationID()
- if sysdLaunch {
- ci.LogSystemdSupport = true // used during fslog.InitLogging()
- }
// Start the logger
fslog.InitLogging()
@@ -398,10 +392,8 @@ func initConfig() {
fs.Debugf("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)
// Inform user about systemd log support now that we have a logger
- if sysdLaunch {
- fs.Debugf("rclone", "systemd logging support automatically activated")
- } else if ci.LogSystemdSupport {
- fs.Debugf("rclone", "systemd logging support manually activated")
+ if fslog.Opt.LogSystemdSupport {
+ fs.Debugf("rclone", "systemd logging support activated")
}
// Start the remote control server if configured
diff --git a/fs/config.go b/fs/config.go
index 87ffe6fcf..da60ada84 100644
--- a/fs/config.go
+++ b/fs/config.go
@@ -43,7 +43,6 @@ var (
type ConfigInfo struct {
LogLevel LogLevel
StatsLogLevel LogLevel
- LogSystemdSupport bool
UseJSONLog bool
DryRun bool
Interactive bool
diff --git a/fs/config/configflags/configflags.go b/fs/config/configflags/configflags.go
index 4d1ea3665..2e4bf3426 100644
--- a/fs/config/configflags/configflags.go
+++ b/fs/config/configflags/configflags.go
@@ -124,7 +124,6 @@ func AddFlags(ci *fs.ConfigInfo, flagSet *pflag.FlagSet) {
flags.StringArrayVarP(flagSet, &downloadHeaders, "header-download", "", nil, "Set HTTP header for download transactions")
flags.StringArrayVarP(flagSet, &headers, "header", "", nil, "Set HTTP header for all transactions")
flags.BoolVarP(flagSet, &ci.RefreshTimes, "refresh-times", "", ci.RefreshTimes, "Refresh the modtime of remote files.")
- flags.BoolVarP(flagSet, &ci.LogSystemdSupport, "log-systemd", "", ci.LogSystemdSupport, "Activate systemd integration for the logger.")
}
// ParseHeaders converts the strings passed in via the header flags into HTTPOptions
diff --git a/fs/log.go b/fs/log.go
index 76c0965aa..b7c16e290 100644
--- a/fs/log.go
+++ b/fs/log.go
@@ -5,7 +5,6 @@ import (
"fmt"
"log"
- sysdjournald "github.com/iguanesolutions/go-systemd/v5/journald"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -72,28 +71,7 @@ func (l *LogLevel) Type() string {
// LogPrint sends the text to the logger of level
var LogPrint = func(level LogLevel, text string) {
- var prefix string
- if GetConfig(context.TODO()).LogSystemdSupport {
- switch level {
- case LogLevelDebug:
- prefix = sysdjournald.DebugPrefix
- case LogLevelInfo:
- prefix = sysdjournald.InfoPrefix
- case LogLevelNotice:
- prefix = sysdjournald.NoticePrefix
- case LogLevelWarning:
- prefix = sysdjournald.WarningPrefix
- case LogLevelError:
- prefix = sysdjournald.ErrPrefix
- case LogLevelCritical:
- prefix = sysdjournald.CritPrefix
- case LogLevelAlert:
- prefix = sysdjournald.AlertPrefix
- case LogLevelEmergency:
- prefix = sysdjournald.EmergPrefix
- }
- }
- text = fmt.Sprintf("%s%-6s: %s", prefix, level, text)
+ text = fmt.Sprintf("%-6s: %s", level, text)
_ = log.Output(4, text)
}
diff --git a/fs/log/log.go b/fs/log/log.go
index d87fd970e..ed1e1cbbb 100644
--- a/fs/log/log.go
+++ b/fs/log/log.go
@@ -3,6 +3,7 @@ package log
import (
"context"
+ "fmt"
"io"
"log"
"os"
@@ -10,16 +11,19 @@ import (
"runtime"
"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"
)
-// Options contains options for the remote control server
+// Options contains options for controlling the logging
type Options struct {
- File string // Log everything to this file
- Format string // Comma separated list of log format options
- UseSyslog bool // Use Syslog for logging
- SyslogFacility string // Facility for syslog, e.g. KERN,USER,...
+ File string // Log everything to this file
+ Format string // Comma separated list of log format options
+ UseSyslog bool // Use Syslog for logging
+ SyslogFacility string // Facility for syslog, e.g. KERN,USER,...
+ LogSystemdSupport bool // set if using systemd logging
}
// DefaultOpt is the default values used for Opt
@@ -91,19 +95,17 @@ func Stack(o interface{}, info string) {
func InitLogging() {
flagsStr := "," + Opt.Format + ","
var flags int
- if !fs.GetConfig(context.Background()).LogSystemdSupport {
- if strings.Contains(flagsStr, ",date,") {
- flags |= log.Ldate
- }
- if strings.Contains(flagsStr, ",time,") {
- flags |= log.Ltime
- }
- if strings.Contains(flagsStr, ",microseconds,") {
- flags |= log.Lmicroseconds
- }
- if strings.Contains(flagsStr, ",UTC,") {
- flags |= log.LUTC
- }
+ if strings.Contains(flagsStr, ",date,") {
+ flags |= log.Ldate
+ }
+ if strings.Contains(flagsStr, ",time,") {
+ flags |= log.Ltime
+ }
+ if strings.Contains(flagsStr, ",microseconds,") {
+ flags |= log.Lmicroseconds
+ }
+ if strings.Contains(flagsStr, ",UTC,") {
+ flags |= log.LUTC
}
if strings.Contains(flagsStr, ",longfile,") {
flags |= log.Llongfile
@@ -135,9 +137,46 @@ func InitLogging() {
}
startSysLog()
}
+
+ // Activate systemd logger support if systemd invocation ID is
+ // detected and output is going to stderr (not logging to a file or syslog)
+ if !Redirected() {
+ if _, usingSystemd := systemd.GetInvocationID(); usingSystemd {
+ Opt.LogSystemdSupport = true
+ }
+ }
+
+ // Systemd logging output
+ if Opt.LogSystemdSupport {
+ startSystemdLog()
+ }
}
// Redirected returns true if the log has been redirected from stdout
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/logflags/logflags.go b/fs/log/logflags/logflags.go
index a788bbf4a..70496b8a3 100644
--- a/fs/log/logflags/logflags.go
+++ b/fs/log/logflags/logflags.go
@@ -16,4 +16,5 @@ func AddFlags(flagSet *pflag.FlagSet) {
flags.StringVarP(flagSet, &log.Opt.Format, "log-format", "", log.Opt.Format, "Comma separated list of log format options")
flags.BoolVarP(flagSet, &log.Opt.UseSyslog, "syslog", "", log.Opt.UseSyslog, "Use Syslog for logging")
flags.StringVarP(flagSet, &log.Opt.SyslogFacility, "syslog-facility", "", log.Opt.SyslogFacility, "Facility for syslog, e.g. KERN,USER,...")
+ flags.BoolVarP(flagSet, &log.Opt.LogSystemdSupport, "log-systemd", "", log.Opt.LogSystemdSupport, "Activate systemd integration for the logger.")
}