Skip to content

Latest commit

 

History

History
163 lines (120 loc) · 4.86 KB

recordlogevent-python-agent-api.mdx

File metadata and controls

163 lines (120 loc) · 4.86 KB
title type shortDescription tags metaDescription freshnessValidatedDate
record_log_event (Python agent API)
apiDoc
Records a log event for use in logging in context
Agents
Python agent
Python agent API
Python API: This call records a log event for use in logging in context.
never

Syntax

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 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

  <th>
    Description
  </th>
</tr>
Parameter
`message`
    _string_, _dictionary_
  </td>

  <td>
    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](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.enabled) and optionally configure [include](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.include) and [exclude](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.exclude) rules.
  </td>
</tr>

<tr>
  <td>
    `level`

    _string_
  </td>

  <td>
    Optional. Defines the logging level. Defaults to `UNKNOWN`.
  </td>
</tr>

<tr>
  <td>
    `timestamp`

    _float_
  </td>

  <td>
    Optional. Defines the timestamp of the log message. Defaults to `time.time()`.
  </td>
</tr>

<tr>
  <td>
    `attributes`

    _dictionary_
  </td>

  <td>
    Optional. Items included in this dictionary will be considered context data attributes under the prefix `context.`.

    To report these attributes, [enable context data forwarding](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.enabled) and optionally configure [include](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.include) and [exclude](/docs/agents/python-agent/configuration/python-agent-configuration#application_logging.forwarding.context_data.exclude) rules.
  </td>
</tr>

<tr>
  <td>
    `application`

    _object_
  </td>

  <td>
    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`](/docs/agents/python-agent/python-agent-api/application) function.
  </td>
</tr>

<tr>
  <td>
    `priority`

    _object_
  </td>

  <td>
    Optional. Sets the priority of the log event. See [`event_harvest_config.harvest_limits.log_event_data`](/docs/apm/agents/python-agent/configuration/python-agent-configuration#event_harvest_config.harvest_limits.log_event_data) for additional information on how priority affects logging events.
  </td>
</tr>
This setting is disabled when [high security mode](/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:

@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:

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:

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...