1
+ /* ******************************************************************************
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2019 vk.com
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ ******************************************************************************/
24
+ package com.vk.api.sdk.okhttp
25
+
26
+ import android.support.v4.util.ArrayMap
27
+ import com.vk.api.sdk.utils.log.Logger
28
+ import com.vk.api.sdk.utils.getValue
29
+ import com.vk.api.sdk.utils.threadLocal
30
+ import okhttp3.Interceptor
31
+ import okhttp3.Response
32
+ import okhttp3.logging.HttpLoggingInterceptor
33
+
34
+ class LoggingInterceptor (private val filterCredentials : Boolean , private val logger : Logger ) : Interceptor {
35
+ private val delegate by threadLocal {
36
+ HttpLoggingInterceptor (object : HttpLoggingInterceptor .Logger {
37
+ override fun log (message : String ) {
38
+ val finalMessage = if (filterCredentials) filterCredentials(message) else message
39
+ logger.log(logger.logLevel, finalMessage)
40
+ }
41
+
42
+ private fun filterCredentials (msg : String ): String {
43
+ return msg
44
+ .replace(" access_token=[a-z0-9]+" .toRegex(), " access_token=<HIDE>" )
45
+ .replace(" key=[a-z0-9]+" .toRegex(), " key=<HIDE>" )
46
+ }
47
+ })
48
+ }
49
+
50
+ override fun intercept (chain : Interceptor .Chain ): Response {
51
+ // Do not log big bodies because of probability of OutOfMemoryError
52
+ val bodyLength = chain.request().body()?.contentLength() ? : 0
53
+ delegate.level =
54
+ if (bodyLength > 1024L ) HttpLoggingInterceptor .Level .BASIC
55
+ else LogLevelMap .levelsMap[logger.logLevel]
56
+ return delegate.intercept(chain)
57
+ }
58
+
59
+ object LogLevelMap {
60
+ val levelsMap = ArrayMap <Logger .LogLevel , HttpLoggingInterceptor .Level >()
61
+ init {
62
+ levelsMap[Logger .LogLevel .NONE ] = HttpLoggingInterceptor .Level .NONE
63
+ levelsMap[Logger .LogLevel .ERROR ] = HttpLoggingInterceptor .Level .NONE
64
+ levelsMap[Logger .LogLevel .WARNING ] = HttpLoggingInterceptor .Level .BASIC
65
+ levelsMap[Logger .LogLevel .DEBUG ] = HttpLoggingInterceptor .Level .HEADERS
66
+ levelsMap[Logger .LogLevel .VERBOSE ] = HttpLoggingInterceptor .Level .BODY
67
+ }
68
+ }
69
+ }
0 commit comments