[Upload]网络请求日志分级

This commit is contained in:
donghongyu
2021-11-19 16:16:11 +08:00
parent c18b46b5c2
commit 501dac9e55
3 changed files with 50 additions and 29 deletions

View File

@@ -42,15 +42,16 @@ class HttpLoggingInterceptor : Interceptor {
}
val logMsg = StringBuilder()
.append("------> http request start").append("\r\n")
.append(protocol).append(", ")
.append(request.method()).append("\r\n")
.append("Request Headers: {").append(request.headers().toString().replace("\n",", ")).append("}\r\n")
.append("Url: ").append(request.url()).append("\r\n")
.append("Content-Type: ").append(requestBody?.contentType()).append("\r\n")
.append("Content-Length: ").append(requestBody?.contentLength()).append("\r\n")
.append("Content-body: ").append(body).append("\r\n")
.append("------> http request end").append("\r\n").append("\r\n")
.append("------> http request start").append("\r\n")
.append(protocol).append(", ")
.append(request.method()).append("\r\n")
.append("Request Headers: {").append(request.headers().toString().replace("\n", ", "))
.append("}\r\n")
.append("Url: ").append(request.url()).append("\r\n")
.append("Content-Type: ").append(requestBody?.contentType()).append("\r\n")
.append("Content-Length: ").append(requestBody?.contentLength()).append("\r\n")
.append("Content-body: ").append(body).append("\r\n")
.append("------> http request end").append("\r\n").append("\r\n")
val startTime = System.nanoTime()
@@ -72,16 +73,21 @@ class HttpLoggingInterceptor : Interceptor {
}
logMsg.append("------> http response start").append("\r\n")
.append(response.code()).append(" ")
.append(response.message()).append(" ")
.append(response.protocol()).append(" ").append("\r\n")
.append("Response Content: ").append(responseContent).append("\r\n")
.append("Content-Type: ").append(contentType).append("\r\n")
.append("Content-Length: ").append(bodySize).append("\r\n")
.append("Time: (").append(endTime).append("ms)").append("\r\n")
.append("------> http response end")
Logger.d(TAG, logMsg.toString())
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response
.append(response.code()).append(" ")
.append(response.message()).append(" ")
.append(response.protocol()).append(" ").append("\r\n")
.append("Response Content: ").append(responseContent).append("\r\n")
.append("Content-Type: ").append(contentType).append("\r\n")
.append("Content-Length: ").append(bodySize).append("\r\n")
.append("Time: (").append(endTime).append("ms)").append("\r\n")
.append("------> http response end")
if (response.code() != 200) {
Logger.e(TAG, logMsg.toString())
} else {
Logger.d(TAG, logMsg.toString())
}
return if (consumedResponse) response.newBuilder()
.body(ResponseBody.create(contentType, responseContent)).build() else response
}
}