Skip to content

fix(logger): fix exception on flush without buffer #6794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2025

Conversation

tonnico
Copy link
Contributor

@tonnico tonnico commented Jun 11, 2025

Issue number: #6793

Summary

Changes

Please provide a summary of what's being changed
earlier exit when no buffer config is set.

User experience

Please share what the user experience looks like before and after this change

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@tonnico tonnico requested a review from a team as a code owner June 11, 2025 09:16
@tonnico tonnico requested a review from leandrodamascena June 11, 2025 09:16
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jun 11, 2025
@boring-cyborg boring-cyborg bot added the logger label Jun 21, 2025
@leandrodamascena leandrodamascena linked an issue Jun 21, 2025 that may be closed by this pull request
@github-actions github-actions bot added the bug Something isn't working label Jun 21, 2025
Copy link

codecov bot commented Jun 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.23%. Comparing base (a5482c8) to head (60e308a).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6794      +/-   ##
===========================================
+ Coverage    96.21%   96.23%   +0.01%     
===========================================
  Files          273      273              
  Lines        12691    12691              
  Branches       946      946              
===========================================
+ Hits         12211    12213       +2     
+ Misses         375      374       -1     
+ Partials       105      104       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tonnico thanks for sending this PR. Your fork doesn't allow modification, so, can you please add this test to make sure we are testing this situation?

File: https://github.com/tonnico/powertools-lambda-python/blob/c2dd26f297cfcb8834cc0ecd96f256259a5acd9d/tests/functional/logger/required_dependencies/test_powertools_logger_buffer.py

def test_flush_buffer_without_buffer_config(stdout, service_name, lambda_context, monkeypatch):
    # Set initial trace ID for first Lambda invocation
    monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")

    # GIVEN A logger without buffer configuration
    logger = Logger(level="DEBUG", service=service_name, stream=stdout)

    @logger.inject_lambda_context(flush_buffer_on_uncaught_error=True)
    def handler(event, context):
        # Log messages that should be flushed when an exception occurs
        logger.debug("this log line will be flushed after error - 1")
        logger.debug("this log line will be flushed after error - 2")
        raise ValueError("Test error")

    # WHEN Invoking the handler and expecting a ValueError
    # AND flush_buffer_on_uncaught_error is True but there is no logger buffer configuration
    with pytest.raises(ValueError):
        handler({}, lambda_context)

    # THEN Verify that buffered log messages are flushed without any exception
    log = capture_multiple_logging_statements_output(stdout)
    assert len(log) == 2, "Expected two log messages to be flushed"

@leandrodamascena leandrodamascena changed the title fix: exception on flush without buffer fix(logger): fix exception on flush without buffer Jun 22, 2025
Co-authored-by: leandrodamascena <leandro.damascena@gmail.com>
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jun 23, 2025
@boring-cyborg boring-cyborg bot added the tests label Jun 23, 2025
@tonnico
Copy link
Contributor Author

tonnico commented Jun 23, 2025

Hey @tonnico thanks for sending this PR. Your fork doesn't allow modification, so, can you please add this test to make sure we are testing this situation?

Hello @leandrodamascena. Thank you for the test. However, I have a slightly different view of the behaviour.

When no buffer configuration is set and the flush_buffer_on_uncaught_error parameter is set to True, nothing should be flushed because there is no buffer. Therefore, when the logger is set to DEBUG and a message is sent with logger.debug, I expect these messages to be sent immediately and not buffered.

I changed some comments and values and hope this is ok for you.

Copy link

@leandrodamascena
Copy link
Contributor

Hey @tonnico thanks for sending this PR. Your fork doesn't allow modification, so, can you please add this test to make sure we are testing this situation?

Hello @leandrodamascena. Thank you for the test. However, I have a slightly different view of the behaviour.

When no buffer configuration is set and the flush_buffer_on_uncaught_error parameter is set to True, nothing should be flushed because there is no buffer. Therefore, when the logger is set to DEBUG and a message is sent with logger.debug, I expect these messages to be sent immediately and not buffered.

I changed some comments and values and hope this is ok for you.

Thanks for fixing that, I agree 100% with you! Just let the CI completes and I'll merge!!

@leandrodamascena leandrodamascena self-requested a review June 23, 2025 14:08
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVED!

@leandrodamascena leandrodamascena merged commit c3c652e into aws-powertools:develop Jun 23, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working logger size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Missing _buffer_config check before in flush_buffer
2 participants