ソースを参照

修改日志拦截器错误信息

drake 2 年 前
コミット
35554298b9

+ 2 - 8
net/src/main/java/com/drake/net/interceptor/LogRecordInterceptor.kt

@@ -1,6 +1,5 @@
 package com.drake.net.interceptor
 
-import android.util.Log
 import com.drake.net.log.LogRecorder
 import com.drake.net.request.logString
 import com.drake.net.response.logString
@@ -55,13 +54,8 @@ open class LogRecordInterceptor(
             )
             return response
         } catch (e: Exception) {
-            LogRecorder.recordException(
-                generateId,
-                System.currentTimeMillis(),
-                -1,
-                null,
-                Log.getStackTraceString(e)
-            )
+            val error = "Review LogCat for details, occurred exception: ${e.javaClass.simpleName}"
+            LogRecorder.recordException(generateId, System.currentTimeMillis(), -1, null, error)
             throw e
         }
     }

+ 8 - 8
net/src/main/java/com/drake/net/log/LogRecorder.kt

@@ -81,9 +81,7 @@ object LogRecorder {
             if (header.length > 2) header = header.substring(1, header.length - 1)
             fastLog(id, MessageType.REQUEST_HEADER, key + HEADER_DELIMITER + SPACE + header)
         }
-        if (body != null) {
-            largeLog(id, MessageType.REQUEST_BODY, body)
-        }
+        largeLog(id, MessageType.REQUEST_BODY, body)
     }
 
     /**
@@ -124,7 +122,7 @@ object LogRecorder {
      *
      * @param id 请求的唯一标识符
      * @param requestMillis 请求时间毫秒值
-     * @param errorMessage 错误信息, 如果存在\n换行符, 仅接受最后一行
+     * @param error 错误信息, 如果存在\n换行符, 仅接受最后一行
      */
     @JvmStatic
     fun recordException(
@@ -132,12 +130,12 @@ object LogRecorder {
         requestMillis: Long,
         code: Int?,
         response: String?,
-        errorMessage: String?
+        error: String?
     ) {
         if (!enabled) return
         largeLog(id, MessageType.RESPONSE_BODY, response)
         logWithHandler(id, MessageType.RESPONSE_STATUS, code.toString(), 0)
-        logWithHandler(id, MessageType.RESPONSE_ERROR, errorMessage, 0)
+        logWithHandler(id, MessageType.RESPONSE_ERROR, error, 0)
         logWithHandler(
             id,
             MessageType.RESPONSE_TIME,
@@ -156,6 +154,7 @@ object LogRecorder {
     }
 
     private fun logWithHandler(id: String, type: MessageType, message: String?, partsCount: Int) {
+        message ?: return
         val handlerMessage = handler.obtainMessage()
         val tag = LOG_PREFIX + DELIMITER + id + DELIMITER + type.type
         val bundle = Bundle()
@@ -167,7 +166,8 @@ object LogRecorder {
     }
 
     private fun largeLog(id: String, type: MessageType, content: String?) {
-        val contentLength = content?.length ?: 0
+        content ?: return
+        val contentLength = content.length
         if (contentLength > LOG_LENGTH) {
             val parts = contentLength / LOG_LENGTH
             for (i in 0..parts) {
@@ -176,7 +176,7 @@ object LogRecorder {
                 if (end > contentLength) {
                     end = contentLength
                 }
-                logWithHandler(id, type, content?.substring(start, end), parts)
+                logWithHandler(id, type, content.substring(start, end), parts)
             }
         } else {
             logWithHandler(id, type, content, 0)