Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: Added support to include date-time in log filename #7697

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/content/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,22 @@ triggering follow-on actions if data was copied, or skipping if not.
NB: Enabling this option turns a usually non-fatal error into a potentially
fatal one - please check and adjust your scripts accordingly!

### --file-time-format=WILDCARD~#~FORMAT ###

The value provided for this flag is split into two parts (`WILDCARD` and `FORMAT`)
at `~#~`. The `FORMAT` is a time stamp format as defined
[here](https://pkg.go.dev/github.com/ncruces/go-strftime). The timestamp is converted
into the provided format and all the `WILDCARD` in the filename,
(e.g., `--log-file=FILE`, see [Log File Section](#--log-filefile-))
will be replaced by the formatted timestamp.

E.g. usage:

Assume that the current time is `27 March 2024 10:00 P.M.` and the flag is used as
`--file-time-format=:dt:~#~%Y-%m-%d` along with the flag `--log-file=C:\err_:dt:.log`.
This means that the wildcard is `:dt:` and the time format is `%Y-%m-%d`.
So the logs will be printed in the file `C:\err_2024-03-27.log`.

### --fix-case ###

Normally, a sync to a case insensitive dest (such as macOS / Windows) will
Expand Down
33 changes: 28 additions & 5 deletions fs/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import (
"reflect"
"runtime"
"strings"
"time"

"github.com/ncruces/go-strftime"
"github.com/rclone/rclone/fs"
"github.com/sirupsen/logrus"
)

// 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,...
LogSystemdSupport bool // set if using systemd logging
File string // Log everything to this file
FilenameTimeFormat string // Date time format that is used to replace the :dt: in logging filename
FilenameTimeWildcard string // The wildcard that is replaced with formatted time in logging filenames
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
Expand Down Expand Up @@ -114,8 +118,27 @@ func InitLogging() {

fs.LogPrintPid = strings.Contains(flagsStr, ",pid,")

if Opt.FilenameTimeFormat != "" {
tmp := strings.SplitN(Opt.FilenameTimeFormat, "~#~", 2)
if len(tmp) != 2 {
log.Fatalf("Invalid Filename Time Format provided via --file-time-format. Please refer docs for proper usage.")
}

Opt.FilenameTimeWildcard = tmp[0]
Opt.FilenameTimeFormat = tmp[1]

if Opt.FilenameTimeWildcard == "" {
log.Fatalf("The wildcard in the Filename Time Format cannot be empty")
}
}

// Log file output
if Opt.File != "" {
if Opt.FilenameTimeFormat != "" && Opt.FilenameTimeWildcard != "" {
timeStr := strftime.Format(Opt.FilenameTimeFormat, time.Now())
Opt.File = strings.ReplaceAll(Opt.File, Opt.FilenameTimeWildcard, timeStr)
}

f, err := os.OpenFile(Opt.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
Expand Down
1 change: 1 addition & 0 deletions fs/log/logflags/logflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func AddFlags(flagSet *pflag.FlagSet) {
rc.AddOption("log", &log.Opt)

flags.StringVarP(flagSet, &log.Opt.FilenameTimeFormat, "file-time-format", "", log.Opt.FilenameTimeFormat, "Date time format that is used to replace the provided wildcard with time in several filenames", "Logging")
flags.StringVarP(flagSet, &log.Opt.File, "log-file", "", log.Opt.File, "Log everything to this file", "Logging")
flags.StringVarP(flagSet, &log.Opt.Format, "log-format", "", log.Opt.Format, "Comma separated list of log format options", "Logging")
flags.BoolVarP(flagSet, &log.Opt.UseSyslog, "syslog", "", log.Opt.UseSyslog, "Use Syslog for logging", "Logging")
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ require (
github.com/minio/minio-go/v7 v7.0.66
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/sys/mountinfo v0.7.1
github.com/ncruces/go-strftime v0.1.9
github.com/ncw/swift/v2 v2.0.2
github.com/oracle/oci-go-sdk/v65 v65.55.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.6
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v1.18.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/ncw/swift/v2 v2.0.2 h1:jx282pcAKFhmoZBSdMcCRFn9VWkoBIRsCpe+yZq7vEk=
github.com/ncw/swift/v2 v2.0.2/go.mod h1:z0A9RVdYPjNjXVo2pDOPxZ4eu3oarO1P91fTItcb+Kg=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
Expand Down