log: optionally print pid in logs - #5593

This option is useful to troubleshoot `rclone mount --daemon`
This commit is contained in:
Ivan Andreev 2021-08-18 14:48:44 +03:00
parent fbc7f2e61b
commit 68be24c88d
3 changed files with 10 additions and 1 deletions

View File

@ -1024,7 +1024,7 @@ have a signal to rotate logs.
### --log-format LIST ### ### --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 ### ### --log-level LEVEL ###

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"os"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "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 // LogPrint sends the text to the logger of level
var LogPrint = func(level LogLevel, text string) { var LogPrint = func(level LogLevel, text string) {
text = fmt.Sprintf("%-6s: %s", level, text) text = fmt.Sprintf("%-6s: %s", level, text)
if LogPrintPid {
text = fmt.Sprintf("[%d] %s", os.Getpid(), text)
}
_ = log.Output(4, text) _ = log.Output(4, text)
} }

View File

@ -113,6 +113,8 @@ func InitLogging() {
} }
log.SetFlags(flags) log.SetFlags(flags)
fs.LogPrintPid = strings.Contains(flagsStr, ",pid,")
// Log file output // Log file output
if Opt.File != "" { if Opt.File != "" {
f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640) f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)