---
title: Python agent logs in context
source: https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-python
---

With our Python APM agent, you can get **logs in context**, which lets you see your app logs in the context of your other New Relic data. For general information on this feature, see [APM logs in context](https://docs.newrelic.com/docs/apm/new-relic-apm/getting-started/get-started-logs-context).

> #### 💡 TIP
>
> Got lots of Python logs? Check out our [tutorial on how to optimize and manage them](https://docs.newrelic.com/docs/tutorial-large-logs/get-started-managing-large-logs/).

## Automatic logs in context options [#automatic]

You have three options to configure APM logs in context to send your app's logs and linking metadata automatically to New Relic.

**Option 1 (simplest): Let the agent decorate and forward your logs.**

This is the simplest approach, and it's a great choice for developers who may not have the access or interest in setting up a log forwarder, or for accounts that want to see the power of logs and other linking metadata in context of their apps, without a lot of overhead.

Using this option, the agent adds `span.id`, `trace.id`, `hostname`, `entity.guid`, and `entity.name`. [Learn about log forwarding limitations.](https://docs.newrelic.com/docs/logs/logs-context/get-started-logs-context#forwarding-details)

All you need to do is install an agent version with log forwarding capabilities ([Python agent 7.12.0.176 or higher](https://docs.newrelic.com/docs/release-notes/agent-release-notes/python-release-notes/)). If forwarding is disabled, you can use this configuration:

`newrelic.ini`:

````ini
application_logging.enabled=true
application_logging.forwarding.enabled=true
```

Environment variable:

```ini
NEW_RELIC_APPLICATION_LOGGING_ENABLED=true
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true
```

This option is on by default beginning with [agent version 7.16.0.178](/docs/release-notes/agent-release-notes/python-release-notes/python-agent-71600178).

<DNT>
  **Optional adjustments:**
</DNT>

Once this is enabled, you also have control over the maximum number of logs sent to New Relic every minute. The default value is 10,000. If more than 10,000 logs are received in a 60-second window, your logs will begin to be sampled.

Set it to a higher number to receive more logs. Set it to a lower number to receive fewer logs. Any integer up to 100,000 is valid.

`newrelic.ini`:

For Python agent version 11.0.0+:
```ini
application_logging.forwarding.max_samples_stored=10000
```

For Python agent version 10.17.0 and below:
```ini
event_harvest_config.harvest_limits.log_event_data=10000
```

Environment variable:

```ini
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_MAX_SAMPLES_STORED=10000
```

If you have an existing log forwarding solution and are updating your agent to use automatic logs in context, be sure to <DNT>**disable your manual log forwarder**</DNT>. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your [specific log forwarder](/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding).

````

**Option 2: Let the agent decorate your logs.**

Already have a log forwarder you like? We've got you covered! Language agents can decorate your logs with the linking metadata needed to provide access to automatic logs-in-context features.

This option should not be used with in-agent forwarding. Using an [external log forwarder](https://docs.newrelic.com/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding) to send logs to New Relic while in-agent forwarding is enabled will cause your logs to be sent up twice to New Relic. Depending on your account, this may result in double billing.

This option should also not be used with [the manual log decorating formatter](#3-old-formatter). If you have references to the manual formatter in your codebase, please remove them before enabling this option.

1.  If you want to use this option, make sure you have the in-agent forwarding configuration option disabled.

    `newrelic.ini`:

    ```ini
    application_logging.forwarding.enabled=false
    ```

2.  Enable log decorating in your configuration, then relaunch the agent to start decorating your logs.

    `newrelic.ini`:

    ```ini
    application_logging.enabled=true
    application_logging.forwarding.enabled=false
    application_logging.local_decorating.enabled=true
    ```

    Environment variable:

    ```
    NEW_RELIC_APPLICATION_LOGGING_ENABLED: "true"
    NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED: "false"
    NEW_RELIC_APPLICATION_LOGGING_LOCAL_DECORATING_ENABLED: "true"
    ```

    Our decorator adds five attributes to every log message: `entity.guid`, `entity.name`, `hostname`, `trace.id`, and  `span.id`. Example:

    ```
    This is my log message. NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|
    ```

    If the log message is empty or blank, output message will also be empty. Example:

    ```
    NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|.
    ```

    The output log message will be an empty string.

    Some attributes may be empty if the log occurred outside a transaction or if they are not applicable to your application's context.

    We recommend this option over the manual decorating formatter, `NewRelicContextFormatter`.

**Option 3: Use the manual process to forward and decorate logs.**

You can also use our manual installation option to enable logs in context for APM apps monitored by Python.

1.  Make sure you have already [set up logging in New Relic](https://docs.newrelic.com/docs/logs/enable-log-management-new-relic/enable-log-monitoring-new-relic/enable-log-management-new-relic/). This includes configuring a supported log forwarder that collects your application logs and extends the metadata that is forwarded to New Relic.

2.  [Install](https://docs.newrelic.com/docs/agents/python-agent/installation/standard-python-agent-install/) or [update](https://docs.newrelic.com/docs/agents/python-agent/installation/update-python-agent) to the latest Python agent version, and [enable distributed tracing](https://docs.newrelic.com/docs/distributed-tracing/enable-configure/quick-start/). Use [Python agent version 5.4.0 or higher](https://docs.newrelic.com/docs/release-notes/agent-release-notes/python-release-notes/) for logs in context.

3.  Configure logs in context for your log handler.
    Before language agents had the ability to forward and decorate logs, you could enable logs in context by 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. For more information about configuring log handlers, see the [Python.org documentation](https://docs.python.org/3/library/logging.handlers.html).

    ```python
    # 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)
    ```

4.  To verify that you have configured the log appender correctly, run your application, then check your [logs data in New Relic](https://docs.newrelic.com/docs/logs/log-management/ui-data/use-logs-ui/) using the query operator `has:span.id has:trace.id`.

    If everything is configured correctly and your data is being forwarded to New Relic with the enriched metadata, your logs should now be emitted as JSON and contain `trace.id` and `span.id` fields. If you don't see log data in the UI, follow the [troubleshooting procedures](https://docs.newrelic.com/docs/logs/log-management/troubleshooting/no-log-data-appears-ui/).

## Secure your data [#obfuscation]

Your logs may include sensitive information protected by HIPAA or other compliance protocols. By default we obfuscate number patterns that appear to be for items such as credit cards or Social Security numbers, but you may need to hash or mask additional information.

For more information, see our documentation about [obfuscation expressions and rules](https://docs.newrelic.com/docs/logs/ui-data/obfuscation-ui/). You can hash or mask your log data by using the New Relic UI or by using NerdGraph, our GraphQL API.

## What's next? [#what-next]

After you set up APM logs in context, make the most of your logging data:

-   Explore the logging data across your platform with our [logs UI](https://docs.newrelic.com/docs/logs/log-management/ui-data/use-logs-ui/).
-   See your logs in context of your app's performance in the [APM UI](https://docs.newrelic.com/docs/logs/log-management/ui-data/use-logs-ui/#links). Troubleshoot [errors](https://docs.newrelic.com/docs/apm/apm-ui-pages/error-analytics/errors-page-find-fix-verify-problems/) with [distributed tracing](https://docs.newrelic.com/docs/apm/distributed-tracing/ui-data/understand-use-distributed-tracing-data), stack traces, application logs, and more.
-   Get deeper visibility into both your application and your platform performance data by forwarding your logs with our [infrastructure agent](https://docs.newrelic.com/docs/logs/enable-log-management-new-relic/enable-log-monitoring-new-relic/forward-your-logs-using-infrastructure-agent/). Review your [infrastructure logs](https://docs.newrelic.com/docs/logs/log-management/ui-data/use-logs-ui/#links) in the UI.
-   Set up [alerts](https://docs.newrelic.com/docs/alerts-applied-intelligence/new-relic-alerts/alert-conditions/create-alert-conditions/).
-   [Query your data](https://docs.newrelic.com/docs/query-your-data/explore-query-data/get-started/introduction-querying-new-relic-data/) and [create dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards/).
