From 68be24c88d5c0ec5b71f712a4490144370781cd1 Mon Sep 17 00:00:00 2001 From: Ivan Andreev Date: Wed, 18 Aug 2021 14:48:44 +0300 Subject: [PATCH] log: optionally print pid in logs - #5593 This option is useful to troubleshoot `rclone mount --daemon` --- docs/content/docs.md | 2 +- fs/log.go | 7 +++++++ fs/log/log.go | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) 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)