Logs in context for the Go agent connects your logs and APM data in New Relic, giving full context to high-level events, as well as providing high value data to specific log lines. Read on to learn how to configure logs in context and enrich your log data.
Compatibility and requirements
To use log management with the Go agent, ensure your configuration meets the following requirements:
Configure logs in context with log management
To configure New Relic logs in context with Go:
- Enable log management with a compatible log forwarding plugin.
- Install or update the Go agent.
- Configure logs in context using the Logrus extension.
- Optional: Configure the Logrus extension.
- Check for logging data.
Enable log management
Confirm that you have log management enabled, with a compatible log forwarding plugin installed to send your application logs to New Relic.
Install or update the Go agent
Install or update to the most recent Go agent version, and enable Distributed tracing.
Configure logs in context using the Logrus extension
To configure the Logrus plugin:
This step assumes that you have imported the following packages in files where Logrus is configured:
import (log "github.com/sirupsen/logrus""github.com/newrelic/go-agent/v3/integrations/logcontext/nrlogrusplugin""github.com/newrelic/go-agent/v3/newrelic")Set the Logrus formatter to
nrlogrusplugin.ContextFormatter
:logger := log.New()logger.SetFormatter(nrlogrusplugin.ContextFormatter{})Or, if you are using the Logrus standard logger:
log.SetFormatter(nrlogrusplugin.ContextFormatter{})The logger will now look for a
newrelic.Transaction
inside its context and decorate logs accordingly.Update your standard logging call to include contextual logging and pass this to the logger.
For example, instead of
logger.Info("Hello New Relic!")
, usectx := newrelic.NewContext(context.Background(), txn)logger.WithContext(ctx).Info("Hello New Relic!")where
txn
is the current running transaction.
Check for logging data
To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:
- Includes
trace.id
andspan.id
fields - Is properly-formatted JSON lines
If everything is configured correctly and your data is being reported, you should see data logs in the New Relic Logs UI using the query operator has:span.id OR has:trace.id
What's next?
Now that you've set up APM logs in context, here are some potential next steps:
- Explore your data using the Logs UI.
- Troubleshoot errors with distributed tracing, stack traces, application logs, and more.
- Query your data and create custom dashboards or alerts.
その他のヘルプ
さらに支援が必要な場合は、これらのサポートと学習リソースを確認してください:
- Explorers Hubでは、コミュニティからのサポートを受けたり、ディスカッションに参加したりすることができます。
- 当社サイトで答えを見つけて、サポートポータルの使用方法を確認してください。
- Linux、Windows、およびmacOS向けトラブルシューティングツールであるNew Relic Diagnosticsを実行してください。
- New Relicのデータセキュリティとライセンスドキュメント をご確認ください。