Skip to content

feat: logger-Remove custom keys interface #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/core/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

Logger log = LogManager.getLogger();

@Logging(logEvent = true)
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
...
LoggingUtils.appendKey("test", "willBeLogged");
...
Map<String, String> 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
Expand Down
2 changes: 1 addition & 1 deletion docs/utilities/sqs_large_message_handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -51,6 +53,25 @@ public static void appendKeys(Map<String, String> 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)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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();
}
}