欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Retrofit 自定义注解 实现可选择性的打印接口日志

Retrofit 自定义注解 实现可选择性的打印接口日志

2024/10/24 9:16:36 来源:https://blog.csdn.net/qq_22706515/article/details/141031540  浏览:    关键词:Retrofit 自定义注解 实现可选择性的打印接口日志

序言

有时候我们需要打印okhttp的日志,但是现在的日志拦截器,不能做到接口级别的日志输出控制。要么就是全部打印。这样很影响调试效率。所以我在这块做了一些探索。

使用效果

普通输出

只需要在要打印日志的接口上添加 @PrintLog 注解就可以打印,没有添加的接口不会打印
在这里插入图片描述

打印出来的日志

tag通过初始化的时候指定

httpClient.addInterceptor(new LoginControlInterceptor("ok_http_log"));

在这里插入图片描述

支持自定义tag

参数定义

在这里插入图片描述

输出效果在这里插入图片描述

使用

日志打印功能基于下面的库实现,需要添加下面的库为支撑。

implementation 'com.github.fengzhizi715.okhttp-logging-interceptor:core:v1.1.4'

下面三个自定义的类

AndroidLogProxy

package com.trs.library.rx2.http.http_log;import android.text.TextUtils;
import android.util.Log;import androidx.annotation.NonNull;import cn.netdiscovery.http.interceptor.log.LogProxy;/*** <pre>* Created by zhuguohui* Date: 2024/8/8* Time: 10:34* Desc:* </pre>*/
public class AndroidLogProxy implements LogProxy {private String tempTag;@Overridepublic void d(@NonNull String s, @NonNull String s1) {Log.d(getTag(s), s1);}private String getTag(String tag){if(!TextUtils.isEmpty(tempTag)){return tempTag;}else{return tag;}}@Overridepublic void e(@NonNull String s, @NonNull String s1) {Log.e(getTag(s), s1);}@Overridepublic void w(@NonNull String s, @NonNull String s1) {Log.w(getTag(s), s1);}@Overridepublic void i(@NonNull String s, @NonNull String s1) {Log.i(getTag(s), s1);}public void setTempTag(String tempTag) {this.tempTag = tempTag;}}

LoginControlInterceptor

package com.trs.library.rx2.http.http_log;import com.trs.library.BuildConfig;import java.io.IOException;
import java.lang.annotation.Annotation;import cn.netdiscovery.http.interceptor.LoggingInterceptor;
import cn.netdiscovery.http.interceptor.log.LogManager;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Invocation;/*** <pre>* Created by zhuguohui* Date: 2024/8/8* Time: 11:33* Desc:控制是否输出日志的拦截器* </pre>*/
public class LoginControlInterceptor implements Interceptor {private final AndroidLogProxy androidLogProxy;Interceptor logInterceptor;public LoginControlInterceptor(String tag) {androidLogProxy = new AndroidLogProxy();LogManager.INSTANCE.logProxy(androidLogProxy);logInterceptor = new LoggingInterceptor.Builder().loggable(BuildConfig.DEBUG) // TODO: 发布到生产环境需要改成false.request().requestTag(tag).response().responseTag(tag).build();}@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();Invocation invocation = request.tag(Invocation.class);Annotation[] declaredAnnotations = invocation.method().getDeclaredAnnotations();boolean printLog = false;String tempTag = null;for (Annotation ano : declaredAnnotations) {if (ano.annotationType() == PrintLog.class) {printLog = true;tempTag = ((PrintLog) ano).value();}}if (printLog) {//需要打印日志if (logInterceptor != null) {androidLogProxy.setTempTag(tempTag);Response response = logInterceptor.intercept(chain);androidLogProxy.setTempTag(null);return response;}}return chain.proceed(chain.request());}
}

注解 PrintLog

package com.trs.library.rx2.http.http_log;import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.Retention;
import java.lang.annotation.Target;/*** <pre>* Created by zhuguohui* Date: 2024/8/8* Time: 16:20* Desc:用于打印日志* </pre>*/
@Target(METHOD)
@Retention(RUNTIME)
public @interface PrintLog {String value() default "";
}

使用

httpClient.addInterceptor(new LoginControlInterceptor("ok_http_log"));

难点

难点主要是怎么在拦截器中获取自定义注解,看来Retrofit的源码,它把被反射的方法保存在。tag的Invocation中。通过下面的方法获取。

        Request request = chain.request();Invocation invocation = request.tag(Invocation.class);Annotation[] declaredAnnotations = invocation.method().getDeclaredAnnotations();

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com