Skip to content

Commit 70a57bf

Browse files
pankajagrawal16Pankaj Agrawal
and
Pankaj Agrawal
authored
feat: logger-Remove custom keys interface (#395)
* feat: logger-Remove custom keys interface * docs: logger-Remove custom keys interface Co-authored-by: Pankaj Agrawal <[email protected]>
1 parent 5ec85fd commit 70a57bf

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

docs/core/logging.md

+34
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,40 @@ You can append your own keys to your existing logs via `appendKey`.
154154
}
155155
```
156156

157+
158+
### Removing additional keys
159+
160+
You can remove any additional key from entry using `LoggingUtils.removeKeys()`.
161+
162+
=== "App.java"
163+
164+
```java hl_lines="19 20"
165+
/**
166+
* Handler for requests to Lambda function.
167+
*/
168+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
169+
170+
Logger log = LogManager.getLogger();
171+
172+
@Logging(logEvent = true)
173+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
174+
...
175+
LoggingUtils.appendKey("test", "willBeLogged");
176+
...
177+
Map<String, String> customKeys = new HashMap<>();
178+
customKeys.put("test1", "value");
179+
customKeys.put("test2", "value1");
180+
181+
LoggingUtils.appendKeys(customKeys);
182+
...
183+
LoggingUtils.removeKey("test");
184+
LoggingUtils.removeKeys("test1", "test2");
185+
...
186+
}
187+
}
188+
```
189+
190+
157191
## Override default object mapper
158192

159193
You can optionally choose to override default object mapper which is used to serialize lambda function events. You might

docs/utilities/sqs_large_message_handling.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This utility is compatible with versions *[1.1.0+](https://github.com/awslabs/am
3434
To install this utility, add the following dependency to your project.
3535

3636
!!! note "Using Java 9 or later?"
37-
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.
37+
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.
3838

3939
=== "Maven"
4040
```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"

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/LoggingUtils.java

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import org.apache.logging.log4j.ThreadContext;
2020

21+
import static java.util.Arrays.asList;
22+
2123
/**
2224
* A class of helper functions to add additional functionality to Logging.
2325
*
@@ -51,6 +53,25 @@ public static void appendKeys(Map<String, String> customKeys) {
5153
ThreadContext.putAll(customKeys);
5254
}
5355

56+
/**
57+
* Remove an additional key from log entry.
58+
*
59+
* @param customKey The name of the key to be logged
60+
*/
61+
public static void removeKey(String customKey) {
62+
ThreadContext.remove(customKey);
63+
}
64+
65+
66+
/**
67+
* Removes additional keys from log entry.
68+
*
69+
* @param keys Map of custom keys values to be appended to logs
70+
*/
71+
public static void removeKeys(String... keys) {
72+
ThreadContext.removeAll(asList(keys));
73+
}
74+
5475
/**
5576
* Sets the instance of ObjectMapper object which is used for serialising event when
5677
* {@code @Logging(logEvent = true)}.

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/LoggingUtilsTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,37 @@ void shouldSetCustomKeyAsMapOnThreadContext() {
5252
.containsEntry("test", "value")
5353
.containsEntry("test1", "value1");
5454
}
55+
56+
@Test
57+
void shouldRemoveCustomKeyOnThreadContext() {
58+
LoggingUtils.appendKey("test", "value");
59+
60+
assertThat(ThreadContext.getImmutableContext())
61+
.hasSize(1)
62+
.containsEntry("test", "value");
63+
64+
LoggingUtils.removeKey("test");
65+
66+
assertThat(ThreadContext.getImmutableContext())
67+
.isEmpty();
68+
}
69+
70+
@Test
71+
void shouldRemoveCustomKeysOnThreadContext() {
72+
Map<String, String> customKeys = new HashMap<>();
73+
customKeys.put("test", "value");
74+
customKeys.put("test1", "value1");
75+
76+
LoggingUtils.appendKeys(customKeys);
77+
78+
assertThat(ThreadContext.getImmutableContext())
79+
.hasSize(2)
80+
.containsEntry("test", "value")
81+
.containsEntry("test1", "value1");
82+
83+
LoggingUtils.removeKeys("test", "test1");
84+
85+
assertThat(ThreadContext.getImmutableContext())
86+
.isEmpty();
87+
}
5588
}

0 commit comments

Comments
 (0)