• EnglishEspañol日本語한국어Português
  • Log inStart now

record_log_event (Python agent API)

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

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 and optionally configure include and 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 and optionally configure include and 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 function.

priority

object

Optional. Sets the priority of the log event. See event_harvest_config.harvest_limits.log_event_data for additional information on how priority affects logging events.

Return values

None.

Examples

Record log event in background task

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

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

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...
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.