From 3c9516f27a9823e9ec5afe7f8f0aa35ec6b066ba Mon Sep 17 00:00:00 2001 From: Aswin M Prabhu Date: Sun, 2 Sep 2018 12:56:22 +0530 Subject: [PATCH 1/4] Add logrus adapter for Logger interface (#752) --- log/logrus_logger.go | 34 +++++++++++++++++++++++++ log/logrus_logger_test.go | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 log/logrus_logger.go create mode 100644 log/logrus_logger_test.go diff --git a/log/logrus_logger.go b/log/logrus_logger.go new file mode 100644 index 000000000..0f756e3a4 --- /dev/null +++ b/log/logrus_logger.go @@ -0,0 +1,34 @@ +package log + +import ( + "fmt" + "io" + + "github.com/sirupsen/logrus" +) + +type logrusLogger struct { + *logrus.Logger +} + +// NewLogrusLogger returns a logger that logs the keyvals to Writer +// with a time stamp at the logrus.InfoLevel +func NewLogrusLogger(w io.Writer) Logger { + newLogger := logrus.New() + newLogger.Out = w + newLogger.Formatter = &logrus.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true} + return &logrusLogger{newLogger} +} + +func (l logrusLogger) Log(keyvals ...interface{}) error { + if len(keyvals)%2 == 0 { + fields := logrus.Fields{} + for i := 0; i < len(keyvals); i += 2 { + fields[fmt.Sprint(keyvals[i])] = keyvals[i+1] + } + l.WithFields(fields).Info() + } else { + l.Info(keyvals) + } + return nil +} diff --git a/log/logrus_logger_test.go b/log/logrus_logger_test.go new file mode 100644 index 000000000..49425ce9a --- /dev/null +++ b/log/logrus_logger_test.go @@ -0,0 +1,53 @@ +package log_test + +import ( + "bytes" + "errors" + "io/ioutil" + "strings" + "testing" + + "github.com/go-kit/kit/log" +) + +func TestLogrusLogger(t *testing.T) { + t.Parallel() + buf := &bytes.Buffer{} + logger := log.NewLogrusLogger(buf) + + if err := logger.Log("hello", "world"); err != nil { + t.Fatal(err) + } + if want, have := "hello=world\n", strings.Split(buf.String(), " ")[3]; want != have { + t.Errorf("want %#v, have %#v", want, have) + } + + buf.Reset() + if err := logger.Log("a", 1, "err", errors.New("error")); err != nil { + t.Fatal(err) + } + if want, have := "a=1 err=error", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 4)[3]); want != have { + t.Errorf("want %#v, have %#v", want, have) + } + + buf.Reset() + if err := logger.Log("my_map", mymap{0: 0}); err != nil { + t.Fatal(err) + } + if want, have := "my_map=special_behavior", strings.TrimSpace(strings.Split(buf.String(), " ")[3]); want != have { + t.Errorf("want %#v, have %#v", want, have) + } +} + +func BenchmarkLogrusLoggerSimple(b *testing.B) { + benchmarkRunner(b, log.NewLogrusLogger(ioutil.Discard), baseMessage) +} + +func BenchmarkLogrusLoggerContextual(b *testing.B) { + benchmarkRunner(b, log.NewLogrusLogger(ioutil.Discard), withMessage) +} + +func TestLogrusLoggerConcurrency(t *testing.T) { + t.Parallel() + testConcurrency(t, log.NewLogrusLogger(ioutil.Discard), 10000) +} From 88ca082fdd931bce65418c46b2c10a536e5fcb66 Mon Sep 17 00:00:00 2001 From: Aswin M Prabhu Date: Sat, 8 Sep 2018 15:14:55 +0530 Subject: [PATCH 2/4] Make requested changes --- log/{ => logrus}/logrus_logger.go | 13 +++++-------- log/{ => logrus}/logrus_logger_test.go | 22 ++++++++-------------- 2 files changed, 13 insertions(+), 22 deletions(-) rename log/{ => logrus}/logrus_logger.go (53%) rename log/{ => logrus}/logrus_logger_test.go (66%) diff --git a/log/logrus_logger.go b/log/logrus/logrus_logger.go similarity index 53% rename from log/logrus_logger.go rename to log/logrus/logrus_logger.go index 0f756e3a4..933c1564f 100644 --- a/log/logrus_logger.go +++ b/log/logrus/logrus_logger.go @@ -2,8 +2,8 @@ package log import ( "fmt" - "io" + "github.com/go-kit/kit/log" "github.com/sirupsen/logrus" ) @@ -11,13 +11,10 @@ type logrusLogger struct { *logrus.Logger } -// NewLogrusLogger returns a logger that logs the keyvals to Writer -// with a time stamp at the logrus.InfoLevel -func NewLogrusLogger(w io.Writer) Logger { - newLogger := logrus.New() - newLogger.Out = w - newLogger.Formatter = &logrus.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true} - return &logrusLogger{newLogger} +// NewLogrusLogger takes a *logrus.Logger and returns +//a logger that stisfies the go-kit log.Logger interface +func NewLogrusLogger(logger *logrus.Logger) log.Logger { + return &logrusLogger{logger} } func (l logrusLogger) Log(keyvals ...interface{}) error { diff --git a/log/logrus_logger_test.go b/log/logrus/logrus_logger_test.go similarity index 66% rename from log/logrus_logger_test.go rename to log/logrus/logrus_logger_test.go index 49425ce9a..db4a1fc88 100644 --- a/log/logrus_logger_test.go +++ b/log/logrus/logrus_logger_test.go @@ -3,17 +3,20 @@ package log_test import ( "bytes" "errors" - "io/ioutil" "strings" "testing" - "github.com/go-kit/kit/log" + log "github.com/go-kit/kit/log/logrus" + "github.com/sirupsen/logrus" ) func TestLogrusLogger(t *testing.T) { t.Parallel() buf := &bytes.Buffer{} - logger := log.NewLogrusLogger(buf) + logrusLogger := logrus.New() + logrusLogger.Out = buf + logrusLogger.Formatter = &logrus.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true} + logger := log.NewLogrusLogger(logrusLogger) if err := logger.Log("hello", "world"); err != nil { t.Fatal(err) @@ -39,15 +42,6 @@ func TestLogrusLogger(t *testing.T) { } } -func BenchmarkLogrusLoggerSimple(b *testing.B) { - benchmarkRunner(b, log.NewLogrusLogger(ioutil.Discard), baseMessage) -} - -func BenchmarkLogrusLoggerContextual(b *testing.B) { - benchmarkRunner(b, log.NewLogrusLogger(ioutil.Discard), withMessage) -} +type mymap map[int]int -func TestLogrusLoggerConcurrency(t *testing.T) { - t.Parallel() - testConcurrency(t, log.NewLogrusLogger(ioutil.Discard), 10000) -} +func (m mymap) String() string { return "special_behavior" } From d879bac88de310856bd58e9ca21edfb2b8abdcd6 Mon Sep 17 00:00:00 2001 From: Aswin M Prabhu Date: Mon, 17 Sep 2018 20:12:12 +0530 Subject: [PATCH 3/4] Make requested changes --- log/logrus/logrus_logger.go | 21 +++++++++++++-------- log/logrus/logrus_logger_test.go | 10 +++++++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/log/logrus/logrus_logger.go b/log/logrus/logrus_logger.go index 933c1564f..dd76f794e 100644 --- a/log/logrus/logrus_logger.go +++ b/log/logrus/logrus_logger.go @@ -1,6 +1,9 @@ -package log +// Package logrus package provides an adapter to the +// go-kit log.Logger interface. +package logrus import ( + "errors" "fmt" "github.com/go-kit/kit/log" @@ -11,21 +14,23 @@ type logrusLogger struct { *logrus.Logger } +var errMissingValue = errors.New("(MISSING)") + // NewLogrusLogger takes a *logrus.Logger and returns -//a logger that stisfies the go-kit log.Logger interface +// a logger that satisfies the go-kit log.Logger interface. func NewLogrusLogger(logger *logrus.Logger) log.Logger { return &logrusLogger{logger} } func (l logrusLogger) Log(keyvals ...interface{}) error { - if len(keyvals)%2 == 0 { - fields := logrus.Fields{} - for i := 0; i < len(keyvals); i += 2 { + fields := logrus.Fields{} + for i := 0; i < len(keyvals); i += 2 { + if i+1 < len(keyvals) { fields[fmt.Sprint(keyvals[i])] = keyvals[i+1] + } else { + fields[fmt.Sprint(keyvals[i])] = errMissingValue } - l.WithFields(fields).Info() - } else { - l.Info(keyvals) } + l.WithFields(fields).Info() return nil } diff --git a/log/logrus/logrus_logger_test.go b/log/logrus/logrus_logger_test.go index db4a1fc88..cc656ce0f 100644 --- a/log/logrus/logrus_logger_test.go +++ b/log/logrus/logrus_logger_test.go @@ -1,4 +1,4 @@ -package log_test +package logrus_test import ( "bytes" @@ -33,6 +33,14 @@ func TestLogrusLogger(t *testing.T) { t.Errorf("want %#v, have %#v", want, have) } + buf.Reset() + if err := logger.Log("a", 1, "b"); err != nil { + t.Fatal(err) + } + if want, have := "a=1 b=\"(MISSING)\"", strings.TrimSpace(strings.SplitAfterN(buf.String(), " ", 4)[3]); want != have { + t.Errorf("want %#v, have %#v", want, have) + } + buf.Reset() if err := logger.Log("my_map", mymap{0: 0}); err != nil { t.Fatal(err) From d0547d80bd5bf7a1807cce5d518c779bfc9fecf3 Mon Sep 17 00:00:00 2001 From: Aswin M Prabhu Date: Tue, 18 Sep 2018 21:53:47 +0530 Subject: [PATCH 4/4] Fix doc comments --- log/logrus/logrus_logger.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/log/logrus/logrus_logger.go b/log/logrus/logrus_logger.go index dd76f794e..e7e370c94 100644 --- a/log/logrus/logrus_logger.go +++ b/log/logrus/logrus_logger.go @@ -1,4 +1,4 @@ -// Package logrus package provides an adapter to the +// Package logrus provides an adapter to the // go-kit log.Logger interface. package logrus @@ -16,8 +16,7 @@ type logrusLogger struct { var errMissingValue = errors.New("(MISSING)") -// NewLogrusLogger takes a *logrus.Logger and returns -// a logger that satisfies the go-kit log.Logger interface. +// NewLogrusLogger returns a go-kit log.Logger that sends log events to a Logrus logger. func NewLogrusLogger(logger *logrus.Logger) log.Logger { return &logrusLogger{logger} }