1
0
mirror of https://github.com/rclone/rclone.git synced 2025-01-13 20:38:12 +02:00

fs: generalise machinery for putting extra values when using --use-json-log

This commit is contained in:
Nick Craig-Wood 2020-04-11 18:02:50 +01:00
parent 12a208a880
commit 424554bc85
2 changed files with 26 additions and 5 deletions

View File

@ -377,7 +377,7 @@ func (s *StatsInfo) Transferred() []TransferSnapshot {
func (s *StatsInfo) Log() { func (s *StatsInfo) Log() {
if fs.Config.UseJSONLog { if fs.Config.UseJSONLog {
out, _ := s.RemoteStats() out, _ := s.RemoteStats()
fs.LogLevelPrintf(fs.Config.StatsLogLevel, nil, "%T\n", map[string]interface{}(out)) fs.LogLevelPrintf(fs.Config.StatsLogLevel, nil, "%v%v\n", s, fs.LogValue("stats", out))
} else { } else {
fs.LogLevelPrintf(fs.Config.StatsLogLevel, nil, "%v\n", s) fs.LogLevelPrintf(fs.Config.StatsLogLevel, nil, "%v\n", s)
} }

View File

@ -3,7 +3,6 @@ package fs
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -75,6 +74,27 @@ var LogPrint = func(level LogLevel, text string) {
_ = log.Output(4, text) _ = log.Output(4, text)
} }
// LogValueItem describes keyed item for a JSON log entry
type LogValueItem struct {
key string
value interface{}
}
// LogValue should be used as an argument to any logging calls to
// augment the JSON output with more structured information.
//
// key is the dictionary parameter used to store value.
func LogValue(key string, value interface{}) LogValueItem {
return LogValueItem{key: key, value: value}
}
// String returns an empty string so LogValueItem entries won't show
// in the textual representation of logs. They need to be put in so
// the number of parameters of the log call matches.
func (j LogValueItem) String() string {
return ""
}
// LogPrintf produces a log string from the arguments passed in // LogPrintf produces a log string from the arguments passed in
func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{}) { func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{}) {
out := fmt.Sprintf(text, args...) out := fmt.Sprintf(text, args...)
@ -87,9 +107,10 @@ func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{})
"objectType": fmt.Sprintf("%T", o), "objectType": fmt.Sprintf("%T", o),
} }
} }
if strings.HasPrefix(out, "map[") { for _, arg := range args {
fields["json"] = args[0] if item, ok := arg.(LogValueItem); ok {
out = "" fields[item.key] = item.value
}
} }
switch level { switch level {
case LogLevelDebug: case LogLevelDebug: