@@ -8,13 +8,18 @@ import me.hgj.jetpackmvvm.util.CharacterHandler.Companion.xmlFormat
88import me.hgj.jetpackmvvm.util.LogUtils
99import okhttp3.MediaType
1010import okhttp3.Request
11+ import java.security.MessageDigest
12+ import java.security.NoSuchAlgorithmException
1113
1214/* *
1315 * 作者 : hegaojian
1416 * 时间 : 2020/3/26
1517 * 描述 :
1618 */
17- class DefaultFormatPrinter : FormatPrinter {
19+ class DefaultFormatPrinter : FormatPrinter {
20+
21+ private var appendTag = " "
22+
1823 /* *
1924 * 打印网络请求信息, 当网络请求时 {[okhttp3.RequestBody]} 可以解析的情况
2025 *
@@ -25,6 +30,7 @@ class DefaultFormatPrinter : FormatPrinter{
2530 request : Request ,
2631 bodyString : String
2732 ) {
33+ appendTag = md5(URL_TAG + request.url())
2834 val requestBody =
2935 LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString
3036 val tag = getTag(true )
@@ -53,6 +59,7 @@ class DefaultFormatPrinter : FormatPrinter{
5359 * @param request
5460 */
5561 override fun printFileRequest (request : Request ) {
62+ appendTag = md5(URL_TAG + request.url())
5663 val tag = getTag(true )
5764 LogUtils .debugInfo(tag, REQUEST_UP_LINE )
5865 logLines(
@@ -97,6 +104,7 @@ class DefaultFormatPrinter : FormatPrinter{
97104 message : String ,
98105 responseUrl : String
99106 ) {
107+ appendTag = md5(URL_TAG + responseUrl)
100108 var bodyString = bodyString
101109 bodyString =
102110 when {
@@ -155,6 +163,7 @@ class DefaultFormatPrinter : FormatPrinter{
155163 message : String ,
156164 responseUrl : String
157165 ) {
166+ appendTag = md5(URL_TAG + responseUrl)
158167 val tag = getTag(false )
159168 val urlLine = arrayOf<String ?>(
160169 URL_TAG + responseUrl,
@@ -182,6 +191,14 @@ class DefaultFormatPrinter : FormatPrinter{
182191 LogUtils .debugInfo(tag, END_LINE )
183192 }
184193
194+ private fun getTag (isRequest : Boolean ): String {
195+ return if (isRequest) {
196+ " $TAG -Request-$appendTag "
197+ } else {
198+ " $TAG -Response-$appendTag "
199+ }
200+ }
201+
185202 companion object {
186203 private const val TAG = " HttpLog"
187204 private val LINE_SEPARATOR = System .getProperty(" line.separator" )
@@ -349,12 +366,30 @@ class DefaultFormatPrinter : FormatPrinter{
349366 return builder.toString()
350367 }
351368
352- private fun getTag (isRequest : Boolean ): String {
353- return if (isRequest) {
354- " $TAG -Request"
355- } else {
356- " $TAG -Response"
369+ /* *
370+ * md5加密
371+ */
372+ private fun md5 (string : String ): String {
373+ if (TextUtils .isEmpty(string)) {
374+ return " "
375+ }
376+ val md5: MessageDigest
377+ try {
378+ md5 = MessageDigest .getInstance(" MD5" )
379+ val bytes = md5.digest(string.toByteArray())
380+ val result = java.lang.StringBuilder ()
381+ for (b in bytes) {
382+ var temp = Integer .toHexString(b.toInt() and 0xff )
383+ if (temp.length == 1 ) {
384+ temp = " 0$temp "
385+ }
386+ result.append(temp)
387+ }
388+ return result.toString()
389+ } catch (e: NoSuchAlgorithmException ) {
390+ e.printStackTrace()
357391 }
392+ return " "
358393 }
359394 }
360395}
0 commit comments