Skip to content

Commit

Permalink
logrus with fields
Browse files Browse the repository at this point in the history
  • Loading branch information
iamemilio committed Jul 29, 2024
1 parent 94f775b commit 3d2b4a8
Show file tree
Hide file tree
Showing 2 changed files with 29 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
26 changes: 26 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

0 comments on commit 3d2b4a8

Please sign in to comment.