---
title: record_log_event (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/recordlogevent-python-agent-api
---

## Syntax

```py
newrelic.agent.record_log_event(message, level=None, timestamp=None, attributes=None, application=None, priority=None)
```

Records a log event for use in logging in context.

## Requirements

Python agent version 8.5.0 or higher.

## Description

This records a [log event](https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-python) that can be viewed and queried in the New Relic UI. If you want to use this outside of the context of a monitored transaction, use the `application` parameter.

## Parameters

| Parameter                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `message` _string_, _dictionary_ | Required. The `message` that defines the log message. For dictionary values, the key `message` will be extracted if available, and any other items will be considered context data attributes under the prefix `message.`. To report these attributes, [enable context data forwarding](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-enabled) and optionally configure [include](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-include) and [exclude](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-exclude) rules. |
| `level` _string_                 | Optional. Defines the logging level. Defaults to `UNKNOWN`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `timestamp` _float_              | Optional. Defines the timestamp of the log message. Defaults to `time.time()`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `attributes` _dictionary_        | Optional. Items included in this dictionary will be considered context data attributes under the prefix `context.`. To report these attributes, [enable context data forwarding](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-enabled) and optionally configure [include](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-include) and [exclude](https://docs.newrelic.com/docs/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-context-data-exclude) rules.                                                                                                        |
| `application` _object_           | Optional. If you want to record a log event outside of the context of a monitored transaction, use this to associate the call with a specific application object. An application object can be obtained using the [`newrelic.agent.application`](https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/application) function.                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `priority` _object_              | Optional. Sets the priority of the log event. See [`application_logging.forwarding.max_samples_stored`](https://docs.newrelic.com/docs/apm/agents/python-agent/configuration/python-agent-configuration#application-logging-forwarding-max-samples-stored) for additional information on how priority affects logging events.                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |

> #### ⚠️ IMPORTANT
>
> This setting is disabled when [high security mode](https://docs.newrelic.com/docs/apm/agents/python-agent/getting-started/apm-agent-security-python/#restricted) is enabled.

## Return values

None.

## Examples

### Record log event in background task [#bg-task-event]

Here's an example of recording a log event associated with a background task:

```py
@newrelic.agent.background_task()
def bg_task():
    # do some type of work in this background task...
    application = newrelic.agent.application()
    newrelic.agent.record_log_event('My log message.', application)
```

### Record log event in transaction [#transaction-event]

An example of recording a log event inside a transaction:

```py
def fetch():
    newrelic.agent.record_log_event('Fetching data.')
    # do some type of work in this transaction...
```

### Record log event with context data attributes [#context-data-example]

Here's an example of recording a log event using message attributes and context attributes:

```py
def fetch(product_id):
    newrelic.agent.record_log_event({"message": "Fetching data", "product_id": product_id}, attributes={"thread_id": threading.get_ident()})
    # do some type of work in this transaction...
```
