From eb05e4b3014c4991088f96434f55232ceb71c3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emir=20Ribi=C4=87?= Date: Mon, 29 Jul 2024 12:27:58 +0200 Subject: [PATCH] Add logger name to logrus integration (#866) * add logger name to logrus integration * fix tests --- logrus/logrusentry.go | 2 ++ logrus/logrusentry_test.go | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/logrus/logrusentry.go b/logrus/logrusentry.go index f9d921651..d95358655 100644 --- a/logrus/logrusentry.go +++ b/logrus/logrusentry.go @@ -13,6 +13,7 @@ import ( // The identifier of the Logrus SDK. const sdkIdentifier = "sentry.go.logrus" +const name = "logrus" // These default log field keys are used to pass specific metadata in a way that // Sentry understands. If they are found in the log fields, and the value is of @@ -153,6 +154,7 @@ func (h *Hook) entryToEvent(l *logrus.Entry) *sentry.Event { Extra: data, Message: l.Message, Timestamp: l.Time, + Logger: name, } key := h.key(FieldRequest) if req, ok := s.Extra[key].(*http.Request); ok { diff --git a/logrus/logrusentry_test.go b/logrus/logrusentry_test.go index ee208ab52..db303e43c 100644 --- a/logrus/logrusentry_test.go +++ b/logrus/logrusentry_test.go @@ -73,8 +73,9 @@ func Test_entryToEvent(t *testing.T) { "empty entry": { entry: &logrus.Entry{}, want: &sentry.Event{ - Level: "fatal", - Extra: map[string]any{}, + Level: "fatal", + Extra: map[string]any{}, + Logger: "logrus", }, }, "data fields": { @@ -85,8 +86,9 @@ func Test_entryToEvent(t *testing.T) { }, }, want: &sentry.Event{ - Level: "fatal", - Extra: map[string]any{"bar": "oink", "foo": 123.4}, + Level: "fatal", + Extra: map[string]any{"bar": "oink", "foo": 123.4}, + Logger: "logrus", }, }, "info level": { @@ -94,8 +96,9 @@ func Test_entryToEvent(t *testing.T) { Level: logrus.InfoLevel, }, want: &sentry.Event{ - Level: "info", - Extra: map[string]any{}, + Level: "info", + Extra: map[string]any{}, + Logger: "logrus", }, }, "message": { @@ -106,6 +109,7 @@ func Test_entryToEvent(t *testing.T) { Level: "fatal", Extra: map[string]any{}, Message: "the only thing we have to fear is fear itself", + Logger: "logrus", }, }, "timestamp": { @@ -116,6 +120,7 @@ func Test_entryToEvent(t *testing.T) { Level: "fatal", Extra: map[string]any{}, Timestamp: time.Unix(1, 2).UTC(), + Logger: "logrus", }, }, "http request": { @@ -132,6 +137,7 @@ func Test_entryToEvent(t *testing.T) { Method: http.MethodGet, Headers: map[string]string{"Host": "example.com"}, }, + Logger: "logrus", }, }, "error": { @@ -146,6 +152,7 @@ func Test_entryToEvent(t *testing.T) { Exception: []sentry.Exception{ {Type: "*errors.errorString", Value: "things failed", Stacktrace: &sentry.Stacktrace{Frames: []sentry.Frame{}}}, }, + Logger: "logrus", }, }, "non-error": { @@ -159,6 +166,7 @@ func Test_entryToEvent(t *testing.T) { Extra: map[string]any{ "error": "this isn't really an error", }, + Logger: "logrus", }, }, "error with stack trace": { @@ -192,6 +200,7 @@ func Test_entryToEvent(t *testing.T) { }, }, }, + Logger: "logrus", }, }, "user": { @@ -208,6 +217,7 @@ func Test_entryToEvent(t *testing.T) { User: sentry.User{ ID: "bob", }, + Logger: "logrus", }, }, "user pointer": { @@ -224,6 +234,7 @@ func Test_entryToEvent(t *testing.T) { User: sentry.User{ ID: "alice", }, + Logger: "logrus", }, }, "non-user": { @@ -237,6 +248,7 @@ func Test_entryToEvent(t *testing.T) { Extra: map[string]any{ "user": "just say no to drugs", }, + Logger: "logrus", }, }, }