- 支持http request、response的数据格式化的输出。
- 当请求为Post时,支持Form表单的打印。
- 支持超长日志的打印,解决了Logcat 4K字符截断的问题。
- 支持格式化时去掉竖线边框显示日志。方便将网络请求复制到Postman之类的工具,方便将请求结果复制给服务端的同学。
- 支持日志级别
- 支持显示当前的线程名称
更完善的 Android 日志框架可以参考本人的另一个日志库 L (https://github.com/fengzhizi715/SAF-Kotlin-log) 它提供丰富的日志功能,它的 saf-log-okhttp 模块提供类似于该库的功能。
Gradle:
implementation 'com.safframework.log:saf-logginginterceptor:1.4.6'
它是用于记录OKHttp网络请求的日志的拦截器,纯Kotlin编写
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.writeTimeout(30 * 1000, TimeUnit.MILLISECONDS);
builder.readTimeout(20 * 1000, TimeUnit.MILLISECONDS);
builder.connectTimeout(15 * 1000, TimeUnit.MILLISECONDS);
LoggingInterceptor loggingInterceptor = new LoggingInterceptor.Builder()
.loggable(true) // TODO: 发布到生产环境需要改成false
.request()
.requestTag("Request")
.response()
.responseTag("Response")
//.hideVerticalLine()// 隐藏竖线边框
.build();
//设置拦截器
builder.addInterceptor(loggingInterceptor);
val builder = OkHttpClient.Builder()
builder.writeTimeout((30 * 1000).toLong(), TimeUnit.MILLISECONDS)
builder.readTimeout((20 * 1000).toLong(), TimeUnit.MILLISECONDS)
builder.connectTimeout((15 * 1000).toLong(), TimeUnit.MILLISECONDS)
val loggingInterceptor = LoggingInterceptor.Builder()
.loggable(true) // TODO: 发布到生产环境需要改成false
.request()
.requestTag("Request")
.response()
.responseTag("Response")
//.hideVerticalLine()// 隐藏竖线边框
.build()
//设置拦截器
builder.addInterceptor(loggingInterceptor)
Wechat:fengzhizi715
Java与Android技术栈:每周更新推送原创技术文章,欢迎扫描下方的公众号二维码并关注,期待与您的共同成长和进步。
Copyright (C) 2017 - present, Tony Shen.
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.