22
22
LOGGER_ATTRIBUTE_HANDLER ,
23
23
LOGGER_ATTRIBUTE_POWERTOOLS_HANDLER ,
24
24
LOGGER_ATTRIBUTE_PRECONFIGURED ,
25
- LOGGER_BUFFER_FIRST_INVOKE ,
26
25
)
27
26
from aws_lambda_powertools .logging .exceptions import (
28
27
InvalidBufferItem ,
@@ -528,7 +527,7 @@ def _add_log_record_to_buffer(
528
527
exc_info : logging ._ExcInfoType = None ,
529
528
stack_info : bool = False ,
530
529
extra : Mapping [str , object ] | None = None ,
531
- ):
530
+ ) -> None :
532
531
"""
533
532
Add log record to buffer with intelligent tracer ID handling.
534
533
@@ -558,33 +557,23 @@ def _add_log_record_to_buffer(
558
557
between different tracer contexts.
559
558
"""
560
559
# 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 ()
562
561
563
562
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 )
581
566
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
+ )
584
573
585
- def flush_buffer (self ):
574
+ def flush_buffer (self ) -> None :
586
575
"""
587
- Flush all buffered log records associated with current trace ID .
576
+ Flush all buffered log records associated with current execution .
588
577
589
578
Notes
590
579
-----
@@ -599,10 +588,22 @@ def flush_buffer(self):
599
588
will be propagated to caller
600
589
"""
601
590
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 :
603
603
self ._create_and_flush_log_record (log_line )
604
604
605
- if self ._buffer_cache .has_evicted (tracer_id ):
605
+ # Has items evicted?
606
+ if self ._buffer_cache .has_items_evicted (tracer_id ):
606
607
warnings .warn (
607
608
message = "Some logs are not displayed because they were evicted from the buffer. "
608
609
"Increase buffer size to store more logs in the buffer" ,
0 commit comments