5454 create_attributes ,
5555 create_user_attributes ,
5656 process_user_attribute ,
57+ resolve_logging_context_attributes ,
5758 truncate ,
5859)
5960from newrelic .core .attribute_filter import (
@@ -1524,7 +1525,7 @@ def set_transaction_name(self, name, group=None, priority=None):
15241525 self ._group = group
15251526 self ._name = name
15261527
1527- def record_log_event (self , message , level = None , timestamp = None , priority = None ):
1528+ def record_log_event (self , message , level = None , timestamp = None , attributes = None , priority = None ):
15281529 settings = self .settings
15291530 if not (
15301531 settings
@@ -1537,18 +1538,62 @@ def record_log_event(self, message, level=None, timestamp=None, priority=None):
15371538
15381539 timestamp = timestamp if timestamp is not None else time .time ()
15391540 level = str (level ) if level is not None else "UNKNOWN"
1541+ context_attributes = attributes # Name reassigned for clarity
15401542
1541- if not message or message .isspace ():
1542- _logger .debug ("record_log_event called where message was missing. No log event will be sent." )
1543- return
1543+ # Unpack message and attributes from dict inputs
1544+ if isinstance (message , dict ):
1545+ message_attributes = {k : v for k , v in message .items () if k != "message" }
1546+ message = message .get ("message" , "" )
1547+ else :
1548+ message_attributes = None
1549+
1550+ if message is not None :
1551+ # Coerce message into a string type
1552+ if not isinstance (message , six .string_types ):
1553+ try :
1554+ message = str (message )
1555+ except Exception :
1556+ # Exit early for invalid message type after unpacking
1557+ _logger .debug (
1558+ "record_log_event called where message could not be converted to a string type. No log event will be sent."
1559+ )
1560+ return
1561+
1562+ # Truncate the now unpacked and string converted message
1563+ message = truncate (message , MAX_LOG_MESSAGE_LENGTH )
1564+
1565+ # Collect attributes from linking metadata, context data, and message attributes
1566+ collected_attributes = {}
1567+ if settings and settings .application_logging .forwarding .context_data .enabled :
1568+ if context_attributes :
1569+ context_attributes = resolve_logging_context_attributes (
1570+ context_attributes , settings .attribute_filter , "context."
1571+ )
1572+ if context_attributes :
1573+ collected_attributes .update (context_attributes )
1574+
1575+ if message_attributes :
1576+ message_attributes = resolve_logging_context_attributes (
1577+ message_attributes , settings .attribute_filter , "message."
1578+ )
1579+ if message_attributes :
1580+ collected_attributes .update (message_attributes )
1581+
1582+ # Exit early if no message or attributes found after filtering
1583+ if (not message or message .isspace ()) and not context_attributes and not message_attributes :
1584+ _logger .debug (
1585+ "record_log_event called where no message and no attributes were found. No log event will be sent."
1586+ )
1587+ return
15441588
1545- message = truncate (message , MAX_LOG_MESSAGE_LENGTH )
1589+ # Finally, add in linking attributes after checking that there is a valid message or at least 1 attribute
1590+ collected_attributes .update (get_linking_metadata ())
15461591
15471592 event = LogEventNode (
15481593 timestamp = timestamp ,
15491594 level = level ,
15501595 message = message ,
1551- attributes = get_linking_metadata () ,
1596+ attributes = collected_attributes ,
15521597 )
15531598
15541599 self ._log_events .add (event , priority = priority )
@@ -2062,7 +2107,7 @@ def record_ml_event(event_type, params, application=None):
20622107 application .record_ml_event (event_type , params )
20632108
20642109
2065- def record_log_event (message , level = None , timestamp = None , application = None , priority = None ):
2110+ def record_log_event (message , level = None , timestamp = None , attributes = None , application = None , priority = None ):
20662111 """Record a log event.
20672112
20682113 Args:
@@ -2073,12 +2118,12 @@ def record_log_event(message, level=None, timestamp=None, application=None, prio
20732118 if application is None :
20742119 transaction = current_transaction ()
20752120 if transaction :
2076- transaction .record_log_event (message , level , timestamp )
2121+ transaction .record_log_event (message , level , timestamp , attributes = attributes )
20772122 else :
20782123 application = application_instance (activate = False )
20792124
20802125 if application and application .enabled :
2081- application .record_log_event (message , level , timestamp , priority = priority )
2126+ application .record_log_event (message , level , timestamp , attributes = attributes , priority = priority )
20822127 else :
20832128 _logger .debug (
20842129 "record_log_event has been called but no transaction or application was running. As a result, "
@@ -2089,7 +2134,7 @@ def record_log_event(message, level=None, timestamp=None, application=None, prio
20892134 timestamp ,
20902135 )
20912136 elif application .enabled :
2092- application .record_log_event (message , level , timestamp , priority = priority )
2137+ application .record_log_event (message , level , timestamp , attributes = attributes , priority = priority )
20932138
20942139
20952140def accept_distributed_trace_payload (payload , transport_type = "HTTP" ):
0 commit comments