mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Improve http logs
This commit is contained in:
		
							parent
							
								
									dc215dc80e
								
							
						
					
					
						commit
						b7fd663d77
					
				| @ -22,18 +22,25 @@ | |||||||
|  */ |  */ | ||||||
| package com.owncloud.android.lib.common.http | package com.owncloud.android.lib.common.http | ||||||
| 
 | 
 | ||||||
|  | import timber.log.Timber | ||||||
|  | import java.util.Locale | ||||||
|  | 
 | ||||||
| object LogBuilder { | object LogBuilder { | ||||||
|     fun toLogString( |     fun logHttp( | ||||||
|         networkPetition: NetworkPetition, |         networkPetition: NetworkPetition, | ||||||
|         networkNode: NetworkNode, |         networkNode: NetworkNode, | ||||||
|         description: String |         description: String | ||||||
|     ): String = "[Network, $networkPetition] [$networkNode] $description" |     ) = Timber.d("[Network, $networkPetition] [$networkNode] $description") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| enum class NetworkPetition { | enum class NetworkPetition { | ||||||
|     REQUEST, RESPONSE |     REQUEST, RESPONSE; | ||||||
|  | 
 | ||||||
|  |     override fun toString(): String = super.toString().toLowerCase(Locale.ROOT) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| enum class NetworkNode { | enum class NetworkNode { | ||||||
|     INFO, HEADER, BODY |     INFO, HEADER, BODY; | ||||||
|  | 
 | ||||||
|  |     override fun toString(): String = super.toString().toLowerCase(Locale.ROOT) | ||||||
| } | } | ||||||
|  | |||||||
| @ -23,12 +23,11 @@ | |||||||
|  */ |  */ | ||||||
| package com.owncloud.android.lib.common.http | package com.owncloud.android.lib.common.http | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.http.LogBuilder.toLogString | import com.owncloud.android.lib.common.http.LogBuilder.logHttp | ||||||
| import com.owncloud.android.lib.common.http.NetworkNode.* | import com.owncloud.android.lib.common.http.NetworkNode.* | ||||||
| import com.owncloud.android.lib.common.http.NetworkPetition.* | import com.owncloud.android.lib.common.http.NetworkPetition.* | ||||||
| import okhttp3.Interceptor | import okhttp3.Interceptor | ||||||
| import okhttp3.Response | import okhttp3.Response | ||||||
| import timber.log.Timber |  | ||||||
| 
 | 
 | ||||||
| class LogInterceptor : Interceptor { | class LogInterceptor : Interceptor { | ||||||
| 
 | 
 | ||||||
| @ -39,28 +38,14 @@ class LogInterceptor : Interceptor { | |||||||
|         return response.also { |         return response.also { | ||||||
|             if (httpLogsEnabled) { |             if (httpLogsEnabled) { | ||||||
|                 // Log request |                 // Log request | ||||||
|                 Timber.d( |                 logHttp(REQUEST, INFO, "Type: ${it.request.method} URL: ${it.request.url}") | ||||||
|                     toLogString(REQUEST, INFO, "Type: ${it.request.method} URL: ${it.request.url}") |                 it.headers.forEach { header -> logHttp(REQUEST, HEADER, header.toString()) } | ||||||
|                 ) |                 logHttp(REQUEST, BODY, it.body.toString()) | ||||||
|                 it.headers.forEach { header -> |  | ||||||
|                     Timber.d(toLogString(REQUEST, HEADER, header.toString())) |  | ||||||
|                 } |  | ||||||
|                 Timber.d(toLogString(REQUEST, BODY, it.body.toString())) |  | ||||||
| 
 | 
 | ||||||
|                 // Log response |                 // Log response | ||||||
|                 Timber.d(toLogString(RESPONSE, INFO, "RequestId: ${it.request.header(HttpConstants.OC_X_REQUEST_ID)}")) |                 logHttp(RESPONSE, INFO, "Code: ${it.code}  Message: ${it.message} IsSuccessful: ${it.isSuccessful}") | ||||||
|                 Timber.d( |                 it.headers.forEach { header -> logHttp(RESPONSE, HEADER, header.toString()) } | ||||||
|                     toLogString( |                 logHttp(RESPONSE, BODY, it.body.toString()) | ||||||
|                         RESPONSE, |  | ||||||
|                         INFO, |  | ||||||
|                         "Code: ${it.code}  Message: ${it.message} IsSuccessful: ${it.isSuccessful}" |  | ||||||
|                     ) |  | ||||||
|                 ) |  | ||||||
|                 it.headers.forEach { header -> |  | ||||||
|                     Timber.d(toLogString(RESPONSE, HEADER, header.toString())) |  | ||||||
|                 } |  | ||||||
|                 Timber.d(toLogString(RESPONSE, BODY, it.body.toString())) |  | ||||||
| 
 |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user