2222 LOGGER_ATTRIBUTE_HANDLER ,
2323 LOGGER_ATTRIBUTE_POWERTOOLS_HANDLER ,
2424 LOGGER_ATTRIBUTE_PRECONFIGURED ,
25- LOGGER_BUFFER_FIRST_INVOKE ,
2625)
2726from aws_lambda_powertools .logging .exceptions import (
2827 InvalidBufferItem ,
@@ -528,7 +527,7 @@ def _add_log_record_to_buffer(
528527 exc_info : logging ._ExcInfoType = None ,
529528 stack_info : bool = False ,
530529 extra : Mapping [str , object ] | None = None ,
531- ):
530+ ) -> None :
532531 """
533532 Add log record to buffer with intelligent tracer ID handling.
534533
@@ -558,33 +557,23 @@ def _add_log_record_to_buffer(
558557 between different tracer contexts.
559558 """
560559 # Determine tracer ID, defaulting to first invoke marker
561- tracer_id = get_tracer_id () or LOGGER_BUFFER_FIRST_INVOKE
560+ tracer_id = get_tracer_id ()
562561
563562 try :
564- # Create log record for buffering
565- log_record : dict [str , Any ] = _create_buffer_record (level = level , msg = msg , args = args , extra = extra )
566-
567- # Migrate log records from first invoke to current tracer context
568- if tracer_id != LOGGER_BUFFER_FIRST_INVOKE and self ._buffer_cache .get (LOGGER_BUFFER_FIRST_INVOKE ):
569- # Retrieve first invoke log records
570- first_invoke_items = self ._buffer_cache .get (LOGGER_BUFFER_FIRST_INVOKE )
571-
572- # Transfer log records to current tracer context
573- for item in first_invoke_items :
574- self ._buffer_cache .add (tracer_id , item )
575-
576- # Clear first invoke buffer
577- self ._buffer_cache .clear (LOGGER_BUFFER_FIRST_INVOKE )
578-
579- # Add current log record to buffer
580- self ._buffer_cache .add (tracer_id , log_record )
563+ if tracer_id :
564+ log_record : dict [str , Any ] = _create_buffer_record (level = level , msg = msg , args = args , extra = extra )
565+ self ._buffer_cache .add (tracer_id , log_record )
581566 except InvalidBufferItem as exc :
582- # Wrap and re-raise buffer addition error
583- raise InvalidBufferItem ("Cannot add item to the buffer" ) from exc
567+ # Wrap and re-raise buffer addition error as warning
568+ warnings .warn (
569+ message = f"Cannot add item to the buffer: { str (exc )} " ,
570+ category = PowertoolsUserWarning ,
571+ stacklevel = 3 ,
572+ )
584573
585- def flush_buffer (self ):
574+ def flush_buffer (self ) -> None :
586575 """
587- Flush all buffered log records associated with current trace ID .
576+ Flush all buffered log records associated with current execution .
588577
589578 Notes
590579 -----
@@ -599,10 +588,22 @@ def flush_buffer(self):
599588 will be propagated to caller
600589 """
601590 tracer_id = get_tracer_id ()
602- for log_line in self ._buffer_cache .get (tracer_id ):
591+
592+ # Flushing log without a tracer id? Return
593+ if not tracer_id :
594+ return
595+
596+ # is buffer empty? return
597+ buffer = self ._buffer_cache .get (tracer_id )
598+ if not buffer :
599+ return
600+
601+ # Process log records
602+ for log_line in buffer :
603603 self ._create_and_flush_log_record (log_line )
604604
605- if self ._buffer_cache .has_evicted (tracer_id ):
605+ # Has items evicted?
606+ if self ._buffer_cache .has_items_evicted (tracer_id ):
606607 warnings .warn (
607608 message = "Some logs are not displayed because they were evicted from the buffer. "
608609 "Increase buffer size to store more logs in the buffer" ,
0 commit comments