Skip to content
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

feat(logger): add context manager for logger keys #5883

Merged
merged 7 commits into from
Jan 21, 2025

Conversation

anafalcao
Copy link
Collaborator

@anafalcao anafalcao commented Jan 17, 2025

Issue number: #5864

Additional issues: #3719 #2062

Summary

This PR adds the append_context_keys method, and it allows temporary modification of a Logger instance's context without creating a new logger.

Changes

New method to Logger formatter - append_context_keys(). This PR also adds more tests and documentation for this method.

User experience

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(service="example_service")


def lambda_handler(event: dict, context: LambdaContext) -> str:
    with logger.append_context_keys(user_id="123", operation="process"):
        logger.info("Log with context")

    logger.info("Log without context")

    return "hello world"

image

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.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation logger tests labels Jan 17, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 17, 2025
Copy link
Contributor

No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.

@github-actions github-actions bot added do-not-merge need-issue PRs that are missing related issues labels Jan 17, 2025
@anafalcao anafalcao changed the title feature(logger): add keys as context manager to Logger feat(logger): add keys as context manager to Logger Jan 17, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Jan 17, 2025
Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (076a913) to head (d523ce8).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5883   +/-   ##
========================================
  Coverage    96.19%   96.20%           
========================================
  Files          232      232           
  Lines        10941    10956   +15     
  Branches      2023     2027    +4     
========================================
+ Hits         10525    10540   +15     
  Misses         327      327           
  Partials        89       89           

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

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 20, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 20, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 20, 2025
@leandrodamascena leandrodamascena changed the title feat(logger): add keys as context manager to Logger feat(logger): add context manager for logger keys Jan 20, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 20, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 20, 2025
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.

Hi @anafalcao! Thanks a lot for working on this, another super cool PR!

I left some feedback to improve the customer experience and make it even better.

aws_lambda_powertools/logging/formatter.py Show resolved Hide resolved
aws_lambda_powertools/logging/formatter.py Show resolved Hide resolved
aws_lambda_powertools/logging/logger.py Show resolved Hide resolved
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 20, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 21, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 21, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 21, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 21, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 21, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 21, 2025
@anafalcao anafalcao marked this pull request as ready for review January 21, 2025 13:54
@anafalcao anafalcao requested a review from a team as a code owner January 21, 2025 13:54
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 21, 2025
Copy link

sonarqubecloud bot commented Jan 21, 2025

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 21, 2025
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.

Another super super nice PR @anafalcao! Congratulations for having such a sense of ownership and innovating on behalf of our customer!

Parabéns 🎆

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jan 21, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Jan 21, 2025
@dreamorosi
Copy link
Contributor

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Jan 21, 2025

Quality Gate Passed Quality Gate passed

Issues 3 New issues 0 Accepted issues
Measures 0 Security Hotspots 0.0% Coverage on New Code 0.0% Duplication on New Code
See analysis details on SonarQube Cloud

Can we look at these?

This is literally SonarQube complaining about a new fake class for test with a very common practice: abstracting a class with pass in the methods so it not override the parent class's implementation.

I'll mark them a false positive.

@dreamorosi dreamorosi merged commit e96608e into aws-powertools:develop Jan 21, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality logger need-issue PRs that are missing related issues size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
3 participants