|
| 1 | +package com.volcengine.interceptor; |
| 2 | + |
| 3 | +import com.squareup.okhttp.Connection; |
| 4 | +import com.squareup.okhttp.Headers; |
| 5 | +import com.squareup.okhttp.Interceptor; |
| 6 | +import com.squareup.okhttp.MediaType; |
| 7 | +import com.squareup.okhttp.Protocol; |
| 8 | +import com.squareup.okhttp.Request; |
| 9 | +import com.squareup.okhttp.RequestBody; |
| 10 | +import com.squareup.okhttp.Response; |
| 11 | +import com.squareup.okhttp.ResponseBody; |
| 12 | +import com.squareup.okhttp.internal.http.HttpEngine; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import com.volcengine.utils.LoggerUtil; |
| 17 | +import okio.Buffer; |
| 18 | +import okio.BufferedSource; |
| 19 | +import static com.volcengine.utils.ConstantsUtil.NEW_LINE; |
| 20 | + |
| 21 | +public final class HttpLoggingInterceptor implements Interceptor { |
| 22 | + |
| 23 | + private static final LoggerUtil REQUEST_LOGGER = LoggerUtil.loggerFor("com.volcengine.request"); |
| 24 | + private static final LoggerUtil REQUEST_ID_LOGGER = LoggerUtil.loggerFor("com.volcengine.request.requestId"); |
| 25 | + private static final String HEAD_LOG_ID = "X-Tt-Logid"; |
| 26 | + |
| 27 | + private static final Charset UTF8 = Charset.forName("UTF-8"); |
| 28 | + |
| 29 | + public HttpLoggingInterceptor() { |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Response intercept(Chain chain) throws IOException { |
| 35 | + // 打印请求的日志 |
| 36 | + logRequestLog(chain); |
| 37 | + Request request = chain.request(); |
| 38 | + long startNs = System.nanoTime(); |
| 39 | + Response response = chain.proceed(request); |
| 40 | + long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); |
| 41 | + // 打印响应的日志 |
| 42 | + logResponseLog(response, tookMs); |
| 43 | + return response; |
| 44 | + } |
| 45 | + |
| 46 | + private void logResponseLog(Response response, long tookMs) throws IOException { |
| 47 | + Headers headers = response.headers(); |
| 48 | + if (REQUEST_ID_LOGGER.isDebugEnabled()){ |
| 49 | + String responseState = response.isSuccessful() ? "successful" : "failed"; |
| 50 | + REQUEST_ID_LOGGER.debug("Received " + responseState + " response: " + response.code() + ", " + "RequestId: " + headers.get(HEAD_LOG_ID)); |
| 51 | + } |
| 52 | + |
| 53 | + if (REQUEST_LOGGER.isDebugEnabled() || REQUEST_LOGGER.isTraceEnabled()) { |
| 54 | + StringBuilder responseInfo = new StringBuilder(); |
| 55 | + ResponseBody responseBody = response.body(); |
| 56 | + responseInfo.append(NEW_LINE + "<-- " + protocol(response.protocol()) + ' ' + response.code() + ' ' |
| 57 | + + response.message() + " (" + tookMs + "ms" |
| 58 | + + ", " + responseBody.contentLength() + "-byte body" + ')'); |
| 59 | + |
| 60 | + for (int i = 0, count = headers.size(); i < count; i++) { |
| 61 | + responseInfo.append(NEW_LINE + headers.name(i) + ": " + headers.value(i)); |
| 62 | + } |
| 63 | + |
| 64 | + if (!REQUEST_LOGGER.isTraceEnabled() || !HttpEngine.hasBody(response)) { |
| 65 | + responseInfo.append(NEW_LINE + "<-- END HTTP"); |
| 66 | + } else if (bodyEncoded(response.headers())) { |
| 67 | + responseInfo.append(NEW_LINE + "<-- END HTTP (encoded body omitted)"); |
| 68 | + } else { |
| 69 | + BufferedSource source = responseBody.source(); |
| 70 | + source.request(Long.MAX_VALUE); // Buffer the entire body. |
| 71 | + Buffer buffer = source.buffer(); |
| 72 | + |
| 73 | + Charset charset = UTF8; |
| 74 | + MediaType contentType = responseBody.contentType(); |
| 75 | + if (contentType != null) { |
| 76 | + charset = contentType.charset(UTF8); |
| 77 | + } |
| 78 | + |
| 79 | + if (responseBody.contentLength() != 0) { |
| 80 | + responseInfo.append(NEW_LINE + "ResponseBody: "); |
| 81 | + responseInfo.append(buffer.clone().readString(charset)); |
| 82 | + } |
| 83 | + |
| 84 | + responseInfo.append(NEW_LINE + "<-- END HTTP (" + buffer.size() + "-byte body)"); |
| 85 | + } |
| 86 | + REQUEST_LOGGER.debug(responseInfo::toString); |
| 87 | + REQUEST_LOGGER.trace(responseInfo::toString); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private void logRequestLog(Chain chain) throws IOException { |
| 93 | + |
| 94 | + REQUEST_ID_LOGGER.debug(()->{ |
| 95 | + Request request = chain.request(); |
| 96 | + Connection connection = chain.connection(); |
| 97 | + Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1; |
| 98 | + return request.method() + ' ' + request.httpUrl() + ' ' + protocol(protocol); |
| 99 | + }); |
| 100 | + |
| 101 | + if (REQUEST_LOGGER.isDebugEnabled()){ |
| 102 | + Request request = chain.request(); |
| 103 | + RequestBody requestBody = request.body(); |
| 104 | + boolean hasRequestBody = requestBody != null; |
| 105 | + Connection connection = chain.connection(); |
| 106 | + Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1; |
| 107 | + StringBuilder requestLog = new StringBuilder(); |
| 108 | + requestLog.append("--> " + request.method() + ' ' + request.httpUrl() + ' ' + protocol(protocol)); |
| 109 | + if (hasRequestBody) { |
| 110 | + requestLog.append(" (" + requestBody.contentLength() + "-byte body)"); |
| 111 | + } |
| 112 | + |
| 113 | + if (hasRequestBody) { |
| 114 | + // Request body headers are only present when installed as a network interceptor. Force |
| 115 | + // them to be included (when available) so there values are known. |
| 116 | + if (requestBody.contentType() != null) { |
| 117 | + requestLog.append(NEW_LINE + "Content-Type: " + requestBody.contentType()); |
| 118 | + } |
| 119 | + if (requestBody.contentLength() != -1) { |
| 120 | + requestLog.append(NEW_LINE + "Content-Length: " + requestBody.contentLength()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Headers headers = request.headers(); |
| 125 | + for (int i = 0, count = headers.size(); i < count; i++) { |
| 126 | + String name = headers.name(i); |
| 127 | + // Skip headers from the request body as they are explicitly logged above. |
| 128 | + if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) { |
| 129 | + requestLog.append(NEW_LINE + ": " + headers.value(i)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (!hasRequestBody) { |
| 134 | + requestLog.append(NEW_LINE + "--> END " + request.method()); |
| 135 | + } else if (bodyEncoded(request.headers())) { |
| 136 | + requestLog.append(NEW_LINE + "--> END " + request.method() + " (encoded body omitted)"); |
| 137 | + } else { |
| 138 | + Buffer buffer = new Buffer(); |
| 139 | + requestBody.writeTo(buffer); |
| 140 | + |
| 141 | + Charset charset = UTF8; |
| 142 | + MediaType contentType = requestBody.contentType(); |
| 143 | + if (contentType != null) { |
| 144 | + contentType.charset(UTF8); |
| 145 | + } |
| 146 | + |
| 147 | + requestLog.append(""); |
| 148 | + requestLog.append(buffer.readString(charset)); |
| 149 | + |
| 150 | + requestLog.append("--> END " + request.method() |
| 151 | + + " (" + requestBody.contentLength() + "-byte body)"); |
| 152 | + } |
| 153 | + REQUEST_LOGGER.debug(requestLog.toString()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private boolean bodyEncoded(Headers headers) { |
| 158 | + String contentEncoding = headers.get("Content-Encoding"); |
| 159 | + return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity"); |
| 160 | + } |
| 161 | + |
| 162 | + private static String protocol(Protocol protocol) { |
| 163 | + return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1"; |
| 164 | + } |
| 165 | +} |
0 commit comments