Skip to content

Commit 7f1aac5

Browse files
pankajagrawal16Pankaj Agrawal
and
Pankaj Agrawal
authored
docs: ability to override object mapper used for logging event (#303)
Co-authored-by: Pankaj Agrawal <[email protected]>
1 parent a1cfe72 commit 7f1aac5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/content/core/logging.mdx

+37
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
100100
```
101101

102102
You can also explicitly log any incoming event using `logEvent` param.
103+
Refer [Override default object mapper](#override-default-object-mapper) to customise what is logged.
103104

104105
<Note type="warning">
105106
This is disabled by default to prevent sensitive info being logged.
@@ -165,6 +166,42 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
165166
}
166167
```
167168

169+
## Override default object mapper
170+
171+
You can optionally choose to override default object mapper which is used to serialize lambda function events. You might
172+
want to supply custom object mapper in order to control how serialisation is done, for example, when you want to log only
173+
specific fields from received event due to security.
174+
175+
```java:title=App.java
176+
package helloworld;
177+
178+
import org.apache.logging.log4j.LogManager;
179+
import org.apache.logging.log4j.Logger;
180+
import software.amazon.lambda.logging.LoggingUtils;
181+
import software.amazon.lambda.logging.Logging;
182+
...
183+
184+
/**
185+
* Handler for requests to Lambda function.
186+
*/
187+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
188+
189+
Logger log = LogManager.getLogger();
190+
191+
// highlight-start
192+
static {
193+
ObjectMapper objectMapper = new ObjectMapper();
194+
LoggingUtils.defaultObjectMapper(objectMapper);
195+
}
196+
// highlight-end
197+
198+
@Logging(logEvent = true)
199+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
200+
...
201+
}
202+
}
203+
```
204+
168205
## Sampling debug logs
169206

170207
You can dynamically set a percentage of your logs to **DEBUG** level via env var `POWERTOOLS_LOGGER_SAMPLE_RATE` or

0 commit comments

Comments
 (0)