Skip to content

Logger: inject_lambda_context overrides keys appended before in custom middleware #85

Closed

Description

@heitorlessa

What were you trying to accomplish?

Have a custom middleware to inject additional keys to the Logger as well as using inject_lambda_context.

@lambda_handler_decorator(trace_execution=True)
def process_booking_handler(
    handler: Callable, event: Dict, context: Any, logger: Logger = None
) -> Callable:
    if logger is None:
        logger = Logger()

    handler = logger.inject_lambda_context(handler)

    logger.info("Injecting and annotating process booking state machine")
    process_booking_context = _build_process_booking_model(event)
    _logger_inject_process_booking_sfn(logger=logger, event=event)
    _tracer_annotate_process_booking_sfn(process_booking_context=process_booking_context)

    return handler(event, context)

Expected Behavior

Have additional key added to the log when structure_logs is used outside the handler e.g. shared file, before the handler

Current Behavior

Any additional key is removed once the handler is executed with inject_lambda_context

Possible Solution

inject_lambda_context is overwriting existing structured logs instead of appending

Steps to Reproduce (for bugs)

  1. Use structure_logs before inject_lambda_context is used

Code

    logger = Logger()
    logger.structure_logs(append=True, additional_key="test")

    @logger.inject_lambda_context
    def handler(event, context):
        logger.info("Hello") # will not contain additional_key

    handler({}, lambda_context)

Environment

  • Powertools version used: 1.0.0
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: Python 3.7
  • Debugging logs

How to enable debug mode**

# paste logs here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions