Skip to content

Commit 6110044

Browse files
authored
Merge pull request #937 from iamemilio/logrus-attributes
Logrus attributes
2 parents bc375fb + 74c0fbe commit 6110044

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

v3/integrations/logcontext-v2/nrlogrus/formatter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ func NewFormatter(app *newrelic.Application, formatter logrus.Formatter) Context
3030
// Format renders a single log entry.
3131
func (f ContextFormatter) Format(e *logrus.Entry) ([]byte, error) {
3232
logData := newrelic.LogData{
33-
Severity: e.Level.String(),
34-
Message: e.Message,
33+
Severity: e.Level.String(),
34+
Message: e.Message,
35+
Attributes: e.Data,
3536
}
3637

3738
logBytes, err := f.formatter.Format(e)

v3/integrations/logcontext-v2/nrlogrus/formatter_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,32 @@ func TestBackgroundLog(t *testing.T) {
114114
})
115115
}
116116

117+
func TestBackgroundLogWithFields(t *testing.T) {
118+
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
119+
newrelic.ConfigAppLogDecoratingEnabled(true),
120+
newrelic.ConfigAppLogForwardingEnabled(true),
121+
)
122+
out := bytes.NewBuffer([]byte{})
123+
log := newTextLogger(out, app.Application)
124+
message := "Hello World!"
125+
log.WithField("test field", []string{"a", "b"}).Info(message)
126+
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
127+
EntityGUID: integrationsupport.TestEntityGUID,
128+
Hostname: host,
129+
EntityName: integrationsupport.SampleAppName,
130+
})
131+
app.ExpectLogEvents(t, []internal.WantLog{
132+
{
133+
Severity: logrus.InfoLevel.String(),
134+
Message: message,
135+
Timestamp: internal.MatchAnyUnixMilli,
136+
Attributes: map[string]interface{}{
137+
"test field": []string{"a", "b"},
138+
},
139+
},
140+
})
141+
}
142+
117143
func TestJSONBackgroundLog(t *testing.T) {
118144
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
119145
newrelic.ConfigAppLogDecoratingEnabled(true),
@@ -192,3 +218,39 @@ func TestLogInContext(t *testing.T) {
192218

193219
txn.End()
194220
}
221+
222+
func TestLogInContextWithFields(t *testing.T) {
223+
app := integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn,
224+
newrelic.ConfigAppLogDecoratingEnabled(true),
225+
newrelic.ConfigAppLogForwardingEnabled(true),
226+
)
227+
out := bytes.NewBuffer([]byte{})
228+
log := newTextLogger(out, app.Application)
229+
txn := app.StartTransaction("test txn")
230+
231+
ctx := newrelic.NewContext(context.Background(), txn)
232+
message := "Hello World!"
233+
log.WithField("hi", 1).WithContext(ctx).Info(message)
234+
235+
logcontext.ValidateDecoratedOutput(t, out, &logcontext.DecorationExpect{
236+
EntityGUID: integrationsupport.TestEntityGUID,
237+
Hostname: host,
238+
EntityName: integrationsupport.SampleAppName,
239+
TraceID: txn.GetLinkingMetadata().TraceID,
240+
SpanID: txn.GetLinkingMetadata().SpanID,
241+
})
242+
txn.ExpectLogEvents(t, []internal.WantLog{
243+
{
244+
Severity: logrus.InfoLevel.String(),
245+
Message: message,
246+
Timestamp: internal.MatchAnyUnixMilli,
247+
SpanID: txn.GetLinkingMetadata().SpanID,
248+
TraceID: txn.GetLinkingMetadata().TraceID,
249+
Attributes: map[string]interface{}{
250+
"hi": 1,
251+
},
252+
},
253+
})
254+
255+
txn.End()
256+
}

0 commit comments

Comments
 (0)