Do not log big data request (ex: file upload)

This commit is contained in:
Benoit Marty 2019-09-25 11:26:49 +02:00
parent ae8bceacba
commit 4c04014e4d
1 changed files with 15 additions and 8 deletions

View File

@ -22,6 +22,7 @@ import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import okio.Buffer import okio.Buffer
import timber.log.Timber
import java.io.IOException import java.io.IOException
import java.nio.charset.Charset import java.nio.charset.Charset
import javax.inject.Inject import javax.inject.Inject
@ -58,15 +59,21 @@ internal class CurlLoggingInterceptor @Inject constructor(private val logger: Ht
val requestBody = request.body() val requestBody = request.body()
if (requestBody != null) { if (requestBody != null) {
val buffer = Buffer() if (requestBody.contentLength() > 100_000) {
requestBody.writeTo(buffer) Timber.w("Unable to log curl command data, size is too big (${requestBody.contentLength()})")
var charset: Charset? = UTF8 // Ensure the curl command will failed
val contentType = requestBody.contentType() curlCmd += "DATA IS TOO BIG"
if (contentType != null) { } else {
charset = contentType.charset(UTF8) val buffer = Buffer()
requestBody.writeTo(buffer)
var charset: Charset? = UTF8
val contentType = requestBody.contentType()
if (contentType != null) {
charset = contentType.charset(UTF8)
}
// try to keep to a single line and use a subshell to preserve any line breaks
curlCmd += " --data $'" + buffer.readString(charset!!).replace("\n", "\\n") + "'"
} }
// try to keep to a single line and use a subshell to preserve any line breaks
curlCmd += " --data $'" + buffer.readString(charset!!).replace("\n", "\\n") + "'"
} }
val headers = request.headers() val headers = request.headers()