diff --git a/docs/core/logger.md b/docs/core/logger.md index 23b5d1ea9..6a72664bb 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -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 diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index be5ae0dba..f1217a1e6 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -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( @@ -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(); @@ -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