Skip to content

Commit 55cee27

Browse files
author
Pankaj Agrawal
committed
docs: ability to clear state of logger on each request for custom keys
1 parent 21fceba commit 55cee27

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

docs/core/logging.md

+64
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ for known event sources, where either a request ID or X-Ray Trace ID are present
224224

225225
## Appending additional keys
226226

227+
!!! info "Custom keys are persisted across warm invocations"
228+
Always set additional keys as part of your handler to ensure they have the latest value, or explicitly clear them with [`clearState=true`](#clearing-all-state).
229+
227230
You can append your own keys to your existing logs via `appendKey`.
228231

229232
=== "App.java"
@@ -286,6 +289,67 @@ You can remove any additional key from entry using `LoggingUtils.removeKeys()`.
286289
}
287290
```
288291

292+
### Clearing all state
293+
294+
Logger is commonly initialized in the global scope. Due to [Lambda Execution Context reuse](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html),
295+
this means that custom keys can be persisted across invocations. If you want all custom keys to be deleted, you can use
296+
`clearState=true` attribute on `@Logging` annotation.
297+
298+
299+
=== "App.java"
300+
301+
```java hl_lines="8 12"
302+
/**
303+
* Handler for requests to Lambda function.
304+
*/
305+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
306+
307+
Logger log = LogManager.getLogger();
308+
309+
@Logging(clearState = true)
310+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
311+
...
312+
if(input.getHeaders().get("someSpecialHeader")) {
313+
LoggingUtils.appendKey("specialKey", "value");
314+
}
315+
316+
log.info("Collecting payment");
317+
...
318+
}
319+
}
320+
```
321+
=== "#1 Request"
322+
323+
```json hl_lines="11"
324+
{
325+
"level": "INFO",
326+
"message": "Collecting payment",
327+
"timestamp": "2021-05-03 11:47:12,494+0200",
328+
"service": "payment",
329+
"coldStart": true,
330+
"functionName": "test",
331+
"functionMemorySize": 128,
332+
"functionArn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
333+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
334+
"specialKey": "value"
335+
}
336+
```
337+
338+
=== "#2 Request"
339+
340+
```json
341+
{
342+
"level": "INFO",
343+
"message": "Collecting payment",
344+
"timestamp": "2021-05-03 11:47:12,494+0200",
345+
"service": "payment",
346+
"coldStart": true,
347+
"functionName": "test",
348+
"functionMemorySize": 128,
349+
"functionArn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
350+
"lambda_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72"
351+
}
352+
```
289353

290354
## Override default object mapper
291355

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/handlers/PowerLogToolEnabledWithClearState.java

-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,4 @@ public Object handleRequest(Object input, Context context) {
3333
COUNT++;
3434
return null;
3535
}
36-
37-
@Logging
38-
public void anotherMethod() {
39-
System.out.println("test");
40-
}
4136
}

0 commit comments

Comments
 (0)