Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 775fdb0

Browse files
committed
Add first draft to log network calls
1 parent e28335a commit 775fdb0

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class HttpClient {
5757
private static OkHttpClient sOkHttpClient;
5858
private static Context sContext;
5959
private static HashMap<String, List<Cookie>> sCookieStore = new HashMap<>();
60+
private static LogInterceptor sLogInterceptor;
6061

6162
public static OkHttpClient getOkHttpClient() {
6263
if (sOkHttpClient == null) {
@@ -111,6 +112,7 @@ public List<Cookie> loadForRequest(HttpUrl url) {
111112

112113
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
113114
.protocols(Arrays.asList(Protocol.HTTP_1_1))
115+
.addInterceptor(getLogInterceptor())
114116
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
115117
.writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
116118
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
@@ -137,6 +139,13 @@ public static void setContext(Context context) {
137139
sContext = context;
138140
}
139141

142+
public static LogInterceptor getLogInterceptor() {
143+
if (sLogInterceptor == null) {
144+
sLogInterceptor = new LogInterceptor();
145+
}
146+
return sLogInterceptor;
147+
}
148+
140149
public List<Cookie> getCookiesFromUrl(HttpUrl httpUrl) {
141150
return sCookieStore.get(httpUrl.host());
142151
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.common.http
24+
25+
object LogBuilder {
26+
fun toLogString(
27+
networkPetition: NetworkPetition,
28+
networkNode: NetworkNode,
29+
description: String
30+
): String = "[Network, $networkPetition] [$networkNode] $description"
31+
}
32+
33+
enum class NetworkPetition {
34+
REQUEST, RESPONSE
35+
}
36+
37+
enum class NetworkNode {
38+
INFO, HEADER, BODY
39+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
package com.owncloud.android.lib.common.http
25+
26+
import com.owncloud.android.lib.common.http.LogBuilder.toLogString
27+
import okhttp3.Interceptor
28+
import okhttp3.Response
29+
import timber.log.Timber
30+
31+
class LogInterceptor : Interceptor {
32+
33+
override fun intercept(chain: Interceptor.Chain): Response {
34+
// Log request
35+
chain.request().let {
36+
Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.INFO, "Type: ${it.method} URL: ${it.url}"))
37+
it.headers.forEach { header ->
38+
Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.HEADER, header.toString()))
39+
}
40+
Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.BODY, it.body.toString()))
41+
}
42+
43+
val response = chain.proceed(chain.request())
44+
45+
// Log response
46+
response.let {
47+
Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.INFO, "RequestId: ${it.request.header(HttpConstants.OC_X_REQUEST_ID)}"))
48+
Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.INFO, "Code: ${it.code} Message: ${it.message} IsSuccessful: ${it.isSuccessful}"))
49+
it.headers.forEach { header ->
50+
Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.HEADER, header.toString()))
51+
}
52+
Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.BODY, it.body.toString()))
53+
}
54+
55+
return response
56+
}
57+
}

0 commit comments

Comments
 (0)