Skip to content

chore(logger): clear prev request buffers in manual mode #3742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,16 +767,19 @@ sequenceDiagram
4. **What happens if the log buffer reaches its maximum size?**
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.

5. **What timestamp is used when I flush the logs?**
5. **How is the log size of a log line calculated?**
The log size is calculated based on the size of the stringified log line in bytes. This includes the size of the log message, the size of any additional keys, and the size of the timestamp.

6. **What timestamp is used when I flush the logs?**
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.

6. **What happens if I try to add a log line that is bigger than max buffer size?**
7. **What happens if I try to add a log line that is bigger than max buffer size?**
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.

7. **What happens if Lambda times out without flushing the buffer?**
8. **What happens if Lambda times out without flushing the buffer?**
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.

8. **Do child loggers inherit the buffer?**
9. **Do child loggers inherit the buffer?**
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.

### Reordering log keys position
Expand Down
8 changes: 7 additions & 1 deletion packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,12 @@ class Logger extends Utility implements LoggerInterface {
logLevel: number
): void {
log.prepareForPrint();
// This is the first time we see this traceId, so we need to clear the buffer
// from previous requests. This is ok because in AWS Lambda, the same sandbox
// environment can only ever be used by one request at a time.
if (this.#buffer?.has(xrayTraceId) === false) {
this.#buffer?.clear();
}
this.#buffer?.setItem(
xrayTraceId,
JSON.stringify(
Expand Down Expand Up @@ -1424,7 +1430,6 @@ class Logger extends Utility implements LoggerInterface {

/**
* Empties the buffer for the current request
*
*/
public clearBuffer(): void {
const traceId = this.envVarsService.getXrayTraceId();
Expand Down Expand Up @@ -1453,6 +1458,7 @@ class Logger extends Utility implements LoggerInterface {

/**
* Set the correlation ID for the log item.
*
* This method can be used to set the correlation ID for the log item or to search for the correlation ID in the event.
*
* @example
Expand Down