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
+166-4
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@ Logger provides an opinionated logger with output structured as JSON.
9
9
10
10
* Capturing key fields from the Lambda context, cold starts, and structure logging output as JSON.
11
11
* Logging Lambda invocation events when instructed (disabled by default).
12
-
* Printing all the logs only for a percentage of invocations via log sampling (disabled by default).
12
+
* Switch log level to `DEBUG` for a percentage of invocations (sampling).
13
+
* Buffering logs for a specific request or invocation, and flushing them automatically on error or manually as needed.
13
14
* Appending additional keys to structured logs at any point in time.
14
15
* Providing a custom log formatter (Bring Your Own Formatter) to output logs in a structure compatible with your organization’s Logging RFC.
15
16
@@ -547,6 +548,167 @@ We prioritise log level settings in this order:
547
548
548
549
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
550
551
+
### Buffering logs
552
+
553
+
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.
554
+
555
+
!!! 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.
590
+
591
+
#### Flushing on errors
592
+
593
+
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
620
+
Logger-->>Lambda: Logger buffer ready
621
+
Lambda->>Logger: logger.debug("First debug log")
622
+
Logger-->>Logger: Buffer first debug log
623
+
Lambda->>Logger: logger.info("Info log")
624
+
Logger->>CloudWatch: Directly log info message
625
+
Lambda->>Logger: logger.debug("Second debug log")
626
+
Logger-->>Logger: Buffer second debug log
627
+
Lambda->>Logger: logger.flush_buffer()
628
+
Logger->>CloudWatch: Emit buffered logs to stdout
629
+
Lambda->>Client: Return execution result
630
+
```
631
+
<i>Flushing buffer manually</i>
632
+
</center>
633
+
634
+
##### Flushing when logging an error
635
+
636
+
<center>
637
+
```mermaid
638
+
sequenceDiagram
639
+
participant Client
640
+
participant Lambda
641
+
participant Logger
642
+
participant CloudWatch
643
+
Client->>Lambda: Invoke Lambda
644
+
Lambda->>Logger: Initialize with DEBUG level buffering
645
+
Logger-->>Lambda: Logger buffer ready
646
+
Lambda->>Logger: logger.debug("First log")
647
+
Logger-->>Logger: Buffer first debug log
648
+
Lambda->>Logger: logger.debug("Second log")
649
+
Logger-->>Logger: Buffer second debug log
650
+
Lambda->>Logger: logger.debug("Third log")
651
+
Logger-->>Logger: Buffer third debug log
652
+
Lambda->>Lambda: Exception occurs
653
+
Lambda->>Logger: logger.error("Error details")
654
+
Logger->>CloudWatch: Emit buffered debug logs
655
+
Logger->>CloudWatch: Emit error log
656
+
Lambda->>Client: Raise exception
657
+
```
658
+
<i>Flushing buffer when an error happens</i>
659
+
</center>
660
+
661
+
##### Flushing on error
662
+
663
+
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>
684
+
</center>
685
+
686
+
#### Buffering FAQs
687
+
688
+
1.**Does the buffer persist across Lambda invocations?**
689
+
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.
690
+
691
+
2.**Are my logs buffered during cold starts?**
692
+
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.
693
+
694
+
3.**How can I prevent log buffering from consuming excessive memory?**
695
+
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.
696
+
697
+
4.**What happens if the log buffer reaches its maximum size?**
698
+
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.
699
+
700
+
5.**What timestamp is used when I flush the logs?**
701
+
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.
702
+
703
+
6.**What happens if I try to add a log line that is bigger than max buffer size?**
704
+
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.
705
+
706
+
7.**What happens if Lambda times out without flushing the buffer?**
707
+
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.
708
+
709
+
8.**Do child loggers inherit the buffer?**
710
+
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.
711
+
550
712
### Reordering log keys position
551
713
552
714
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 +728,7 @@ You can change the order of [standard Logger keys](#standard-structured-keys) or
### Using multiple Logger instances across your code
751
+
### Creating child loggers
590
752
591
753
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
754
@@ -803,7 +965,7 @@ When working with custom log formatters, you take full control over the structur
803
965
804
966
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
967
806
-
### Bring your own JSON serializer
968
+
### Extend JSON replacer function
807
969
808
970
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