Android OkHttp - 启用日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24952199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OkHttp - Enable logs
提问by Jul
I used Retrofit in order to make HTTP requests and JSON parsing and I loved the way to turn on debug logs. Logs allow to see body requests, URL... which is very useful. As Retrofit use OkHttp, I'm wondering if OkHttp also have a way to enable logs for each requests made.
我使用 Retrofit 来进行 HTTP 请求和 JSON 解析,我喜欢打开调试日志的方式。日志允许查看正文请求、URL...这非常有用。由于 Retrofit 使用 OkHttp,我想知道 OkHttp 是否也有办法为每个请求启用日志。
采纳答案by seato
The interceptors feature is currently in review, but you can build your own version of okHttp with the feature by applying the code changes in the pull request.
拦截器功能目前正在审核中,但您可以通过在拉取请求中应用代码更改来构建您自己的 okHttp 版本。
You can implement the functionality you want with something like this
你可以用这样的东西来实现你想要的功能
// Create an interceptor which catches requests and logs the info you want
RequestInterceptor logRequests= new RequestInterceptor() {
public Request execute(Request request) {
Log.i("REQUEST INFO", request.toString());
return request; // return the request unaltered
}
};
OkHttpClient client = new OkHttpClient();
List<RequestInterceptor> requestInterceptors = client.requestInterceptors();
requestInterceptros.add(logRequests);
A testis included within the pull request if you want to see more.
如果您想查看更多信息,则拉取请求中包含一个测试。
I'm going to have to warn you ahead of time about using this.There may be changes to the interceptor API since it has yet to be merged in. Don't use it with production code, but it's innocuous enough for personal testing.
我将不得不提前警告你使用它。拦截器 API 可能会有变化,因为它尚未合并。不要将它与生产代码一起使用,但它对于个人测试来说是无害的。
回答by James McCracken
Using an Interceptor
, you can define the following class:
使用Interceptor
,您可以定义以下类:
class LoggingInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.d("OkHttp", String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.d("OkHttp", String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
And add it:
并添加它:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())
.build();
回答by Jesse Wilson
None yet. But there's an interceptors featureunder development that should make it easy.
还没有。但是有一个正在开发中的拦截器功能应该可以使它变得容易。
回答by Hoang Nguyen Huu
There's an official solution from Square (employee) now. You can try: https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor
现在有来自 Square(员工)的官方解决方案。你可以试试:https: //github.com/square/okhttp/tree/master/okhttp-logging-interceptor
回答by sajad abbasi
you can enble logging and integerate with Timber to log only in debug.
您可以启用日志记录并使用 Timber 进行整数化以仅在调试时登录。
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Timber.tag("OkHttp: ");
Timber.i(message);
}
}).setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build();
回答by Ashok Varma
For better ui and debugging of OkHttp Network calls you can use libraries like GANDER
为了更好的用户界面和 OkHttp 网络调用的调试,你可以使用像GANDER这样的库
Others features include :
其他功能包括:
Apps using Gander will display a notification showing a summary of ongoing HTTP activity. Tapping on the notification launches the full Gander UI. Apps can optionally suppress the notification, and launch the Gander UI directly from within their own interface. HTTP interactions and their contents can be exported via a share intent.
Search HTTP Activity and also request and response
- The main Gander activity is launched in its own task, allowing it to be displayed alongside the host app UI using Android 7.x multi-window support.
- Gander Provides following variants
- Persistence : Saves logs to disk and TTL can be controlled
- In Memory Database : Logs will be in memory as long as the app lifecycle.
- No Op : This does nothing. So if users want Gander only in debug builds they can releaseCompile NoOp without dealing with variants, if(Build.DEBUG) ..etc
使用 Gander 的应用程序将显示一个通知,显示正在进行的 HTTP 活动的摘要。点击通知会启动完整的 Gander UI。应用程序可以选择取消通知,并直接从他们自己的界面中启动 Gander UI。HTTP 交互及其内容可以通过共享意图导出。
搜索 HTTP 活动以及请求和响应
- 主要 Gander 活动在其自己的任务中启动,允许它使用 Android 7.x 多窗口支持与主机应用程序 UI 一起显示。
- Gander 提供以下变体
- 持久性:将日志保存到磁盘,并且可以控制 TTL
- 在内存数据库中:只要应用程序生命周期,日志就会在内存中。
- 无操作:这什么都不做。因此,如果用户只在调试版本中想要 Gander,他们可以在不处理变体的情况下发布编译 NoOp,if(Build.DEBUG) ..etc
回答by xordonkey
for okhttp3
对于 okhttp3
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> Log.d(YourClass.class.getSimpleName(), "OkHttp: " + message));
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient.getHttpClient().interceptors().add(logging);