Skip to content

Commit

Permalink
Add logger name to logrus integration (#866)
Browse files Browse the repository at this point in the history
* add logger name to logrus integration

* fix tests
  • Loading branch information
ribice authored Jul 29, 2024
1 parent c6c965c commit eb05e4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 18 additions & 6 deletions logrus/logrusentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -85,17 +86,19 @@ 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": {
entry: &logrus.Entry{
Level: logrus.InfoLevel,
},
want: &sentry.Event{
Level: "info",
Extra: map[string]any{},
Level: "info",
Extra: map[string]any{},
Logger: "logrus",
},
},
"message": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -132,6 +137,7 @@ func Test_entryToEvent(t *testing.T) {
Method: http.MethodGet,
Headers: map[string]string{"Host": "example.com"},
},
Logger: "logrus",
},
},
"error": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -192,6 +200,7 @@ func Test_entryToEvent(t *testing.T) {
},
},
},
Logger: "logrus",
},
},
"user": {
Expand All @@ -208,6 +217,7 @@ func Test_entryToEvent(t *testing.T) {
User: sentry.User{
ID: "bob",
},
Logger: "logrus",
},
},
"user pointer": {
Expand All @@ -224,6 +234,7 @@ func Test_entryToEvent(t *testing.T) {
User: sentry.User{
ID: "alice",
},
Logger: "logrus",
},
},
"non-user": {
Expand All @@ -237,6 +248,7 @@ func Test_entryToEvent(t *testing.T) {
Extra: map[string]any{
"user": "just say no to drugs",
},
Logger: "logrus",
},
},
}
Expand Down

0 comments on commit eb05e4b

Please sign in to comment.