From 7bb6c9aa1592a6f5919c062728059edb3820912c Mon Sep 17 00:00:00 2001 From: Pankaj Agrawal Date: Fri, 14 May 2021 06:54:27 +0200 Subject: [PATCH 1/2] feat: logger-Remove custom keys interface --- .../powertools/logging/LoggingUtils.java | 21 ++++++++++++ .../powertools/logging/LoggingUtilsTest.java | 33 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java b/powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java index 839bb1d6a..418ae584b 100644 --- a/powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java +++ b/powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.logging.log4j.ThreadContext; +import static java.util.Arrays.asList; + /** * A class of helper functions to add additional functionality to Logging. * @@ -51,6 +53,25 @@ public static void appendKeys(Map customKeys) { ThreadContext.putAll(customKeys); } + /** + * Remove an additional key from log entry. + * + * @param customKey The name of the key to be logged + */ + public static void removeKey(String customKey) { + ThreadContext.remove(customKey); + } + + + /** + * Removes additional keys from log entry. + * + * @param keys Map of custom keys values to be appended to logs + */ + public static void removeKeys(String... keys) { + ThreadContext.removeAll(asList(keys)); + } + /** * Sets the instance of ObjectMapper object which is used for serialising event when * {@code @Logging(logEvent = true)}. diff --git a/powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/LoggingUtilsTest.java b/powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/LoggingUtilsTest.java index 25e0303c7..91fea4c7a 100644 --- a/powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/LoggingUtilsTest.java +++ b/powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/LoggingUtilsTest.java @@ -52,4 +52,37 @@ void shouldSetCustomKeyAsMapOnThreadContext() { .containsEntry("test", "value") .containsEntry("test1", "value1"); } + + @Test + void shouldRemoveCustomKeyOnThreadContext() { + LoggingUtils.appendKey("test", "value"); + + assertThat(ThreadContext.getImmutableContext()) + .hasSize(1) + .containsEntry("test", "value"); + + LoggingUtils.removeKey("test"); + + assertThat(ThreadContext.getImmutableContext()) + .isEmpty(); + } + + @Test + void shouldRemoveCustomKeysOnThreadContext() { + Map customKeys = new HashMap<>(); + customKeys.put("test", "value"); + customKeys.put("test1", "value1"); + + LoggingUtils.appendKeys(customKeys); + + assertThat(ThreadContext.getImmutableContext()) + .hasSize(2) + .containsEntry("test", "value") + .containsEntry("test1", "value1"); + + LoggingUtils.removeKeys("test", "test1"); + + assertThat(ThreadContext.getImmutableContext()) + .isEmpty(); + } } \ No newline at end of file From a89dd1b4995c4a3ade07777347e935927a4b0c9e Mon Sep 17 00:00:00 2001 From: Pankaj Agrawal Date: Fri, 14 May 2021 07:41:39 +0200 Subject: [PATCH 2/2] docs: logger-Remove custom keys interface --- docs/core/logging.md | 34 ++++++++++++++++++++ docs/utilities/sqs_large_message_handling.md | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/core/logging.md b/docs/core/logging.md index 36cc26c52..37aa1e0a9 100644 --- a/docs/core/logging.md +++ b/docs/core/logging.md @@ -154,6 +154,40 @@ You can append your own keys to your existing logs via `appendKey`. } ``` + +### Removing additional keys + +You can remove any additional key from entry using `LoggingUtils.removeKeys()`. + +=== "App.java" + + ```java hl_lines="19 20" + /** + * Handler for requests to Lambda function. + */ + public class App implements RequestHandler { + + Logger log = LogManager.getLogger(); + + @Logging(logEvent = true) + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { + ... + LoggingUtils.appendKey("test", "willBeLogged"); + ... + Map customKeys = new HashMap<>(); + customKeys.put("test1", "value"); + customKeys.put("test2", "value1"); + + LoggingUtils.appendKeys(customKeys); + ... + LoggingUtils.removeKey("test"); + LoggingUtils.removeKeys("test1", "test2"); + ... + } + } + ``` + + ## Override default object mapper You can optionally choose to override default object mapper which is used to serialize lambda function events. You might diff --git a/docs/utilities/sqs_large_message_handling.md b/docs/utilities/sqs_large_message_handling.md index 1b84dc8c3..2f9b4af5b 100644 --- a/docs/utilities/sqs_large_message_handling.md +++ b/docs/utilities/sqs_large_message_handling.md @@ -34,7 +34,7 @@ This utility is compatible with versions *[1.1.0+](https://github.com/awslabs/am To install this utility, add the following dependency to your project. !!! note "Using Java 9 or later?" -If you are working with lambda function on runtime **Java 9 or later**, please refer **[issue](https://github.com/awslabs/aws-lambda-powertools-java/issues/50)** for a workaround. + If you are working with lambda function on runtime **Java 9 or later**, please refer **[issue](https://github.com/awslabs/aws-lambda-powertools-java/issues/50)** for a workaround. === "Maven" ```xml hl_lines="3 4 5 6 7 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36"