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: optionally print pid in logs - #5593

Ivan Andreev68be24c88d5c
diff --git a/docs/content/docs.md b/docs/content/docs.md
index f08deda83..5a7183a2b 100644
--- a/docs/content/docs.md
+++ b/docs/content/docs.md
@@ -1024,7 +1024,7 @@ have a signal to rotate logs.
### --log-format LIST ###
-Comma separated list of log format options. `date`, `time`, `microseconds`, `longfile`, `shortfile`, `UTC`. The default is "`date`,`time`".
+Comma separated list of log format options. Accepted options are `date`, `time`, `microseconds`, `pid`, `longfile`, `shortfile`, `UTC`. Any other keywords will be silently ignored. `pid` will tag log messages with process identifier which useful with `rclone mount --daemon`. Other accepted options are explained in the [go documentation](https://pkg.go.dev/log#pkg-constants). The default log format is "`date`,`time`".
### --log-level LEVEL ###
diff --git a/fs/log.go b/fs/log.go
index ccc50fa05..a3bc71a4a 100644
--- a/fs/log.go
+++ b/fs/log.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
+ "os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -80,9 +81,15 @@ func (l *LogLevel) UnmarshalJSON(in []byte) error {
})
}
+// LogPrintPid enables process pid in log
+var LogPrintPid = false
+
// LogPrint sends the text to the logger of level
var LogPrint = func(level LogLevel, text string) {
text = fmt.Sprintf("%-6s: %s", level, text)
+ if LogPrintPid {
+ text = fmt.Sprintf("[%d] %s", os.Getpid(), text)
+ }
_ = log.Output(4, text)
}
diff --git a/fs/log/log.go b/fs/log/log.go
index 6eb714dbd..9c66fdf02 100644
--- a/fs/log/log.go
+++ b/fs/log/log.go
@@ -113,6 +113,8 @@ func InitLogging() {
}
log.SetFlags(flags)
+ fs.LogPrintPid = strings.Contains(flagsStr, ",pid,")
+
// Log file output
if Opt.File != "" {
f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)