From d970fe8a288e00d21be36fef7e374f3b92c4f7ae Mon Sep 17 00:00:00 2001 From: Adam Renberg Tamm Date: Mon, 11 Mar 2019 11:52:47 +0100 Subject: [PATCH 1/2] Pass entry.Context to bugsnag.Notify in hook --- logger/logger.go | 3 +++ logger/logger_test.go | 6 +++++- logrusbugsnag/bugsnag.go | 4 +++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 381c58a..bdb4cca 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -33,6 +33,9 @@ func ContextLog(ctx Valuer, err []error, entry *logrus.Entry) *logrus.Entry { if ctx != nil { entry = entry.WithFields(getLoggableValues(ctx)) + if ctx, ok := ctx.(context.Context); ok { + entry = entry.WithContext(ctx) + } } if len(err) != 0 { diff --git a/logger/logger_test.go b/logger/logger_test.go index d8aa9ec..19a1e63 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/assert" ) +type contextKeyType string + func buildLogger() (Logger, *bytes.Buffer) { buf := bytes.NewBuffer(nil) logrusLogger := logrus.New() @@ -110,7 +112,8 @@ func TestContextLog(t *testing.T) { GlobalFields["testKey"] = "value" - ctx := context.Background() + ctx := context.WithValue(context.Background(), contextKeyType("ctxValue"), "value") + ctx = WithField(ctx, "bar", "baz") entry := logger(ctx, nil).WithField("a", "b") assert.Equal(t, logrus.Fields{ @@ -119,6 +122,7 @@ func TestContextLog(t *testing.T) { "a": "b", "testKey": "value", }, entry.Data) + assert.Equal(t, ctx, entry.Context) } func TestLogIfError(t *testing.T) { diff --git a/logrusbugsnag/bugsnag.go b/logrusbugsnag/bugsnag.go index bc0226b..040333c 100644 --- a/logrusbugsnag/bugsnag.go +++ b/logrusbugsnag/bugsnag.go @@ -94,7 +94,9 @@ func (hook *Hook) Fire(entry *logrus.Entry) error { } errWithStack := bugsnag_errors.New(notifyErr, skipFrames) - bugsnagErr := bugsnag.Notify(errWithStack, metadata) + + // Pass in our own metadata as well as the entry context, which might contain more data that Bugsnag can extract. + bugsnagErr := bugsnag.Notify(errWithStack, metadata, entry.Context) if bugsnagErr != nil { return ErrBugsnagSendFailed{bugsnagErr} } From 198145b0709556a001ec4db7a6b2a4a90889eed4 Mon Sep 17 00:00:00 2001 From: Adam Renberg Tamm Date: Mon, 11 Mar 2019 17:25:06 +0100 Subject: [PATCH 2/2] Add version constraint on logrus >= 1.4.0 --- Gopkg.lock | 6 +++--- Gopkg.toml | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 783cb60..967e0e2 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -209,12 +209,12 @@ version = "v1.0.0" [[projects]] - digest = "1:9d57e200ef5ccc4217fe0a34287308bac652435e7c6513f6263e0493d2245c56" + digest = "1:b73fe282e350b3ef2c71d8ff08e929e0b9670b1bb5b7fde1d3c1b4cd6e6dc8b1" name = "github.com/sirupsen/logrus" packages = ["."] pruneopts = "" - revision = "bcd833dfe83d3cebad139e4a29ed79cb2318bf95" - version = "v1.2.0" + revision = "dae0fa8d5b0c810a8ab733fbd5510c7cae84eca4" + version = "v1.4.0" [[projects]] digest = "1:c587772fb8ad29ad4db67575dad25ba17a51f072ff18a22b4f0257a4d9c24f75" diff --git a/Gopkg.toml b/Gopkg.toml index 33ee026..6abaf47 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -28,6 +28,10 @@ name = "github.com/stretchr/testify" version = ">= 1.2.1" +[[constraint]] + name = "github.com/sirupsen/logrus" + version = ">= 1.4.0" + [[override]] name = "github.com/gorilla/mux" branch = "subrouter-match-err"