Partager via


HttpLogOptions Class

  • java.lang.Object
    • com.azure.core.http.policy.HttpLogOptions

public class HttpLogOptions

The HttpLogOptions class provides configuration options for HTTP logging. This includes setting the log level, specifying allowed header names and query parameters for logging, and controlling whether to pretty print the body of HTTP messages.

This class is useful when you need to control the amount of information that is logged during the execution of HTTP requests and responses. It allows you to specify the log level, which determines the amount of detail included in the logs (such as the URL, headers, and body of requests and responses).

Code sample:

In this example, the HttpLogOptions is created and the log level is set to HttpLogDetailLevel.BODY_AND_HEADERS. This means that the URL, HTTP method, headers, and body content of each request and response will be logged. The allowed header names and query parameters for logging are also specified, and pretty printing of the body is enabled. The HttpLogOptions is then used to create an HttpLoggingPolicy, which can then be added to the pipeline.

HttpLogOptions logOptions = new HttpLogOptions();
 logOptions.setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS);
 logOptions.setAllowedHeaderNames(new HashSet<>(Arrays.asList("Date", "x-ms-request-id")));
 logOptions.setAllowedQueryParamNames(new HashSet<>(Arrays.asList("api-version")));
 logOptions.setPrettyPrintBody(true);
 HttpLoggingPolicy loggingPolicy = new HttpLoggingPolicy(logOptions);

Constructor Summary

Constructor Description
HttpLogOptions()

Creates a new instance that does not log any information about HTTP requests or responses.

Method Summary

Modifier and Type Method and Description
HttpLogOptions addAllowedHeaderName(String allowedHeaderName)

Sets the given allowed header to the default header set that should be logged.

HttpLogOptions addAllowedQueryParamName(String allowedQueryParamName)

Sets the given allowed query param that should be logged.

HttpLogOptions disableRedactedHeaderLogging(boolean disableRedactedHeaderLogging)

Sets the flag that controls if header names which value is redacted should be logged.

Set<String> getAllowedHeaderNames()

Gets the allowed headers that should be logged.

Set<String> getAllowedQueryParamNames()

Gets the allowed query parameters.

String getApplicationId()

Deprecated

Use ClientOptions to configure applicationId.

Gets the application specific id.

HttpLogDetailLevel getLogLevel()

Gets the level of detail to log on HTTP messages.

HttpRequestLogger getRequestLogger()

Gets the HttpRequestLogger that will be used to log HTTP requests.

HttpResponseLogger getResponseLogger()

Gets the HttpResponseLogger that will be used to log HTTP responses.

boolean isPrettyPrintBody()

Gets flag to allow pretty printing of message bodies.

boolean isRedactedHeaderLoggingDisabled()

Gets the flag that controls if header names with redacted values should be logged.

HttpLogOptions setAllowedHeaderNames(Set<String> allowedHeaderNames)

Sets the given allowed headers that should be logged.

HttpLogOptions setAllowedQueryParamNames(Set<String> allowedQueryParamNames)

Sets the given allowed query params to be displayed in the logging info.

HttpLogOptions setApplicationId(String applicationId)

Deprecated

Use ClientOptions to configure applicationId.

Sets the custom application specific id supplied by the user of the client library.

HttpLogOptions setLogLevel(HttpLogDetailLevel logLevel)

Sets the level of detail to log on Http messages.

HttpLogOptions setPrettyPrintBody(boolean prettyPrintBody)

Sets flag to allow pretty printing of message bodies.

HttpLogOptions setRequestLogger(HttpRequestLogger requestLogger)

Sets the HttpRequestLogger that will be used to log HTTP requests.

HttpLogOptions setResponseLogger(HttpResponseLogger responseLogger)

Sets the HttpResponseLogger that will be used to log HTTP responses.

Methods inherited from java.lang.Object

Constructor Details

HttpLogOptions

public HttpLogOptions()

Creates a new instance that does not log any information about HTTP requests or responses.

Method Details

addAllowedHeaderName

public HttpLogOptions addAllowedHeaderName(String allowedHeaderName)

Sets the given allowed header to the default header set that should be logged.

Parameters:

allowedHeaderName - The allowed header name from the user.

Returns:

The updated HttpLogOptions object.

addAllowedQueryParamName

public HttpLogOptions addAllowedQueryParamName(String allowedQueryParamName)

Sets the given allowed query param that should be logged.

Parameters:

allowedQueryParamName - The allowed query param name from the user.

Returns:

The updated HttpLogOptions object.

disableRedactedHeaderLogging

public HttpLogOptions disableRedactedHeaderLogging(boolean disableRedactedHeaderLogging)

Sets the flag that controls if header names which value is redacted should be logged.

Applies only if logging request and response headers is enabled. See setLogLevel(HttpLogDetailLevel logLevel) for details. Defaults to `false` - redacted header names are logged.

Parameters:

disableRedactedHeaderLogging - If true, redacted header names are not logged. Otherwise, they are logged as a comma separated list under redactedHeaders property.

Returns:

The updated HttpLogOptions object.

getAllowedHeaderNames

public Set getAllowedHeaderNames()

Gets the allowed headers that should be logged.

Returns:

The list of allowed headers.

getAllowedQueryParamNames

public Set getAllowedQueryParamNames()

Gets the allowed query parameters.

Returns:

The list of allowed query parameters.

getApplicationId

@Deprecated
public String getApplicationId()

Deprecated

Use ClientOptions to configure applicationId.

Gets the application specific id.

Returns:

The application specific id.

getLogLevel

public HttpLogDetailLevel getLogLevel()

Gets the level of detail to log on HTTP messages.

Returns:

getRequestLogger

public HttpRequestLogger getRequestLogger()

Gets the HttpRequestLogger that will be used to log HTTP requests.

A default HttpRequestLogger will be used if one isn't supplied.

Returns:

The HttpRequestLogger that will be used to log HTTP requests.

getResponseLogger

public HttpResponseLogger getResponseLogger()

Gets the HttpResponseLogger that will be used to log HTTP responses.

A default HttpResponseLogger will be used if one isn't supplied.

Returns:

The HttpResponseLogger that will be used to log HTTP responses.

isPrettyPrintBody

public boolean isPrettyPrintBody()

Gets flag to allow pretty printing of message bodies.

Returns:

true if pretty printing of message bodies is allowed.

isRedactedHeaderLoggingDisabled

public boolean isRedactedHeaderLoggingDisabled()

Gets the flag that controls if header names with redacted values should be logged.

Returns:

true if header names with redacted values should be logged.

setAllowedHeaderNames

public HttpLogOptions setAllowedHeaderNames(Set allowedHeaderNames)

Sets the given allowed headers that should be logged.

This method sets the provided header names to be the allowed header names which will be logged for all HTTP requests and responses, overwriting any previously configured headers. Additionally, users can use addAllowedHeaderName(String allowedHeaderName) or getAllowedHeaderNames() to add or remove more headers names to the existing set of allowed header names.

Parameters:

allowedHeaderNames - The list of allowed header names from the user.

Returns:

The updated HttpLogOptions object.

setAllowedQueryParamNames

public HttpLogOptions setAllowedQueryParamNames(Set allowedQueryParamNames)

Sets the given allowed query params to be displayed in the logging info.

Parameters:

allowedQueryParamNames - The list of allowed query params from the user.

Returns:

The updated HttpLogOptions object.

setApplicationId

@Deprecated
public HttpLogOptions setApplicationId(String applicationId)

Deprecated

Use ClientOptions to configure applicationId.

Sets the custom application specific id supplied by the user of the client library.

Parameters:

applicationId - The user specified application id.

Returns:

The updated HttpLogOptions object.

setLogLevel

public HttpLogOptions setLogLevel(HttpLogDetailLevel logLevel)

Sets the level of detail to log on Http messages.

If logLevel is not provided, default value of NONE is set.

Parameters:

logLevel - The HttpLogDetailLevel.

Returns:

The updated HttpLogOptions object.

setPrettyPrintBody

public HttpLogOptions setPrettyPrintBody(boolean prettyPrintBody)

Sets flag to allow pretty printing of message bodies.

Parameters:

prettyPrintBody - If true, pretty prints message bodies when logging. If the detailLevel does not include body logging, this flag does nothing.

Returns:

The updated HttpLogOptions object.

setRequestLogger

public HttpLogOptions setRequestLogger(HttpRequestLogger requestLogger)

Sets the HttpRequestLogger that will be used to log HTTP requests.

A default HttpRequestLogger will be used if one isn't supplied.

Parameters:

requestLogger - The HttpRequestLogger that will be used to log HTTP requests.

Returns:

The updated HttpLogOptions object.

setResponseLogger

public HttpLogOptions setResponseLogger(HttpResponseLogger responseLogger)

Sets the HttpResponseLogger that will be used to log HTTP responses.

A default HttpResponseLogger will be used if one isn't supplied.

Parameters:

responseLogger - The HttpResponseLogger that will be used to log HTTP responses.

Returns:

The updated HttpLogOptions object.

Applies to