Skip to content

Commit 7a28de3

Browse files
authored
Add LogLevel Hooks to HttpLogOptions and HttpLoggingPolicy (Azure#16088)
Add LogLevel Hooks to HttpLogOptions and HttpLoggingPolicy
1 parent 412be1e commit 7a28de3

File tree

8 files changed

+499
-149
lines changed

8 files changed

+499
-149
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineCallContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
package com.azure.core.http;
55

6+
import com.azure.core.implementation.http.HttpPipelineCallContextHelper;
67
import com.azure.core.util.Context;
78

89
import java.util.Objects;
910
import java.util.Optional;
1011

1112
/**
12-
* Type representing context local to a single http request and it's response.
13+
* Represents the information used to make a single HTTP request.
1314
*/
1415
public final class HttpPipelineCallContext {
1516
private HttpRequest httpRequest;
@@ -46,6 +47,10 @@ public final class HttpPipelineCallContext {
4647
this.data = data;
4748
}
4849

50+
static {
51+
HttpPipelineCallContextHelper.setAccessor(HttpPipelineCallContext::getContext);
52+
}
53+
4954
/**
5055
* Stores a key-value data in the context.
5156
*

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/HttpLogOptions.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
package com.azure.core.http.policy;
55

6-
import com.azure.core.util.CoreUtils;
76
import com.azure.core.util.ClientOptions;
7+
import com.azure.core.util.CoreUtils;
88
import com.azure.core.util.logging.ClientLogger;
99

1010
import java.util.Arrays;
@@ -22,6 +22,10 @@ public class HttpLogOptions {
2222
private Set<String> allowedHeaderNames;
2323
private Set<String> allowedQueryParamNames;
2424
private boolean prettyPrintBody;
25+
26+
private HttpRequestLogger requestLogger;
27+
private HttpResponseLogger responseLogger;
28+
2529
private final ClientLogger logger = new ClientLogger(HttpLogOptions.class);
2630

2731
private static final int MAX_APPLICATION_ID_LENGTH = 24;
@@ -96,10 +100,9 @@ public Set<String> getAllowedHeaderNames() {
96100
*
97101
* <p>
98102
* This method sets the provided header names to be the whitelisted header names which will be logged for all HTTP
99-
* requests and responses, overwriting any previously configured headers, including the default set. Additionally,
100-
* users can use {@link HttpLogOptions#addAllowedHeaderName(String)} or
101-
* {@link HttpLogOptions#getAllowedHeaderNames()} to add or remove more headers names to the existing set of
102-
* allowed header names.
103+
* requests and responses, overwriting any previously configured headers. Additionally, users can use {@link
104+
* HttpLogOptions#addAllowedHeaderName(String)} or {@link HttpLogOptions#getAllowedHeaderNames()} to add or remove
105+
* more headers names to the existing set of allowed header names.
103106
* </p>
104107
*
105108
* @param allowedHeaderNames The list of whitelisted header names from the user.
@@ -202,12 +205,60 @@ public boolean isPrettyPrintBody() {
202205
/**
203206
* Sets flag to allow pretty printing of message bodies.
204207
*
205-
* @param prettyPrintBody If true, pretty prints message bodies when logging. If the detailLevel does not
206-
* include body logging, this flag does nothing.
208+
* @param prettyPrintBody If true, pretty prints message bodies when logging. If the detailLevel does not include
209+
* body logging, this flag does nothing.
207210
* @return The updated HttpLogOptions object.
208211
*/
209212
public HttpLogOptions setPrettyPrintBody(boolean prettyPrintBody) {
210213
this.prettyPrintBody = prettyPrintBody;
211214
return this;
212215
}
216+
217+
/**
218+
* Gets the {@link HttpRequestLogger} that will be used to log HTTP requests.
219+
* <p>
220+
* A default {@link HttpRequestLogger} will be used if one isn't supplied.
221+
*
222+
* @return The {@link HttpRequestLogger} that will be used to log HTTP requests.
223+
*/
224+
public HttpRequestLogger getRequestLogger() {
225+
return requestLogger;
226+
}
227+
228+
/**
229+
* Sets the {@link HttpRequestLogger} that will be used to log HTTP requests.
230+
* <p>
231+
* A default {@link HttpRequestLogger} will be used if one isn't supplied.
232+
*
233+
* @param requestLogger The {@link HttpRequestLogger} that will be used to log HTTP requests.
234+
* @return The updated HttpLogOptions object.
235+
*/
236+
public HttpLogOptions setRequestLogger(HttpRequestLogger requestLogger) {
237+
this.requestLogger = requestLogger;
238+
return this;
239+
}
240+
241+
/**
242+
* Gets the {@link HttpResponseLogger} that will be used to log HTTP responses.
243+
* <p>
244+
* A default {@link HttpResponseLogger} will be used if one isn't supplied.
245+
*
246+
* @return The {@link HttpResponseLogger} that will be used to log HTTP responses.
247+
*/
248+
public HttpResponseLogger getResponseLogger() {
249+
return responseLogger;
250+
}
251+
252+
/**
253+
* Sets the {@link HttpResponseLogger} that will be used to log HTTP responses.
254+
* <p>
255+
* A default {@link HttpResponseLogger} will be used if one isn't supplied.
256+
*
257+
* @param responseLogger The {@link HttpResponseLogger} that will be used to log HTTP responses.
258+
* @return The updated HttpLogOptions object.
259+
*/
260+
public HttpLogOptions setResponseLogger(HttpResponseLogger responseLogger) {
261+
this.responseLogger = responseLogger;
262+
return this;
263+
}
213264
}

0 commit comments

Comments
 (0)