Skip to content

Commit d13f3c6

Browse files
dreamorosiam29d
andauthored
chore(logger): clear prev request buffers in manual mode (#3742)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent 51a3410 commit d13f3c6

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

docs/core/logger.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,19 @@ sequenceDiagram
767767
4. **What happens if the log buffer reaches its maximum size?**
768768
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.
769769

770-
5. **What timestamp is used when I flush the logs?**
770+
5. **How is the log size of a log line calculated?**
771+
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.
772+
773+
6. **What timestamp is used when I flush the logs?**
771774
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.
772775

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

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

779-
8. **Do child loggers inherit the buffer?**
782+
9. **Do child loggers inherit the buffer?**
780783
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.
781784

782785
### Reordering log keys position

packages/logger/src/Logger.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,12 @@ class Logger extends Utility implements LoggerInterface {
13741374
logLevel: number
13751375
): void {
13761376
log.prepareForPrint();
1377+
// This is the first time we see this traceId, so we need to clear the buffer
1378+
// from previous requests. This is ok because in AWS Lambda, the same sandbox
1379+
// environment can only ever be used by one request at a time.
1380+
if (this.#buffer?.has(xrayTraceId) === false) {
1381+
this.#buffer?.clear();
1382+
}
13771383
this.#buffer?.setItem(
13781384
xrayTraceId,
13791385
JSON.stringify(
@@ -1424,7 +1430,6 @@ class Logger extends Utility implements LoggerInterface {
14241430

14251431
/**
14261432
* Empties the buffer for the current request
1427-
*
14281433
*/
14291434
public clearBuffer(): void {
14301435
const traceId = this.envVarsService.getXrayTraceId();
@@ -1453,6 +1458,7 @@ class Logger extends Utility implements LoggerInterface {
14531458

14541459
/**
14551460
* Set the correlation ID for the log item.
1461+
*
14561462
* This method can be used to set the correlation ID for the log item or to search for the correlation ID in the event.
14571463
*
14581464
* @example

0 commit comments

Comments
 (0)