in sdk/core/azure-core-http/src/main/java/com/azure/android/core/http/policy/HttpLoggingPolicy.java [81:205]
public void process(HttpPipelinePolicyChain chain) {
// No logging will be performed, trigger a no-op.
if (httpLogDetailLevel == HttpLogDetailLevel.NONE) {
chain.processNextPolicy(chain.getRequest());
return;
}
// final ClientLogger logger = new ClientLogger((String) context.getData("caller-method").orElse(""));
final ClientLogger logger = new ClientLogger((String) "caller-method"); // TODO: bring context ^
if (!logger.canLogAtLevel(LogLevel.INFORMATIONAL)) {
chain.processNextPolicy(chain.getRequest());
return;
}
final long startNs = System.nanoTime();
final HttpRequest httpRequest = chain.getRequest();
// final Integer retryCount = context.getData(RETRY_COUNT_CONTEXT);
// final Integer retryCount = null; // TODO: bring context ^
StringBuilder requestLogMessage = new StringBuilder();
if (httpLogDetailLevel.shouldLogUrl()) {
requestLogMessage.append("--> ")
.append(httpRequest.getHttpMethod())
.append(" ")
.append(this.getRedactedUrl(httpRequest.getUrl()))
.append(LINE_SEPARATOR);
// if (retryCount != null) {
// requestLogMessage.append(retryCount).append(LINE_SEPARATOR);
// }
}
this.appendHeaders(logger, httpRequest.getHeaders(), requestLogMessage);
if (httpLogDetailLevel.shouldLogBody()) {
if (httpRequest.getBody() == null) {
requestLogMessage.append("(empty body)")
.append(LINE_SEPARATOR)
.append("--> END ")
.append(httpRequest.getHttpMethod())
.append(LINE_SEPARATOR);
} else {
final String requestContentType = httpRequest.getHeaders().getValue("Content-Type");
final long requestContentLength = this.getContentLength(logger, httpRequest.getHeaders());
if (this.isContentLoggable(requestContentType, requestContentLength)) {
final String content = this.convertBytesToString(httpRequest.getBody(), logger);
requestLogMessage.append(requestContentLength)
.append("-byte body:")
.append(LINE_SEPARATOR)
.append(content)
.append(LINE_SEPARATOR)
.append("--> END ")
.append(httpRequest.getHttpMethod())
.append(LINE_SEPARATOR);
} else {
requestLogMessage.append(requestContentLength)
.append("-byte body: (content not logged)")
.append(LINE_SEPARATOR)
.append("--> END ")
.append(httpRequest.getHttpMethod())
.append(LINE_SEPARATOR);
}
}
}
logger.info(requestLogMessage.toString());
chain.processNextPolicy(httpRequest, new NextPolicyCallback() {
@Override
public PolicyCompleter.CompletionState onSuccess(HttpResponse response, PolicyCompleter completer) {
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
final String contentLengthHeaderValue = response.getHeaderValue("Content-Length");
final String contentLengthMessage
= (contentLengthHeaderValue == null || contentLengthHeaderValue.length() == 0)
? "unknown-length body"
: contentLengthHeaderValue + "-byte body";
StringBuilder responseLogMessage = new StringBuilder();
if (httpLogDetailLevel.shouldLogUrl()) {
responseLogMessage.append("<-- ")
.append(response.getStatusCode())
.append(" ")
.append(getRedactedUrl(response.getRequest().getUrl()))
.append(" (")
.append(tookMs)
.append(" ms, ")
.append(contentLengthMessage)
.append(")")
.append(LINE_SEPARATOR);
}
appendHeaders(logger, response.getHeaders(), responseLogMessage);
HttpResponse httpResponse = response;
if (httpLogDetailLevel.shouldLogBody()) {
final String responseContentType = response.getHeaderValue("Content-Type");
final long responseContentLength = getContentLength(logger, response.getHeaders());
if (isContentLoggable(responseContentType, responseContentLength)) {
httpResponse = response.buffer();
final String content = convertBytesToString(httpResponse.getBodyAsByteArray(), logger);
responseLogMessage.append("Response body:")
.append(LINE_SEPARATOR)
.append(content)
.append(LINE_SEPARATOR)
.append("<-- END HTTP");
} else {
responseLogMessage.append("(body content not logged)")
.append(LINE_SEPARATOR)
.append("<-- END HTTP");
}
} else {
responseLogMessage.append("<-- END HTTP");
}
logger.info(responseLogMessage.toString());
return completer.completed(httpResponse);
}
@Override
public PolicyCompleter.CompletionState onError(Throwable error, PolicyCompleter completer) {
logger.warning("<-- HTTP FAILED: ", error);
return completer.completedError(error);
}
});
}