Configure logs in context for Go

Combined with New Relic Logs, 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. This document explains how to configure logs in context and start getting log data.

Compatibility and requirements

Access to logs in context requires a New Relic APM Pro or Pro trial subscription and enabling of distributed tracing. Before enabling distributed tracing, read the transition guide to learn about the effects on existing APM features and set-up recommendations.

To use New Relic Logs with the Go agent, ensure your configuration meets the following requirements:

Configure logs in context with New Relic Logs

To configure New Relic logs in context with Go:

  1. Enable New Relic Logs 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 New Relic Logs

Confirm that you have New Relic Logs 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"
        "github.com/newrelic/go-agent/v3/integrations/logcontext/nrlogrusplugin"
        "github.com/newrelic/go-agent/v3/newrelic"
    )
  2. 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.

  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

Recommendations for learning more: