newrelic / go-agent

New Relic Go Agent
Apache License 2.0
763 stars 294 forks source link

Using logrus JSON formatter with logcontext-v2 #606

Open betabandido opened 1 year ago

betabandido commented 1 year ago

Summary

We would like to use the new logcontext v2, but continue using a JSON formatter that extracts the different logrus fields into individual fields in the log details in New Relic Logs.

Desired Behaviour

In our applications we use logcontext v1 (https://github.com/newrelic/go-agent/tree/master/v3/integrations/logcontext) together with the nrlogrusplugin. We have seen there is now a v2 available for logcontext. We would like to use this new version as it is able to easily annotate log statements outside of a transaction. Otherwise, we need to rely on something like

txn := newRelicApp.StartTransaction("dummy")
log.WithContext(newrelic.NewContext(context.Background(), txn)).
    Info("some message")
txn.End()

which seems a little bit too much just to log a message.

The issue with using logcontext v2 is that we have not been able to use a JSON formatter (New Relic's documentation only mentions the TextFormatter, so we wonder if it is the only one supported at this moment). We have tried:

log.SetFormatter(nrlogrus.NewFormatter(newRelicApp, &log.JSONFormatter{}))

and while we do see the logs on New Relic, the JSON fields are not extracted and they are simply listed in the log message field (as a string, instead of individual fields). That means we cannot easily query using NRQL.

Is it actually possible to use a JSON formatter with logcontext v2 and still get the old behavior (where the different JSON fields get extracted and are available in New Relic Logs)?

iamemilio commented 1 year ago

Hi, we fully support ANY valid logrus formatter, since we are able to extract the value from the logrus fields before they get formatted. This is correctly labeled as an enhancement, and is something we are planning to bring to the v2 logging products in the future.

betabandido commented 1 year ago

Do you have any timeline for this? In the meanwhile, do you have any suggestion other than doing something along the lines of the solution mentioned above? The one using logcontext v1 and this "trick":

txn := newRelicApp.StartTransaction("dummy")
log.WithContext(newrelic.NewContext(context.Background(), txn)).
    Info("some message")
txn.End()