Skip to content

Commit 9b4f80a

Browse files
committed
Change synclog format
1 parent dd81d67 commit 9b4f80a

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

cli/cli.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type args struct {
7777
Workers uint `arg:"-w" help:"Workers count"`
7878
Debug bool `arg:"-d" help:"Show debug logging"`
7979
SyncLog bool `arg:"--sync-log" help:"Show sync log"`
80+
SyncLogFormat string `arg:"--sync-log-format" help:"Format of sync log. Possible values: json"`
8081
ShowProgress bool `arg:"--sync-progress,-p" help:"Show sync progress"`
8182
OnFail string `arg:"--on-fail,-f" help:"Action on failed. Possible values: fatal, skip, skipmissing (DEPRECATED, use --error-handling instead)"`
8283
ErrorHandlingMask uint8 `arg:"--error-handling" help:"Controls error handling. Sum of the values: 1 for ignoring NotFound errors, 2 for ignoring PermissionDenied errors OR 255 to ignore all errors"`
@@ -111,6 +112,7 @@ func GetCliArgs() (cli argsParsed, err error) {
111112
rawCli.ListBuffer = 1000
112113
rawCli.RateLimitObjPerSec = 0
113114
rawCli.ErrorHandlingMask = 0
115+
rawCli.SyncLogFormat = ""
114116

115117
p := arg.MustParse(&rawCli)
116118
cli.args = rawCli
@@ -138,6 +140,12 @@ func GetCliArgs() (cli argsParsed, err error) {
138140
p.Fail("--on-fail must be one of \"fatal, skip, skipmissing\"")
139141
}
140142

143+
switch cli.args.SyncLogFormat {
144+
case "json":
145+
default:
146+
p.Fail("--sync-log-format must be one of \"json\"")
147+
}
148+
141149
if rate, ok := parseBandwith(cli.args.RateLimitBandwidth); ok {
142150
cli.RateLimitBandwidth = rate
143151
} else {

cli/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func init() {
4949
if cli.Debug {
5050
log.SetLevel(logrus.DebugLevel)
5151
}
52+
if cli.SyncLogFormat == "json" {
53+
log.SetFormatter(&logrus.JSONFormatter{})
54+
}
5255
pipeline.Log = log
5356
storage.Log = log
5457
}

cli/print.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/larrabee/s3sync/pipeline"
7+
"github.com/sirupsen/logrus"
78
"time"
89
)
910

@@ -26,20 +27,40 @@ func printLiveStats(ctx context.Context, syncGroup *pipeline.Group) {
2627
func printFinalStats(syncGroup *pipeline.Group, status syncStatus) {
2728
dur := time.Since(syncGroup.StartTime).Seconds()
2829
for _, val := range syncGroup.GetStepsInfo() {
29-
log.Infof("%d %s: Input: %d; Output: %d (%.f obj/sec); Errors: %d\n", val.Num, val.Name, val.Stats.Input, val.Stats.Output, float64(val.Stats.Output)/dur, val.Stats.Error)
30+
log.WithFields(logrus.Fields{
31+
"stepNum": val.Num,
32+
"stepName": val.Name,
33+
"InputObj": val.Stats.Input,
34+
"OutputObj": val.Stats.Output,
35+
"ErrorObj": val.Stats.Error,
36+
"InputObjSpeed": float64(val.Stats.Input)/dur,
37+
"OutputObjSpeed": float64(val.Stats.Output)/dur,
38+
}).Info("Pipeline step finished")
3039
}
31-
log.Infof("Duration: %s", time.Since(syncGroup.StartTime).String())
40+
log.WithFields(logrus.Fields{
41+
"durationSec": time.Since(syncGroup.StartTime).Seconds(),
42+
}).Infof("Duration: %s", time.Since(syncGroup.StartTime).String())
3243

3344
switch status {
3445
case syncStatusOk:
35-
log.Infof("Sync Done")
46+
log.WithFields(logrus.Fields{
47+
"status": status,
48+
}).Infof("Sync Done")
3649
case syncStatusFailed:
37-
log.Error("Sync Failed")
50+
log.WithFields(logrus.Fields{
51+
"status": status,
52+
}).Error("Sync Failed")
3853
case syncStatusAborted:
39-
log.Warnf("Sync Aborted")
54+
log.WithFields(logrus.Fields{
55+
"status": status,
56+
}).Warnf("Sync Aborted")
4057
case syncStatusConfError:
41-
log.Errorf("Sync Configuration error")
58+
log.WithFields(logrus.Fields{
59+
"status": status,
60+
}).Errorf("Sync Configuration error")
4261
default:
43-
log.Warnf("Sync Unknown status")
62+
log.WithFields(logrus.Fields{
63+
"status": status,
64+
}).Warnf("Sync Unknown status")
4465
}
4566
}

pipeline/collection/misc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ var Logger pipeline.StepFn = func(group *pipeline.Group, stepNum int, input <-ch
2626
}
2727
for obj := range input {
2828
if ok {
29-
cfg.Infof("Key: %s", *obj.Key)
29+
cfg.WithFields(logrus.Fields{
30+
"key": *obj.Key,
31+
"size": len(*obj.Content),
32+
"Content-Type": *obj.ContentType,
33+
}).Infof("Sync file")
3034
output <- obj
3135
}
3236
}

0 commit comments

Comments
 (0)