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. Read on to learn how to configure logs in context and enrich your log data.
Compatibility and requirements
To use log monitoring with the Python agent, ensure your configuration meets the following requirements:
Configure logs in context with log management
To configure New Relic logs in context with Python:
- Enable log management 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 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 Python agent
Install or update to the most recent Python agent version, and enable distributed tracing.
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 formatterimport loggingfrom newrelic.agent import NewRelicContextFormatter# Instantiate a new log handlerhandler = logging.StreamHandler()# Instantiate the log formatter and add it to the log handlerformatter = NewRelicContextFormatter()handler.setFormatter(formatter)# Get the root logger and add the handler to itroot_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:
- 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/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.
For more help
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's data security and licenses documentation.