Merge pull request #6925 from vector-im/feature/bma/http_log

Feature/bma/http log
This commit is contained in:
Benoit Marty 2022-08-29 15:04:46 +02:00 committed by GitHub
commit 51dcaf7556
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 34 deletions

1
changelog.d/6925.misc Normal file
View file

@ -0,0 +1 @@
Log basic Http information in production.

View file

@ -42,7 +42,7 @@ internal object NetworkModule {
@Provides
@JvmStatic
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
val logger = FormattedJsonHttpLogger()
val logger = FormattedJsonHttpLogger(BuildConfig.OKHTTP_LOGGING_LEVEL)
val interceptor = HttpLoggingInterceptor(logger)
interceptor.level = BuildConfig.OKHTTP_LOGGING_LEVEL
return interceptor

View file

@ -23,15 +23,17 @@ import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber
internal class FormattedJsonHttpLogger : HttpLoggingInterceptor.Logger {
internal class FormattedJsonHttpLogger(
private val level: HttpLoggingInterceptor.Level
) : HttpLoggingInterceptor.Logger {
companion object {
private const val INDENT_SPACE = 2
}
/**
* Log the message and try to log it again as a JSON formatted string
* Note: it can consume a lot of memory but it is only in DEBUG mode
* Log the message and try to log it again as a JSON formatted string.
* Note: it can consume a lot of memory but it is only in DEBUG mode.
*
* @param message
*/
@ -39,6 +41,10 @@ internal class FormattedJsonHttpLogger : HttpLoggingInterceptor.Logger {
override fun log(@NonNull message: String) {
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("{")) {
// JSON Detected
try {

View file

@ -1,30 +0,0 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.network.interceptors
import androidx.annotation.NonNull
import okhttp3.logging.HttpLoggingInterceptor
/**
* No op logger
*/
internal class FormattedJsonHttpLogger : HttpLoggingInterceptor.Logger {
@Synchronized
override fun log(@NonNull message: String) {
}
}