Enable logs-in-context for Python

BETA

Combined with New Relic Logs, logs-in-context for the Python 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 enable this feature and get started using it.

New Relic's logs-in-context (linking logs directly to Applications, APM errors, and APM traces and spans) for New Relic Logs is currently available as a beta feature. Your use of the early access service is at your own risk. New Relic disclaims all warranties, express or implied, regarding the beta services.

Compatibility and requirements

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

New Relic Logs must be enabled and a compatible logging plugin must be installed prior to enabling logs-in-context. For more information, see Introduction to New Relic Logs.

Enable logs-in-context with New Relic Logs

To enable New Relic logs-in-context with Python:

  1. Install or update the Python agent.
  2. Configure the Python logging framework to use the NewRelicContextFormatter.
  3. Check for logging data.

Install or update the Python agent

Install or update to the most recent Python agent version, and enable Distributed tracing.

Configure the new RelicContextFormatter

Enabling logs in Python is as simple as instantiating a log formatter and adding it to your log handler. This example uses a StreamHandler which by default writes logs to sys.stderr, but any handler can be used. See the Python docs for information about configuring log handlers.

# Import the logging module and the New Relic log formatter
import logging
from newrelic.agent import NewRelicContextFormatter

# Instantiate a new log handler
handler = logging.StreamHandler()

# Instantiate the log formatter and add it to the log handler
formatter = NewRelicContextFormatter()
handler.setFormatter(formatter)

# Get the root logger and add the handler to it
root_logger = logging.getLogger()
root_logger.addHandler(handler)

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/trace.id

What's next?

Once you've enabled logs-in-context, here are some potential next steps:

For more help

Recommendations for learning more: