You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/logger.md
+164-3
Original file line number
Diff line number
Diff line change
@@ -547,6 +547,167 @@ We prioritise log level settings in this order:
547
547
548
548
In the event you have set a log level in Powertools to a level that is lower than the ACL setting, we will output a warning log message informing you that your messages will be discarded by Lambda.
549
549
550
+
### Buffering logs
551
+
552
+
Log buffering enables you to buffer logs for a specific request or invocation. Enable log buffering by passing `logBufferOptions` when initializing a Logger instance. You can buffer logs at the `WARNING`, `INFO`, `DEBUG`, or `TRACE` level, and flush them automatically on error or manually as needed.
553
+
554
+
!!! tip "This is useful when you want to reduce the number of log messages emitted while still having detailed logs when needed, such as when troubleshooting issues."
When configuring the buffer, you can set the following options to fine-tune how logs are captured, stored, and emitted. You can configure the following options in the `logBufferOptions` constructor parameter:
1. Disabling `flushOnErrorLog` will not flush the buffer when logging an error. This is useful when you want to control when the buffer is flushed by calling the `logger.flushBuffer()` method.
589
+
590
+
#### Flushing on errors
591
+
592
+
When using the `logger.injectLambdaContext()` class method decorator or the `injectLambdaContext()` middleware, you can configure the logger to automatically flush the buffer when an error occurs. This is done by setting the `flushBufferOnUncaughtError` option to `true` in the decorator or middleware options.
Lambda->>Logger: Initialize with DEBUG level buffering
619
+
Logger-->>Lambda: Logger buffer ready
620
+
Lambda->>Logger: logger.debug("First debug log")
621
+
Logger-->>Logger: Buffer first debug log
622
+
Lambda->>Logger: logger.info("Info log")
623
+
Logger->>CloudWatch: Directly log info message
624
+
Lambda->>Logger: logger.debug("Second debug log")
625
+
Logger-->>Logger: Buffer second debug log
626
+
Lambda->>Logger: logger.flush_buffer()
627
+
Logger->>CloudWatch: Emit buffered logs to stdout
628
+
Lambda->>Client: Return execution result
629
+
```
630
+
<i>Flushing buffer manually</i>
631
+
</center>
632
+
633
+
##### Flushing when logging an error
634
+
635
+
<center>
636
+
```mermaid
637
+
sequenceDiagram
638
+
participant Client
639
+
participant Lambda
640
+
participant Logger
641
+
participant CloudWatch
642
+
Client->>Lambda: Invoke Lambda
643
+
Lambda->>Logger: Initialize with DEBUG level buffering
644
+
Logger-->>Lambda: Logger buffer ready
645
+
Lambda->>Logger: logger.debug("First log")
646
+
Logger-->>Logger: Buffer first debug log
647
+
Lambda->>Logger: logger.debug("Second log")
648
+
Logger-->>Logger: Buffer second debug log
649
+
Lambda->>Logger: logger.debug("Third log")
650
+
Logger-->>Logger: Buffer third debug log
651
+
Lambda->>Lambda: Exception occurs
652
+
Lambda->>Logger: logger.error("Error details")
653
+
Logger->>CloudWatch: Emit buffered debug logs
654
+
Logger->>CloudWatch: Emit error log
655
+
Lambda->>Client: Raise exception
656
+
```
657
+
<i>Flushing buffer when an error happens</i>
658
+
</center>
659
+
660
+
##### Flushing on error
661
+
662
+
This works only when using the `logger.injectLambdaContext()` class method decorator or the `injectLambdaContext()` middleware. You can configure the logger to automatically flush the buffer when an error occurs by setting the `flushBufferOnUncaughtError` option to `true` in the decorator or middleware options.
<i>Flushing buffer when an uncaught exception happens</i>
683
+
</center>
684
+
685
+
#### Buffering FAQs
686
+
687
+
1.**Does the buffer persist across Lambda invocations?**
688
+
No, each Lambda invocation has its own buffer. The buffer is initialized when the Lambda function is invoked and is cleared after the function execution completes or when flushed manually.
689
+
690
+
2.**Are my logs buffered during cold starts?**
691
+
No, we never buffer logs during cold starts. This is because we want to ensure that logs emitted during this phase are always available for debugging and monitoring purposes. The buffer is only used during the execution of the Lambda function.
692
+
693
+
3.**How can I prevent log buffering from consuming excessive memory?**
694
+
You can limit the size of the buffer by setting the `maxBytes` option in the `logBufferOptions` constructor parameter. This will ensure that the buffer does not grow indefinitely and consume excessive memory.
695
+
696
+
4.**What happens if the log buffer reaches its maximum size?**
697
+
Older logs are removed from the buffer to make room for new logs. This means that if the buffer is full, you may lose some logs if they are not flushed before the buffer reaches its maximum size. When this happens, we emit a warning when flushing the buffer to indicate that some logs have been dropped.
698
+
699
+
5.**What timestamp is used when I flush the logs?**
700
+
The timestamp preserves the original time when the log record was created. If you create a log record at 11:00:10 and flush it at 11:00:25, the log line will retain its original timestamp of 11:00:10.
701
+
702
+
6.**What happens if I try to add a log line that is bigger than max buffer size?**
703
+
The log will be emitted directly to standard output and not buffered. When this happens, we emit a warning to indicate that the log line was too big to be buffered.
704
+
705
+
7.**What happens if Lambda times out without flushing the buffer?**
706
+
Logs that are still in the buffer will be lost. If you are using the log buffer to log asynchronously, you should ensure that the buffer is flushed before the Lambda function times out. You can do this by calling the `logger.flushBuffer()` method at the end of your Lambda function.
707
+
708
+
8.**Do child loggers inherit the buffer?**
709
+
No, child loggers do not inherit the buffer from their parent logger but only the buffer configuration. This means that if you create a child logger, it will have its own buffer and will not share the buffer with the parent logger.
710
+
550
711
### Reordering log keys position
551
712
552
713
You can change the order of [standard Logger keys](#standard-structured-keys) or any keys that will be appended later at runtime via the `logRecordOrder` parameter.
@@ -566,7 +727,7 @@ You can change the order of [standard Logger keys](#standard-structured-keys) or
### Using multiple Logger instances across your code
750
+
### Creating child loggers
590
751
591
752
The `createChild` method allows you to create a child instance of the Logger, which inherits all of the attributes from its parent. You have the option to override any of the settings and attributes from the parent logger, including [its settings](#utility-settings), any [extra keys](#appending-additional-keys), and [the log formatter](#custom-log-formatter).
592
753
@@ -803,7 +964,7 @@ When working with custom log formatters, you take full control over the structur
803
964
804
965
Note that when implementing this method, you should avoid mutating the `attributes` and `additionalLogAttributes` objects directly. Instead, create a new object with the desired structure and return it. If mutation is necessary, you can create a [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone) of the object to avoid side effects.
805
966
806
-
### Bring your own JSON serializer
967
+
### Extend JSON replacer function
807
968
808
969
You can extend the default JSON serializer by passing a custom serializer function to the `Logger` constructor, using the `jsonReplacerFn` option. This is useful when you want to customize the serialization of specific values.
Copy file name to clipboardExpand all lines: docs/upgrade.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -213,7 +213,7 @@ Logger `sampleRateValue` **continues** to determine the percentage of concurrent
213
213
214
214
### Custom log formatter
215
215
216
-
!!! note "Disregard if you are not customizing log output with a [custom log formatter](./core/logger.md#custom-log-formatter-bring-your-own-formatter)."
216
+
!!! note "Disregard if you are not customizing log output with a [custom log formatter](./core/logger.md#custom-log-formatter)."
217
217
218
218
In v1, `Logger` exposed the [standard](./core/logger.md#standard-structured-keys) as a single argument, _e.g., `formatAttributes(attributes: UnformattedAttributes)`_. It expected a plain object with keys and values you wanted in the final log output.
0 commit comments