Repository navigation
fix(logger): route log, warn, and fatal through buffer dispatch - #8871
leandrodamascena merged 4 commits into
Conversation
|
Hi @leandrodamascena, this PR is ready for review. It routes |
Manishram-ai
left a comment
There was a problem hiding this comment.
Thanks for picking this up, the _log dispatcher reads much better than the three copies. I checked out the branch and ran the logger tests (180 passed), mypy and a few custom levels, and found two things, both with suggestions below. With both applied, tests/functional/logger and tests/unit/logger pass (207 with the extra test), and mypy, ty and ruff are clean.
| # 1. Buffer is completely disabled - log right away | ||
| # 2. DEBUG is the maximum level of buffer, so, can't bypass if enabled | ||
| # 3. Store in buffer for potential later processing | ||
| level_num = logging._checkLevel(level) |
There was a problem hiding this comment.
make mypy fails on this line with Module has no attribute "_checkLevel" [attr-defined], since typeshed doesn't expose it. This does the same with public calls, and gives the same ValueError for a name it doesn't know, like "info":
| level_num = logging._checkLevel(level) | |
| level_num = logging.getLevelName(level) if isinstance(level, str) else level | |
| if not isinstance(level_num, int): | |
| raise ValueError(f"Unknown level: {level!r}") |
There was a problem hiding this comment.
Applied! Replaced logging._checkLevel with logging.getLevelName(level) if isinstance(level, str) else level and added the ValueError check for unknown levels.
| level_name = logging.getLevelName(level_num) | ||
| if _check_minimum_buffer_log_level(self._buffer_config.buffer_at_verbosity, level_name): |
There was a problem hiding this comment.
A custom level above buffer_at_verbosity ends up in the buffer here. getLevelName(25) returns "Level 25" (or "NOTICE" after addLevelName), _check_minimum_buffer_log_level doesn't know that name and returns False, so logger.log(25, "...") with the default buffer_at_verbosity="DEBUG" gets buffered while logger.info() prints right away. On develop that call goes straight to the stdlib logger and prints.
Comparing the numbers skips the name lookup, the same way _determine_log_level compares against buffer_at_verbosity:
| level_name = logging.getLevelName(level_num) | |
| if _check_minimum_buffer_log_level(self._buffer_config.buffer_at_verbosity, level_name): | |
| if level_num > logging.getLevelName(self._buffer_config.buffer_at_verbosity): |
With this, the _check_minimum_buffer_log_level import at the top of the file isn't used here anymore, so ruff will ask you to drop it. This is the test I used, it fails on the current head and passes with the change:
def test_logger_log_method_does_not_buffer_custom_level_above_threshold(stdout, service_name, monkeypatch):
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
# GIVEN A logger that buffers DEBUG logs only
logger_buffer_config = LoggerBufferConfig(max_bytes=10240, buffer_at_verbosity="DEBUG")
logger = Logger(level="DEBUG", service=service_name, stream=stdout, buffer_config=logger_buffer_config)
# WHEN Logging with a custom level between INFO and WARNING
logger.log(25, "custom level via log()")
# THEN Message should be output right away, like info()
log = capture_multiple_logging_statements_output(stdout)
assert len(log) == 1
assert log[0]["message"] == "custom level via log()"There was a problem hiding this comment.
Applied! Switched to direct numeric comparison level_num > logging.getLevelName(self._buffer_config.buffer_at_verbosity) to support custom levels, dropped the unused _check_minimum_buffer_log_level import, and added your regression test. All 207 logger tests pass and mypy / ruff are clean. Thank you for the great suggestions!
|
Thank you for the thorough review and suggestions @Manishram-ai! Both items have been implemented in commit
All 207 tests in |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8871 +/- ##
===========================================
- Coverage 97.08% 97.08% -0.01%
===========================================
Files 327 327
Lines 16865 16860 -5
Branches 1545 1543 -2
===========================================
- Hits 16374 16369 -5
Misses 345 345
Partials 146 146 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks @dhaiirryyaa, this is the shape I asked for in the issue: one |
Resolves the overlap with aws-powertools#8837: the buffer record keeps the stacklevel-aware frame walk from develop, _log passes stacklevel through to the buffer, and both sets of buffer tests stay.
|
@dhaiirryyaa I merged develop into your branch myself to keep 3.42.0 moving, since #8837 landed on the same lines. The resolution is what I described earlier: the buffer record keeps the stacklevel-aware frame walk from develop, |
leandrodamascena
left a comment
There was a problem hiding this comment.
Approved on c8dcce5: logger suite 264 passed, required-packages nox session green, and my side-by-side probes for buffering, stacklevel, exc_info and timestamps all match on the merged tree.
|
0bbc252
into
aws-powertools:develop



Issue number: closes #8827
Summary
Changes
_log()dispatcher.log(self, level, msg, *args, **kwargs)to route through the buffer and auto-flush on error log.warn = warningandfatal = criticalaliases._log().User experience
Before this change, calling
logger.log(),logger.warn(), orlogger.fatal()bypassed buffer routing and flush-on-error logic. Now they respectbuffer_at_verbosityand flush buffered logs upon error or critical log levels.Verification
tests/functional/logger/required_dependencies/test_powertools_logger_buffer.py:test_logger_log_method_buffers_and_flushes_on_errortest_logger_warn_and_fatal_aliasesruff check.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.