LoggingInterceptor, 一个OkHttp拦截器,它记录请求和响应数据

分享于 

4分钟阅读

GitHub

  繁體
An OkHttp interceptor which pretty logs request and response data.
  • 源代码名称:LoggingInterceptor
  • 源代码网址:http://www.github.com/ihsanbal/LoggingInterceptor
  • LoggingInterceptor源代码文档
  • LoggingInterceptor源代码下载
  • Git URL:
    git://www.github.com/ihsanbal/LoggingInterceptor.git
    Git Clone代码到本地:
    git clone http://www.github.com/ihsanbal/LoggingInterceptor
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/ihsanbal/LoggingInterceptor
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    LoggingInterceptor - OkHttp3的拦截器,带有漂亮的记录器

    Build StatusAndroid ArsenalAPIJAVASwaggerUI

    用法

    
    
    
    
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    
    
     client.addInterceptor(new LoggingInterceptor.Builder()
    
    
    . loggable(BuildConfig.DEBUG)
    
    
    . setLevel(Level.BASIC)
    
    
    . log(Platform.INFO)
    
    
    . request("Request")
    
    
    . response("Response")
    
    
    . addHeader("version", BuildConfig.VERSION_NAME)
    
    
    . addQueryParam("query", "0")
    
    
    //. logger(new Logger() {
    
    
    // @Override
    
    
    // public void log(int level, String tag, String msg) {
    
    
    // Log.w(tag, msg);
    
    
    // }
    
    
    // })
    
    
    //. executor(Executors.newSingleThreadExecutor())
    
    
    . build());
    
    
     OkHttpClient okHttpClient = client.build();
    
    
    
    //You can use with Retrofit
    
    
    Retrofit retrofitAdapter = new Retrofit.Builder()
    
    
    . addConverterFactory(GsonConverterFactory.create())
    
    
    . addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    
    
    . baseUrl("https://.../")
    
    
    . client(okHttpClient)
    
    
    . build();
    
    
    
    

    下载

    Gradle:

    
    
    
    
    allprojects {
    
    
     repositories {
    
    
     ...
    
    
     maven { url 'https://jitpack.io' }
    
    
     }
    
    
    }
    
    
    
    dependencies {
    
    
     compile('com.github.ihsanbal:LoggingInterceptor:2.0.5') {
    
    
     exclude group: 'org.json', module: 'json'
    
    
     }
    
    
    }
    
    
    
    

    Maven:

    
    
    
    
    <repository>
    
    
     <id>jitpack.io</id>
    
    
     <url>https://jitpack.io</url>
    
    
    </repository>
    
    
    
    <dependency>
    
    
     <groupId>com.github.ihsanbal</groupId>
    
    
     <artifactId>LoggingInterceptor</artifactId>
    
    
     <version>2.0.5</version>
    
    
    </dependency>
    
    
    
    

    执行程序

    添加执行器,以便执行顺序并发打印。

    子对象级别

    
    
    
    
    setLevel(Level.BASIC)
    
    
     .NONE // No logs
    
    
     .BASIC // Logging url,method,headers and body.
    
    
     .HEADERS // Logging headers
    
    
     .BODY // Logging body
    
    
    
    

    平台- 平台

    
    
    
    
    loggable(BuildConfig.DEBUG) // enable/disable sending logs output.
    
    
    log(Platform.WARN) // setting log type
    
    
    
    

    标记

    
    
    
    
    tag("LoggingI") // Request & response each log tag
    
    
    request("request") // Request log tag
    
    
    response("response") // Response log tag
    
    
    
    
    

    配方

    
    
    
    
    addHeader("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ") // Adding to request
    
    
    
    

    注释

    关于这里博客日志的一些提示: "。加快开发速度的方法。"

    还可以使用过滤器&的filter头来获得更好的结果


    数据  INT  PRE  log  Request  logs  
    相关文章