使用Logger打印json样式的OKHTTP 网络请求日志

2017-11-30  本文已影响319人  河里的枇杷树

优雅的打印json风格的OKHTTP 网络请求日志.网上找了很多参考感觉都不得劲,所以自己摸索的搞了一个.下面分析给大家.

先看效果,还是不赖得嘛..


 ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 │ Thread: RxIoScheduler-6
 ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 │ RealCall.getResponseWithInterceptorChain  (RealCall.java:179)
 │    RealInterceptorChain.proceed  (RealInterceptorChain.java:67)
 │       RealInterceptorChain.proceed  (RealInterceptorChain.java:92)
 ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 │ {
 │  "requst":{
 │      "url":"http://test1....... in 91.55625 ms",
 │      "method":"POST",
 │      "headers":{
 │          "appversion":"2.5.6",
 │          "width":"1080"
 │      },
 │      "body":"type"
 │  },
 │  "response":{
 │      "code":200,
 │      "headers":{
 │          "cache-control":"no-cache",
 │          "connection":"keep-alive",
 │      },
 │      "data":{
 │          "status":101,
 │          "msg":"成功",
 │          "data":[
 │              {
 │                  "overdue":"2017-11-04"
 │              },
 │              {
 │                  "overdue":"2017-11-04"
 │              },
 │              {
 │                  "overdue":"2017-11-04"
 │              }
 │          ],
 │          "code":200
 │      }
 │  }
 │ }
 └────────────────────────────────────────────────────────────────────────────────────────────────────────────────

下面直接贴代码了,代码都很容易.

      LogInterceptor logInterceptor = new LogInterceptor();  //就是这里

              new OkHttpClient.Builder()
                    .connectTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS)
                    .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                    .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                    .addInterceptor(headerInterceptor)
                    .addInterceptor(logInterceptor)
                    .build();

LogInterceptor 既是我们打印日志的拦截器,直接暴力贴代码

    public static class LogInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            long t1 = System.nanoTime();
            Response response = chain.proceed(chain.request());
            long t2 = System.nanoTime();
            MediaType contentType = null;
            String bodyString = null;
            String bodyLog = "";
            if (response.body() != null) {
                contentType = response.body().contentType();
                bodyString = response.body().string();
                bodyLog = stringifyResponseBody(bodyString);
            }
            // 请求响应时间
            double time = (t2 - t1) / 1e6d;

            //打印log
            logJson(request, response, bodyLog, time);


            if (response.body() != null) {
                // 打印body后原ResponseBody会被清空,需要重新设置body
                ResponseBody body = ResponseBody.create(contentType, bodyString);
                return response.newBuilder().body(body).build();
            } else {
                return response;
            }
        }

        /**
         * 打印Log
         * @param request
         * @param response
         * @param bodyLog
         * @param time
         */
        private void logJson(Request request, Response response, String bodyLog, double time) {
            if (request == null || response == null) {
                return;
            }

            JSONObject json = new JSONObject(true);

            JSONObject requestJson = new JSONObject(true);
            JSONObject responseJson = new JSONObject(true);


            //request-----------------------------------------------------

            requestJson.put("url", request.url() + " in " + time + " ms");
            requestJson.put("method", request.method());


            if (request.headers() != null) {
                JSONObject requestHeaderJson = new JSONObject(true);
                Map<String, List<String>> requsetHeadersMap = request.headers().toMultimap();
                Set<String> requsetKeys = requsetHeadersMap.keySet();
                for (String key : requsetKeys) {
                    List<String> value = requsetHeadersMap.get(key);
                    requestHeaderJson.put(key, value.get(0));
                }
                requestJson.put("headers", requestHeaderJson);
            }


            requestJson.put("body", stringifyRequestBody(request));

            //response-----------------------------------------------------

            responseJson.put("code", response.code());


            if (response.headers() != null) {
                JSONObject reponseHeaderJson = new JSONObject(true);
                Map<String, List<String>> responseHeadersMap = response.headers().toMultimap();
                Set<String> responseKeys = responseHeadersMap.keySet();
                for (String key : responseKeys) {
                    List<String> value = responseHeadersMap.get(key);
                    reponseHeaderJson.put(key, value.get(0));
                }
                responseJson.put("headers", reponseHeaderJson);
            }


            responseJson.put("data", JSONObject.parse(bodyLog));

            json.put("requst", requestJson);
            json.put("response", responseJson);


            Logger.d(JsonUtil.formatJson(json.toJSONString()));
        }

        private static String stringifyRequestBody(Request request) {
            try {

                Request copy = request.newBuilder().build();
                if (copy != null) {
                    RequestBody body = copy.body();
                    if (body != null) {
                        Buffer buffer = new Buffer();
                        body.writeTo(buffer);
                        return buffer.readUtf8();
                    } else {
                        return "null";
                    }

                } else {
                    return "null";
                }

            } catch (final IOException e) {
                return "did not work";
            }
        }

        public String stringifyResponseBody(String responseBody) {

            return ToolUtils.unicode2String(responseBody);

        }

    }

需要用到的工具类

public class JsonUtil {
    /**
     * 格式化json字符串
     *
     * @param jsonStr 需要格式化的json串
     * @return 格式化后的json串
     */
    public static String formatJson(String jsonStr) {
        if (null == jsonStr || "".equals(jsonStr)) return "";
        StringBuilder sb = new StringBuilder();
        char last = '\0';
        char current = '\0';
        int indent = 0;
        for (int i = 0; i < jsonStr.length(); i++) {
            last = current;
            current = jsonStr.charAt(i);
            //遇到{ [换行,且下一行缩进
            switch (current) {
                case '{':
                case '[':
                    sb.append(current);
                    sb.append('\n');
                    indent++;
                    addIndentBlank(sb, indent);
                    break;
                //遇到} ]换行,当前行缩进
                case '}':
                case ']':
                    sb.append('\n');
                    indent--;
                    addIndentBlank(sb, indent);
                    sb.append(current);
                    break;
                //遇到,换行
                case ',':
                    sb.append(current);
                    if (last != '\\') {
                        sb.append('\n');
                        addIndentBlank(sb, indent);
                    }
                    break;
                default:
                    sb.append(current);
            }
        }
        return sb.toString();
    }

    /**
     * 添加space
     *
     * @param sb
     * @param indent
     */
    private static void addIndentBlank(StringBuilder sb, int indent) {
        for (int i = 0; i < indent; i++) {
            sb.append('\t');
        }
    }

    /**
     * http 请求数据返回 json 中中文字符为 unicode 编码转汉字转码
     *
     * @param theString
     * @return 转化后的结果.
     */
    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读