提交 e9b76866 编写于 作者: J jessyan

Improve FormatPrinter and RequestInterceptor

上级 6042eba9
/** /*
* Copyright 2017 JessYan * Copyright 2017 JessYan
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package me.jessyan.mvparms.demo.app; package me.jessyan.mvparms.demo.app;
import android.app.Application; import android.app.Application;
...@@ -54,7 +54,7 @@ public final class GlobalConfiguration implements ConfigModule { ...@@ -54,7 +54,7 @@ public final class GlobalConfiguration implements ConfigModule {
@Override @Override
public void applyOptions(Context context, GlobalConfigModule.Builder builder) { public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
if (!BuildConfig.LOG_DEBUG){ //Release 时,让框架不再打印 Http 请求和响应的信息 if (!BuildConfig.LOG_DEBUG) { //Release 时,让框架不再打印 Http 请求和响应的信息
builder.printHttpLogLevel(RequestInterceptor.Level.NONE); builder.printHttpLogLevel(RequestInterceptor.Level.NONE);
} }
......
...@@ -23,6 +23,8 @@ import android.text.TextUtils; ...@@ -23,6 +23,8 @@ import android.text.TextUtils;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.jess.arms.http.BaseUrl; import com.jess.arms.http.BaseUrl;
import com.jess.arms.http.GlobalHttpHandler; import com.jess.arms.http.GlobalHttpHandler;
import com.jess.arms.http.log.DefaultFormatPrinter;
import com.jess.arms.http.log.FormatPrinter;
import com.jess.arms.http.log.RequestInterceptor; import com.jess.arms.http.log.RequestInterceptor;
import com.jess.arms.http.imageloader.BaseImageLoaderStrategy; import com.jess.arms.http.imageloader.BaseImageLoaderStrategy;
import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy; import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy;
...@@ -67,6 +69,7 @@ public class GlobalConfigModule { ...@@ -67,6 +69,7 @@ public class GlobalConfigModule {
private ClientModule.RxCacheConfiguration mRxCacheConfiguration; private ClientModule.RxCacheConfiguration mRxCacheConfiguration;
private AppModule.GsonConfiguration mGsonConfiguration; private AppModule.GsonConfiguration mGsonConfiguration;
private RequestInterceptor.Level mPrintHttpLogLevel; private RequestInterceptor.Level mPrintHttpLogLevel;
private FormatPrinter mFormatPrinter;
private Cache.Factory mCacheFactory; private Cache.Factory mCacheFactory;
private GlobalConfigModule(Builder builder) { private GlobalConfigModule(Builder builder) {
...@@ -82,6 +85,7 @@ public class GlobalConfigModule { ...@@ -82,6 +85,7 @@ public class GlobalConfigModule {
this.mRxCacheConfiguration = builder.rxCacheConfiguration; this.mRxCacheConfiguration = builder.rxCacheConfiguration;
this.mGsonConfiguration = builder.gsonConfiguration; this.mGsonConfiguration = builder.gsonConfiguration;
this.mPrintHttpLogLevel = builder.printHttpLogLevel; this.mPrintHttpLogLevel = builder.printHttpLogLevel;
this.mFormatPrinter = builder.formatPrinter;
this.mCacheFactory = builder.cacheFactory; this.mCacheFactory = builder.cacheFactory;
} }
...@@ -198,6 +202,12 @@ public class GlobalConfigModule { ...@@ -198,6 +202,12 @@ public class GlobalConfigModule {
return mPrintHttpLogLevel; return mPrintHttpLogLevel;
} }
@Singleton
@Provides
FormatPrinter provideFormatPrinter(){
return mFormatPrinter == null ? new DefaultFormatPrinter() : mFormatPrinter;
}
@Singleton @Singleton
@Provides @Provides
Cache.Factory provideCacheFactory(Application application) { Cache.Factory provideCacheFactory(Application application) {
...@@ -226,6 +236,7 @@ public class GlobalConfigModule { ...@@ -226,6 +236,7 @@ public class GlobalConfigModule {
private ClientModule.RxCacheConfiguration rxCacheConfiguration; private ClientModule.RxCacheConfiguration rxCacheConfiguration;
private AppModule.GsonConfiguration gsonConfiguration; private AppModule.GsonConfiguration gsonConfiguration;
private RequestInterceptor.Level printHttpLogLevel; private RequestInterceptor.Level printHttpLogLevel;
private FormatPrinter formatPrinter;
private Cache.Factory cacheFactory; private Cache.Factory cacheFactory;
private Builder() { private Builder() {
...@@ -303,6 +314,13 @@ public class GlobalConfigModule { ...@@ -303,6 +314,13 @@ public class GlobalConfigModule {
return this; return this;
} }
public Builder formatPrinter(FormatPrinter formatPrinter){
if (formatPrinter == null)
throw new NullPointerException("FormatPrinter can not be null");
this.formatPrinter = formatPrinter;
return this;
}
public Builder cacheFactory(Cache.Factory cacheFactory) { public Builder cacheFactory(Cache.Factory cacheFactory) {
this.cacheFactory = cacheFactory; this.cacheFactory = cacheFactory;
return this; return this;
......
...@@ -18,22 +18,28 @@ package com.jess.arms.http.log; ...@@ -18,22 +18,28 @@ package com.jess.arms.http.log;
import android.text.TextUtils; import android.text.TextUtils;
import com.jess.arms.di.module.GlobalConfigModule;
import com.jess.arms.utils.CharacterHandler;
import java.util.List; import java.util.List;
import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
import timber.log.Timber; import timber.log.Timber;
/** /**
* ================================================ * ================================================
* 对 OkHttp 的请求和响应信息进行更规范和清晰的打印 * 对 OkHttp 的请求和响应信息进行更规范和清晰的打印, 此类为框架默认实现, 以默认格式打印信息, 若觉得默认打印格式
* <p> * 并不能满足自己的需求, 可自行扩展自己理想的打印格式
*
* @see GlobalConfigModule.Builder#formatPrinter(FormatPrinter)
* Created by JessYan on 25/01/2018 14:51 * Created by JessYan on 25/01/2018 14:51
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a> * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a> * <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================ * ================================================
*/ */
public class DefaultFormatPrinter { public class DefaultFormatPrinter implements FormatPrinter {
private static final String TAG = "ArmsHttpLog"; private static final String TAG = "ArmsHttpLog";
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String DOUBLE_SEPARATOR = LINE_SEPARATOR + LINE_SEPARATOR; private static final String DOUBLE_SEPARATOR = LINE_SEPARATOR + LINE_SEPARATOR;
...@@ -57,9 +63,6 @@ public class DefaultFormatPrinter { ...@@ -57,9 +63,6 @@ public class DefaultFormatPrinter {
private static final String CENTER_LINE = "├ "; private static final String CENTER_LINE = "├ ";
private static final String DEFAULT_LINE = "│ "; private static final String DEFAULT_LINE = "│ ";
private DefaultFormatPrinter() {
throw new UnsupportedOperationException("you can't instantiate me!");
}
private static boolean isEmpty(String line) { private static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || N.equals(line) || T.equals(line) || TextUtils.isEmpty(line.trim()); return TextUtils.isEmpty(line) || N.equals(line) || T.equals(line) || TextUtils.isEmpty(line.trim());
...@@ -71,7 +74,8 @@ public class DefaultFormatPrinter { ...@@ -71,7 +74,8 @@ public class DefaultFormatPrinter {
* @param request * @param request
* @param bodyString * @param bodyString
*/ */
static void printJsonRequest(Request request, String bodyString) { @Override
public void printJsonRequest(Request request, String bodyString) {
final String requestBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString; final String requestBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString;
final String tag = getTag(true); final String tag = getTag(true);
...@@ -87,7 +91,8 @@ public class DefaultFormatPrinter { ...@@ -87,7 +91,8 @@ public class DefaultFormatPrinter {
* *
* @param request * @param request
*/ */
static void printFileRequest(Request request) { @Override
public void printFileRequest(Request request) {
final String tag = getTag(true); final String tag = getTag(true);
Timber.tag(tag).i(REQUEST_UP_LINE); Timber.tag(tag).i(REQUEST_UP_LINE);
...@@ -100,17 +105,21 @@ public class DefaultFormatPrinter { ...@@ -100,17 +105,21 @@ public class DefaultFormatPrinter {
/** /**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况 * 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况
* *
* @param chainMs * @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful * @param isSuccessful 请求是否成功
* @param code * @param code 响应码
* @param headers * @param headers 请求头
* @param bodyString * @param contentType 服务器返回数据的数据类型
* @param segments * @param bodyString 服务器返回的数据(已解析)
* @param message * @param segments 域名后面的资源地址
* @param responseUrl * @param message 响应信息
* @param responseUrl 请求地址
*/ */
static void printJsonResponse(long chainMs, boolean isSuccessful, @Override
int code, String headers, String bodyString, List<String> segments, String message, final String responseUrl) { public void printJsonResponse(long chainMs, boolean isSuccessful, int code, String headers, MediaType contentType,
String bodyString, List<String> segments, String message, final String responseUrl) {
bodyString = RequestInterceptor.isJson(contentType) ? CharacterHandler.jsonFormat(bodyString)
: RequestInterceptor.isXml(contentType) ? CharacterHandler.xmlFormat(bodyString) : bodyString;
final String responseBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString; final String responseBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString;
final String tag = getTag(false); final String tag = getTag(false);
...@@ -126,16 +135,17 @@ public class DefaultFormatPrinter { ...@@ -126,16 +135,17 @@ public class DefaultFormatPrinter {
/** /**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 为 {@code null} 或不可解析的情况 * 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 为 {@code null} 或不可解析的情况
* *
* @param chainMs * @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful * @param isSuccessful 请求是否成功
* @param code * @param code 响应码
* @param headers * @param headers 请求头
* @param segments * @param segments 域名后面的资源地址
* @param message * @param message 响应信息
* @param responseUrl * @param responseUrl 请求地址
*/ */
static void printFileResponse(long chainMs, boolean isSuccessful, @Override
int code, String headers, List<String> segments, String message, final String responseUrl) { public void printFileResponse(long chainMs, boolean isSuccessful, int code, String headers,
List<String> segments, String message, final String responseUrl) {
final String tag = getTag(false); final String tag = getTag(false);
final String[] urlLine = {URL_TAG + responseUrl, N}; final String[] urlLine = {URL_TAG + responseUrl, N};
......
/*
* Copyright 2018 JessYan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jess.arms.http.log;
import com.jess.arms.di.module.GlobalConfigModule;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.Request;
/**
* ================================================
* 对 OkHttp 的请求和响应信息进行更规范和清晰的打印, 开发者可更根据自己的需求自行扩展打印格式
*
* @see DefaultFormatPrinter
* @see GlobalConfigModule.Builder#formatPrinter(FormatPrinter)
* Created by JessYan on 31/01/2018 17:36
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public interface FormatPrinter {
/**
* 打印网络请求信息, 当网络请求时 {{@link okhttp3.RequestBody}} 可以解析的情况
*
* @param request
* @param bodyString 发送给服务器的请求体中的数据(已解析)
*/
void printJsonRequest(Request request, String bodyString);
/**
* 打印网络请求信息, 当网络请求时 {{@link okhttp3.RequestBody}} 为 {@code null} 或不可解析的情况
*
* @param request
*/
void printFileRequest(Request request);
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况
*
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param contentType 服务器返回数据的数据类型
* @param bodyString 服务器返回的数据(已解析)
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
void printJsonResponse(long chainMs, boolean isSuccessful, int code, String headers, MediaType contentType,
String bodyString, List<String> segments, String message, String responseUrl);
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 为 {@code null} 或不可解析的情况
*
* @param chainMs 服务器响应耗时(单位毫秒)
* @param isSuccessful 请求是否成功
* @param code 响应码
* @param headers 请求头
* @param segments 域名后面的资源地址
* @param message 响应信息
* @param responseUrl 请求地址
*/
void printFileResponse(long chainMs, boolean isSuccessful, int code, String headers,
List<String> segments, String message, String responseUrl);
}
...@@ -56,6 +56,7 @@ import timber.log.Timber; ...@@ -56,6 +56,7 @@ import timber.log.Timber;
@Singleton @Singleton
public class RequestInterceptor implements Interceptor { public class RequestInterceptor implements Interceptor {
private GlobalHttpHandler mHandler; private GlobalHttpHandler mHandler;
private final FormatPrinter mPrinter;
private final Level printLevel; private final Level printLevel;
public enum Level { public enum Level {
...@@ -66,12 +67,13 @@ public class RequestInterceptor implements Interceptor { ...@@ -66,12 +67,13 @@ public class RequestInterceptor implements Interceptor {
} }
@Inject @Inject
public RequestInterceptor(@Nullable GlobalHttpHandler handler, @Nullable Level level) { public RequestInterceptor(@Nullable GlobalHttpHandler handler, @Nullable Level level, FormatPrinter printer) {
this.mHandler = handler; this.mHandler = handler;
this.mPrinter = printer;
if (level == null) if (level == null)
printLevel = Level.ALL; this.printLevel = Level.ALL;
else else
printLevel = level; this.printLevel = level;
} }
@Override @Override
...@@ -83,9 +85,9 @@ public class RequestInterceptor implements Interceptor { ...@@ -83,9 +85,9 @@ public class RequestInterceptor implements Interceptor {
if (logRequest) { if (logRequest) {
//打印请求信息 //打印请求信息
if (request.body() != null && isParseable(request.body().contentType())) { if (request.body() != null && isParseable(request.body().contentType())) {
DefaultFormatPrinter.printJsonRequest(request, parseParams(request)); mPrinter.printJsonRequest(request, parseParams(request));
} else { } else {
DefaultFormatPrinter.printFileRequest(request); mPrinter.printFileRequest(request);
} }
} }
...@@ -118,13 +120,10 @@ public class RequestInterceptor implements Interceptor { ...@@ -118,13 +120,10 @@ public class RequestInterceptor implements Interceptor {
final String url = originalResponse.request().url().toString(); final String url = originalResponse.request().url().toString();
if (responseBody != null && isParseable(responseBody.contentType())) { if (responseBody != null && isParseable(responseBody.contentType())) {
DefaultFormatPrinter.printJsonResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1), mPrinter.printJsonResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1), isSuccessful,
isSuccessful, code, header, code, header, responseBody.contentType(), bodyString, segmentList, message, url);
isJson(responseBody.contentType()) ?
CharacterHandler.jsonFormat(bodyString) : isXml(responseBody.contentType()) ?
CharacterHandler.xmlFormat(bodyString) : bodyString, segmentList, message, url);
} else { } else {
DefaultFormatPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1), mPrinter.printFileResponse(TimeUnit.NANOSECONDS.toMillis(t2 - t1),
isSuccessful, code, header, segmentList, message, url); isSuccessful, code, header, segmentList, message, url);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册