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: support extra parameter in Logger messages #257

Merged

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Jan 12, 2021

Issue #, if available: #253

Description of changes:

This PR adds support for extra parameter when logging messages across all levels. Any key/value added using extra parameter will be at the root level of the structured log.

Before this PR, all fields added in extra parameter are discarded when structuring logs.

UX

Add request_id field while logging a message

logger = Logger(service="payment")

fields = { "request_id": "1123" }
logger.info("Hello", extra=fields)
{
    "level": "INFO",
    "location": "<module>:5",
    "message": "Hello",
    "timestamp": "2021-01-12 14:08:12,357",
    "service": "payment",
    "sampling_rate": 0.0,
    "request_id": "1123"
}

FAQ

Q: What's the difference between structure_log and extra after this PR?

Keys added with structure_log will persist across multiple log messages while keys added via extra will only be available in a given log message operation.

Example - Persisting payment_id not request_id

from aws_lambda_powertools import Logger

logger = Logger(service="payment")
logger.structure_logs(append=True, payment_id="123456789")

try:
    booking_id = book_flight()
    logger.info("Flight booked successfully", extra={ "booking_id": booking_id})
except BookingReservationError:
    ...

logger.info("goodbye")

Note that payment_id remains in both log messages while booking_id is only available in the first message.

{
    "level": "INFO",
    "location": "<module>:5",
    "message": "Flight booked successfully",
    "timestamp": "2021-01-12 14:09:10,859",
    "service": "payment",
    "sampling_rate": 0.0,
    "payment_id": "123456789",
    "booking_id": "75edbad0-0857-4fc9-b547-6180e2f7959b"
},
{
    "level": "INFO",
    "location": "<module>:6",
    "message": "goodbye",
    "timestamp": "2021-01-12 14:09:10,860",
    "service": "payment",
    "sampling_rate": 0.0,
    "payment_id": "123456789"
}

Checklist

Breaking change checklist

RFC issue #:

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

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

@heitorlessa
Copy link
Contributor Author

customer approvals: #253 (comment)

@heitorlessa heitorlessa merged commit 1131541 into aws-powertools:develop Jan 12, 2021
@heitorlessa heitorlessa deleted the improv/logger-extra-kwargs branch January 12, 2021 16:54
heitorlessa referenced this pull request in heitorlessa/aws-lambda-powertools-python Jan 17, 2021
* develop:
  chore: move env names to constant file (#264)
  docs: fix import (#267)
  feat: Add AppConfig parameter provider (#236)
  chore: update stale bot
  improv: override Tracer auto-capture response/exception via env vars (#259)
  docs: add info about extras layer (#260)
  feat: support extra parameter in Logger messages (#257)
  chore: general simplifications and cleanup (#255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant