From 2f30686e905cd08e2798519928c5c9749d8c8c20 Mon Sep 17 00:00:00 2001 From: Fabian Ruff Date: Tue, 5 Dec 2017 14:45:27 +0100 Subject: [PATCH] Add a simple log filter that removes err=null pairs --- pkg/cmd/kubernikus/operator.go | 7 +++++-- pkg/util/log/nilfilter.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 pkg/util/log/nilfilter.go diff --git a/pkg/cmd/kubernikus/operator.go b/pkg/cmd/kubernikus/operator.go index cd0ecf8db4..9d3b6fef14 100644 --- a/pkg/cmd/kubernikus/operator.go +++ b/pkg/cmd/kubernikus/operator.go @@ -10,11 +10,13 @@ import ( "github.com/go-kit/kit/log" "github.com/golang/glog" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/sapcc/kubernikus/pkg/cmd" "github.com/sapcc/kubernikus/pkg/controller" "github.com/sapcc/kubernikus/pkg/controller/metrics" - "github.com/spf13/cobra" - "github.com/spf13/pflag" + logutil "github.com/sapcc/kubernikus/pkg/util/log" ) func NewOperatorCommand() *cobra.Command { @@ -90,6 +92,7 @@ func (o *Options) Complete(args []string) error { func (o *Options) Run(c *cobra.Command) error { var logger log.Logger logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) + logger = logutil.NewTrailingNilFilter(logger) logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller) sigs := make(chan os.Signal, 1) diff --git a/pkg/util/log/nilfilter.go b/pkg/util/log/nilfilter.go new file mode 100644 index 0000000000..51135f418f --- /dev/null +++ b/pkg/util/log/nilfilter.go @@ -0,0 +1,24 @@ +package log + +import ( + kitlog "github.com/go-kit/kit/log" +) + +type nilFilter struct { + next kitlog.Logger +} + +// NewTrailingNilFilter removes key values pairs at the end with a nil value +// This mainly for getting rid of err=null trailers +func NewTrailingNilFilter(logger kitlog.Logger) kitlog.Logger { + return &nilFilter{next: logger} +} + +func (l nilFilter) Log(keyvals ...interface{}) error { + for i := len(keyvals) - 1; i > 0; i -= 2 { + if keyvals[i] != nil { + return l.next.Log(keyvals[:i+1]...) + } + } + return l.next.Log(keyvals...) +}