Skip to content

docs: ability to override object mapper used for logging event #303

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 1 commit into from
Mar 1, 2021
Merged
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
37 changes: 37 additions & 0 deletions docs/content/core/logging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
```

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

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

## Override default object mapper

You can optionally choose to override default object mapper which is used to serialize lambda function events. You might
want to supply custom object mapper in order to control how serialisation is done, for example, when you want to log only
specific fields from received event due to security.

```java:title=App.java
package helloworld;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.logging.LoggingUtils;
import software.amazon.lambda.logging.Logging;
...

/**
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

Logger log = LogManager.getLogger();

// highlight-start
static {
ObjectMapper objectMapper = new ObjectMapper();
LoggingUtils.defaultObjectMapper(objectMapper);
}
// highlight-end

@Logging(logEvent = true)
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
...
}
}
```

## Sampling debug logs

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