package com.lukouguoji.module_base.impl

import com.lukouguoji.module_base.ktx.logi
import com.lukouguoji.module_base.ktx.logw
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONArray
import org.json.JSONObject

/**
 * @author ：Martin
 * @data : 2018/6/21 16:41
 * @description : 网络请求Logger实例
 */
class HttpLogImpl : HttpLoggingInterceptor.Logger {

    override fun log(message: String) {
        //非Debug情况下，不打印日志
        try {
            //如果是Json数据，则以Json格式打印，便于查看
            when {
                message.startsWith("{") -> logw(JSONObject(message).toString(2))
                message.startsWith("[") -> logw(JSONArray(message).toString(2))
                else -> logw(message)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            logw(message)
        }
    }

}