Skip to content

Commit 8ca298a

Browse files
authored
fix(logging): Strip log record.name for more robust matching (#4411)
Hi! In the Python SDK, more specifically `sentry_sdk/integrations/logging.py`, a couple of loggers are ignored to avoid recursion errors. ```py _IGNORED_LOGGERS = set( ["sentry_sdk.errors", "urllib3.connectionpool", "urllib3.connection"] ) ``` Log records from these loggers are discarded, using an exact match on `record.name`. Unforunately, this breaks if the user modifies `record.name`, e.g. for formatting, which is what we were doing for log display (before becoming aware that Sentry relied on it after investigating an infinite recursion issue). ```py class _LengthFormatter(logging.Formatter): def format(self, record): """ Format a log record's header to a constant length """ if len(record.name) > _MAX_LOGGER_NAME_LENGTH: sep = "..." cut = _MAX_LOGGER_NAME_LENGTH // 3 - len(sep) record.name = record.name[:cut] + sep + record.name[-(_MAX_LOGGER_NAME_LENGTH - cut - len(sep)) :] record.name = record.name.ljust(_MAX_LOGGER_NAME_LENGTH) record.levelname = record.levelname.ljust(_MAX_LOGGER_LEVEL_NAME_LENGTH) return super().format(record) ``` As you can see, `record.name` is right-padded with blank spaces. We have found a workaround since, but given that it has taken us quite some time to find the issue, I thought that maybe it could affect others. This PR proposes matching `record.name.strip()` instead for increased robustness.
1 parent 4420c4d commit 8ca298a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

sentry_sdk/integrations/logging.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ def sentry_patched_callhandlers(self, record):
119119
# the integration. Otherwise we have a high chance of getting
120120
# into a recursion error when the integration is resolved
121121
# (this also is slower).
122-
if ignored_loggers is not None and record.name not in ignored_loggers:
122+
if (
123+
ignored_loggers is not None
124+
and record.name.strip() not in ignored_loggers
125+
):
123126
integration = sentry_sdk.get_client().get_integration(
124127
LoggingIntegration
125128
)
@@ -164,7 +167,7 @@ def _can_record(self, record):
164167
# type: (LogRecord) -> bool
165168
"""Prevents ignored loggers from recording"""
166169
for logger in _IGNORED_LOGGERS:
167-
if fnmatch(record.name, logger):
170+
if fnmatch(record.name.strip(), logger):
168171
return False
169172
return True
170173

tests/integrations/logging/test_logging.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ def test_ignore_logger(sentry_init, capture_events):
230230
assert not events
231231

232232

233+
def test_ignore_logger_whitespace_padding(sentry_init, capture_events):
234+
"""Here we test insensitivity to whitespace padding of ignored loggers"""
235+
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
236+
events = capture_events()
237+
238+
ignore_logger("testfoo")
239+
240+
padded_logger = logging.getLogger(" testfoo ")
241+
padded_logger.error("hi")
242+
assert not events
243+
244+
233245
def test_ignore_logger_wildcard(sentry_init, capture_events):
234246
sentry_init(integrations=[LoggingIntegration()], default_integrations=False)
235247
events = capture_events()

0 commit comments

Comments
 (0)