Skip to content

Commit b313c30

Browse files
committed
Include logger name in FnApiLogRecordHandler log entries
This change adds the logger name (record.name) to the custom_data field of LogEntry protos in FnApiLogRecordHandler. This is useful for filtering and debugging logs in complex pipelines. Fixes #37146
1 parent ca27526 commit b313c30

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
* Support configuring Firestore database on ReadFn transforms (Java) ([#36904](https://github.com/apache/beam/issues/36904)).
7676
* (Python) Inference args are now allowed in most model handlers, except where they are explicitly/intentionally disallowed ([#37093](https://github.com/apache/beam/issues/37093)).
77+
* Include logger name in FnApiLogRecordHandler log entries for filtering and debugging (Python) ([#37146](https://github.com/apache/beam/issues/37146)).
7778

7879
## Breaking Changes
7980

sdks/python/apache_beam/runners/worker/log_handler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def emit(self, record: logging.LogRecord) -> None:
144144
current_state.name_context.transform_id):
145145
log_entry.transform_id = current_state.name_context.transform_id
146146

147+
# Include the logger name in custom_data for filtering and debugging.
148+
if record.name:
149+
log_entry.custom_data.fields['logger'].string_value = record.name
150+
147151
try:
148152
self._log_entry_queue.put(log_entry, block=False)
149153
except queue.Full:

sdks/python/apache_beam/runners/worker/log_handler_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ def test_context(self):
233233
finally:
234234
statesampler.set_current_tracker(None)
235235

236+
def test_logger_name_in_custom_data(self):
237+
"""Tests that logger name is included in custom_data."""
238+
_LOGGER.info('test message')
239+
self.fn_log_handler.close()
240+
241+
log_entry = self.test_logging_service.log_records_received[0].log_entries[0]
242+
self.assertEqual(
243+
log_entry.custom_data.fields['logger'].string_value, __name__)
244+
236245
def test_extracts_transform_id_during_exceptions(self):
237246
"""Tests that transform ids are captured during user code exceptions."""
238247
descriptor = beam_fn_api_pb2.ProcessBundleDescriptor()

0 commit comments

Comments
 (0)