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
fix(logging): Prevent accidental overwriting of reserved keys via structured arguments
* Ignore logging of reserved keys when passed as StructuredArgument.
* Add error to reserved log keys.
* Update docs with a warning about logging of reserved keys when using structured arguments.
* Update docs with warning in MDC section as well.
* Update product name in documentation.
* Make StructuredArgument implementations package private. They should only be instantiated by factory StructuredArguments.
* Make StructuredArgument interface public and remove package private dependency in LambdaLoggingAspectTest.
* Move reserved key logic from logging implementation to StructuredArguments.
* Remove .keys() method again from StructuredArgument interface. It is no longer needed now.
* Update documentation warnings regarding logging of reserved keys.
Copy file name to clipboardExpand all lines: docs/core/logging.md
+9
Original file line number
Diff line number
Diff line change
@@ -354,6 +354,9 @@ Your logs will always include the following keys in your structured logging:
354
354
| **xray_trace_id** | String | "1-5759e988-bd862e3fe1be46a994272793" | X-Ray Trace ID when [Tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"} |
355
355
| **error** | Map | `{ "name": "InvalidAmountException", "message": "Amount must be superior to 0", "stack": "at..." }` | Eventual exception (e.g. when doing `logger.error("Error", new InvalidAmountException("Amount must be superior to 0"));`) |
356
356
357
+
???+ note
358
+
If you emit a log message with a key that matches one of the [standard structured keys](#standard-structured-keys) or one of the [additional structured keys](#additional-structured-keys), the Logger will log a warning message and ignore the key.
359
+
357
360
## Additional structured keys
358
361
359
362
### Logging Lambda context information
@@ -640,6 +643,9 @@ To append additional keys in your logs, you can use the `StructuredArguments` cl
640
643
}
641
644
```
642
645
646
+
???+ warning "Do not use reserved keys in `StructuredArguments`"
647
+
If the key name of your structured argument matches any of the [standard structured keys](#standard-structured-keys) or any of the [additional structured keys](#additional-structured-keys), the Logger will log a warning message and ignore the key. This is to protect you from accidentally overwriting reserved keys such as the log level or Lambda context information.
648
+
643
649
**Using MDC**
644
650
645
651
Mapped Diagnostic Context (MDC) is essentially a Key-Value store. It is supported by the [SLF4J API](https://www.slf4j.org/manual.html#mdc){target="_blank"},
@@ -650,6 +656,9 @@ Mapped Diagnostic Context (MDC) is essentially a Key-Value store. It is supporte
650
656
???+ warning "Custom keys stored in the MDC are persisted across warm invocations"
651
657
Always set additional keys as part of your handler method to ensure they have the latest value, or explicitly clear them with [`clearState=true`](#clearing-state).
652
658
659
+
???+ warning "Do not add reserved keys to MDC"
660
+
Avoid adding any of the keys listed in [standard structured keys](#standard-structured-keys) and [additional structured keys](#additional-structured-keys) to your MDC. This may cause unindented behavior and will overwrite the context set by the Logger. Unlike with `StructuredArguments`, the Logger will **not** ignore reserved keys set via MDC.
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-log4j/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolver.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolverArgumentsTest.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/internal/handler/PowertoolsArguments.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-logback/src/main/java/software/amazon/lambda/powertools/logging/logback/JsonUtils.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonEncoderTest.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/handler/PowertoolsArguments.java
0 commit comments