Skip to content
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

Logging configuration for specific declarative RestClient using named "client" properties aka quarkus.rest-client."client".logging.* #45520

Merged
merged 1 commit into from
Jan 14, 2025
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
6 changes: 6 additions & 0 deletions docs/src/main/asciidoc/rest-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1845,14 +1845,20 @@ As HTTP messages can have large bodies, we limit the amount of body characters l

NOTE: REST Client is logging the traffic with level DEBUG and does not alter logger properties. You may need to adjust your logger configuration to use this feature.

These configuration properties work globally for all clients injected by CDI.
If you want configure logging for a specific declarative client, you should do it by specifying named "client" properties, also known as `quarkus.rest-client."client".logging.*` properties.

An example logging configuration:

[source,properties]
----
quarkus.rest-client.logging.scope=request-response
quarkus.rest-client.logging.body-limit=50

quarkus.rest-client.extensions-api.scope=all

quarkus.log.category."org.jboss.resteasy.reactive.client.logging".level=DEBUG
quarkus.log.console.level=DEBUG
geoand marked this conversation as resolved.
Show resolved Hide resolved
----

[TIP]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ default Optional<String> uriReload() {
*/
@WithDefault("${microprofile.rest.client.disable.default.mapper:false}")
Boolean disableDefaultMapper();

/**
* Logging configuration.
*/
Optional<RestClientLoggingConfig> logging();
}

class RestClientKeysProvider implements Supplier<Iterable<String>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,25 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
clientBuilder.register(new MicroProfileRestClientResponseFilter(exceptionMappers));
clientBuilder.followRedirects(followRedirects != null ? followRedirects : restClients.followRedirects().orElse(false));

RestClientsConfig.RestClientLoggingConfig logging = restClients.logging();

LoggingScope effectiveLoggingScope = loggingScope; // if a scope was specified programmatically, it takes precedence
if (effectiveLoggingScope == null) {
effectiveLoggingScope = logging != null ? logging.scope().map(LoggingScope::forName).orElse(LoggingScope.NONE)
: LoggingScope.NONE;
RestClientsConfig.RestClientLoggingConfig configRootLogging = restClients.logging();

Integer defaultLoggingBodyLimit = 100;
LoggingScope effectiveLoggingScope = LoggingScope.NONE;
Integer effectiveLoggingBodyLimit = defaultLoggingBodyLimit;
if (getConfiguration().hasProperty(QuarkusRestClientProperties.LOGGING_SCOPE)) {
effectiveLoggingScope = (LoggingScope) getConfiguration().getProperty(QuarkusRestClientProperties.LOGGING_SCOPE);
} else if (loggingScope != null) { //scope, specified programmatically, takes precedence over global configuration
effectiveLoggingScope = loggingScope;
} else if (configRootLogging != null) {
effectiveLoggingScope = configRootLogging.scope().map(LoggingScope::forName).orElse(LoggingScope.NONE);
}

Integer effectiveLoggingBodyLimit = loggingBodyLimit; // if a limit was specified programmatically, it takes precedence
if (effectiveLoggingBodyLimit == null) {
effectiveLoggingBodyLimit = logging != null ? logging.bodyLimit() : 100;
if (getConfiguration().hasProperty(QuarkusRestClientProperties.LOGGING_BODY_LIMIT)) {
effectiveLoggingBodyLimit = (Integer) getConfiguration()
.getProperty(QuarkusRestClientProperties.LOGGING_BODY_LIMIT);
} else if (loggingBodyLimit != null) { //bodyLimit, specified programmatically, takes precedence over global configuration
effectiveLoggingBodyLimit = loggingBodyLimit;
} else if (configRootLogging != null) {
effectiveLoggingBodyLimit = configRootLogging.bodyLimit();
}
clientBuilder.loggingScope(effectiveLoggingScope);
clientBuilder.loggingBodySize(effectiveLoggingBodyLimit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.jboss.resteasy.reactive.client.api.LoggingScope;
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.resteasy.reactive.client.impl.multipart.PausableHttpPostRequestEncoder;

Expand Down Expand Up @@ -81,9 +82,19 @@ void configureBuilder(QuarkusRestClientBuilder builder) {
configureQueryParamStyle(builder);
configureProxy(builder);
configureShared(builder);
configureLogging(builder);
configureCustomProperties(builder);
}

private void configureLogging(QuarkusRestClientBuilder builder) {
if (restClientConfig.logging().isPresent()) {
RestClientsConfig.RestClientLoggingConfig loggingConfig = restClientConfig.logging().get();
builder.property(QuarkusRestClientProperties.LOGGING_SCOPE,
loggingConfig.scope().isPresent() ? LoggingScope.forName(loggingConfig.scope().get()) : LoggingScope.NONE);
builder.property(QuarkusRestClientProperties.LOGGING_BODY_LIMIT, loggingConfig.bodyLimit());
}
}

private void configureCustomProperties(QuarkusRestClientBuilder builder) {
Optional<String> encoder = oneOf(restClientConfig.multipartPostEncoderMode(), configRoot.multipartPostEncoderMode());
if (encoder != null && encoder.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,29 @@ public class QuarkusRestClientProperties {
*/
public static final String CAPTURE_STACKTRACE = "io.quarkus.rest.client.capture-stacktrace";

/**
* Scope of logging for the client.
* <br/>
* WARNING: beware of logging sensitive data
* <br/>
* The possible values are:
* <ul>
* <li>{@code request-response} - enables logging request and responses, including redirect responses</li>
* <li>{@code all} - enables logging requests and responses and lower-level logging</li>
* <li>{@code none} - no additional logging</li>
* </ul>
*
* This property is applicable to reactive REST clients only.
*/
public static final String LOGGING_SCOPE = "io.quarkus.rest.client.logging.scope";

/**
* How many characters of the body should be logged. Message body can be large and can easily pollute the logs.
* <p>
* By default, set to 100.
* <p>
* This property is applicable to reactive REST clients only.
*/
public static final String LOGGING_BODY_LIMIT = "io.quarkus.rest.client.logging.body-limit";

}
Loading