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 configure logs in context and start getting log data.
Compatibility and requirements
Access to logs in context requires a New Relic APM Pro 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 Python agent, ensure your configuration meets the following requirements:
Configure logs in context with New Relic Logs
To configure New Relic logs in context with Python:
- Enable New Relic Logs with a compatible log forwarding plugin.
- Install or update the Python agent.
- Configure the Python logging framework to use the NewRelicContextFormatter.
- Check for logging data.
Enable New Relic Logs
Install or update the Python agent
Configure the NewRelicContextFormatter
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:
- Is properly-formatted JSON lines
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, charts, or alerts.