Skip to content

fix(logger): add setLevel function to set level programmatically #2320

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

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #2303

Summary

Changes

When changing the logging level of a Logger via code, the logging level must be changed. With the current implementation, this was not happening because it was modifying the superclass and not the Logger instance. This PR aims to fix that.

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.

@leandrodamascena leandrodamascena requested a review from a team as a code owner May 24, 2023 08:36
@leandrodamascena leandrodamascena requested review from heitorlessa and removed request for a team May 24, 2023 08:36
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 24, 2023
@leandrodamascena leandrodamascena linked an issue May 24, 2023 that may be closed by this pull request
@github-actions github-actions bot added the bug Something isn't working label May 24, 2023
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

one tiny change and LGTM. Also a reminder for us that this is a workaround until we have a way to fix proxy object (__getattribute__ and __getattr__) while keeping mypy detecting non-existent attrs.

@codecov-commenter
Copy link

codecov-commenter commented May 24, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (653db45) 97.46% compared to head (2d4e6f4) 97.47%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2320   +/-   ##
========================================
  Coverage    97.46%   97.47%           
========================================
  Files          149      150    +1     
  Lines         6901     6925   +24     
  Branches       509      509           
========================================
+ Hits          6726     6750   +24     
  Misses         137      137           
  Partials        38       38           
Impacted Files Coverage Δ
aws_lambda_powertools/logging/logger.py 99.42% <100.00%> (+0.01%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@leandrodamascena leandrodamascena merged commit 55738b9 into aws-powertools:develop May 24, 2023
@leandrodamascena leandrodamascena deleted the logger/setlevel branch May 24, 2023 09:27
@heitorlessa
Copy link
Contributor

heitorlessa commented May 24, 2023

NOTE for future when we do this properly...

  • Remove subclassing from logging.Logger) and subclass from object instead - already commented on why
  • Override __getattribute__ to proxy to self._logger -- had to remove it before, and add __getattr__ under TYPE_CHECKING due to Mypy not detecting non-existent attributes

Then we can remove these ugly workarounds, including things like .log_level and anything else that goes straight to the underlying logger

    def __getattribute__(self, item):
        try:
            # Return actual item from our class first (e.g., append_keys, etc.)
            return object.__getattribute__(self, item)
        except AttributeError:
            # Return actual item from the underlying logger (e.g., setLevel, addHandler, etc.)
            return self._logger.__getattribute__(item)

heitorlessa added a commit to heitorlessa/aws-lambda-powertools-python that referenced this pull request May 26, 2023
* develop:
  chore(deps-dev): bump aws-cdk from 2.80.0 to 2.81.0 (aws-powertools#2332)
  chore(deps-dev): bump coverage from 7.2.5 to 7.2.6 (aws-powertools#2326)
  chore(deps-dev): bump pytest-cov from 4.0.0 to 4.1.0 (aws-powertools#2327)
  chore(deps): bump actions/setup-python from 4.6.0 to 4.6.1 (aws-powertools#2325)
  fix(docs): use concrete secrets from settings (aws-powertools#2322)
  fix(logger): add setLevel function to set level programmatically (aws-powertools#2320)
  feat(parser): add support for parsing SQS events wrapped in Kinesis Firehose (aws-powertools#2294)
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: programmatically changing the log level fails
3 participants