Skip to content

Commit

Permalink
Merge pull request #937 from iamemilio/logrus-attributes
Browse files Browse the repository at this point in the history
Logrus attributes
  • Loading branch information
iamemilio authored Jul 30, 2024
2 parents bc375fb + 74c0fbe commit 6110044
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
5 changes: 3 additions & 2 deletions v3/integrations/logcontext-v2/nrlogrus/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func NewFormatter(app *newrelic.Application, formatter logrus.Formatter) Context
// Format renders a single log entry.
func (f ContextFormatter) Format(e *logrus.Entry) ([]byte, error) {
logData := newrelic.LogData{
Severity: e.Level.String(),
Message: e.Message,
Severity: e.Level.String(),
Message: e.Message,
Attributes: e.Data,
}

logBytes, err := f.formatter.Format(e)
Expand Down
62 changes: 62 additions & 0 deletions v3/integrations/logcontext-v2/nrlogrus/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ func TestBackgroundLog(t *testing.T) {
})
}

func TestBackgroundLogWithFields(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
newrelic.ConfigAppLogForwardingEnabled(true),
)
out := bytes.NewBuffer([]byte{})
log := newTextLogger(out, app.Application)
message := "Hello World!"
log.WithField("test field", []string{"a", "b"}).Info(message)
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
})
app.ExpectLogEvents(t, []internal.WantLog{
{
Severity: logrus.InfoLevel.String(),
Message: message,
Timestamp: internal.MatchAnyUnixMilli,
Attributes: map[string]interface{}{
"test field": []string{"a", "b"},
},
},
})
}

func TestJSONBackgroundLog(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
Expand Down Expand Up @@ -192,3 +218,39 @@ func TestLogInContext(t *testing.T) {

txn.End()
}

func TestLogInContextWithFields(t *testing.T) {
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
newrelic.ConfigAppLogDecoratingEnabled(true),
newrelic.ConfigAppLogForwardingEnabled(true),
)
out := bytes.NewBuffer([]byte{})
log := newTextLogger(out, app.Application)
txn := app.StartTransaction("test txn")

ctx := newrelic.NewContext(context.Background(), txn)
message := "Hello World!"
log.WithField("hi", 1).WithContext(ctx).Info(message)

logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
EntityGUID: integrationsupport.TestEntityGUID,
Hostname: host,
EntityName: integrationsupport.SampleAppName,
TraceID: txn.GetLinkingMetadata().TraceID,
SpanID: txn.GetLinkingMetadata().SpanID,
})
txn.ExpectLogEvents(t, []internal.WantLog{
{
Severity: logrus.InfoLevel.String(),
Message: message,
Timestamp: internal.MatchAnyUnixMilli,
SpanID: txn.GetLinkingMetadata().SpanID,
TraceID: txn.GetLinkingMetadata().TraceID,
Attributes: map[string]interface{}{
"hi": 1,
},
},
})

txn.End()
}

0 comments on commit 6110044

Please sign in to comment.