Avoid trying to log formatted Json if Http bodies are not logged.
This commit is contained in:
parent
8cec528ac7
commit
2c63dee86a
@ -42,7 +42,7 @@ internal object NetworkModule {
|
|||||||
@Provides
|
@Provides
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
|
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
|
||||||
val logger = FormattedJsonHttpLogger()
|
val logger = FormattedJsonHttpLogger(BuildConfig.OKHTTP_LOGGING_LEVEL)
|
||||||
val interceptor = HttpLoggingInterceptor(logger)
|
val interceptor = HttpLoggingInterceptor(logger)
|
||||||
interceptor.level = BuildConfig.OKHTTP_LOGGING_LEVEL
|
interceptor.level = BuildConfig.OKHTTP_LOGGING_LEVEL
|
||||||
return interceptor
|
return interceptor
|
||||||
|
@ -23,7 +23,9 @@ import org.json.JSONException
|
|||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
internal class FormattedJsonHttpLogger : HttpLoggingInterceptor.Logger {
|
internal class FormattedJsonHttpLogger(
|
||||||
|
private val level: HttpLoggingInterceptor.Level
|
||||||
|
) : HttpLoggingInterceptor.Logger {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val INDENT_SPACE = 2
|
private const val INDENT_SPACE = 2
|
||||||
@ -39,6 +41,10 @@ internal class FormattedJsonHttpLogger : HttpLoggingInterceptor.Logger {
|
|||||||
override fun log(@NonNull message: String) {
|
override fun log(@NonNull message: String) {
|
||||||
Timber.v(message)
|
Timber.v(message)
|
||||||
|
|
||||||
|
// Try to log formatted Json only if there is a chance that [message] contains Json.
|
||||||
|
// It can be only the case if we log the bodies of Http requests.
|
||||||
|
if (level != HttpLoggingInterceptor.Level.BODY) return
|
||||||
|
|
||||||
if (message.startsWith("{")) {
|
if (message.startsWith("{")) {
|
||||||
// JSON Detected
|
// JSON Detected
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user