• /
  • Log in
  • Free account

Configure logs in context for Go

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:

  1. Enable log management with a compatible log forwarding plugin.
  2. Install or update the Go agent.
  3. Configure logs in context using the Logrus extension.
  4. Optional: Configure the Logrus extension.
  5. 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:

  1. This step assumes that you have imported the following packages in files where Logrus is configured:

    import (
    log "github.com/sirupsen/logrus"
  2. Set the Logrus formatter to nrlogrusplugin.ContextFormatter:

    logger := log.New()

    Or, if you are using the Logrus standard logger:


    The logger will now look for a newrelic.Transaction inside its context and decorate logs accordingly.

  3. Update your standard logging call to include contextual logging and pass this to the logger.

    For example, instead of logger.Info("Hello New Relic!"), use

    ctx := 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:

  1. Includes trace.id and span.id fields
  2. 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:

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.