目录
- 一、前情回顾
- 二、HttpLoggingInterceptor
- 1、HttpLoggingInterceptor拦截器是做什么的?
- 2、如何使用HttpLoggingInterceptor?
- 2.1 日志级别
- 2.2 如何看日志?
- 2.2.1 日志级别:BODY
- 2.2.2 日志级别:BASIC
- 2.2.3 日志级别:HEADERS
- 2.2.4 日志级别:NONE
一、前情回顾
- 在“认识并使用OkHttp3”中提到“OkHttp的拦截器也要学习下~”,这篇文章就来学习下
HttpLoggingInterceptor
这个拦截器。
二、HttpLoggingInterceptor
1、HttpLoggingInterceptor拦截器是做什么的?
- HttpLoggingInterceptor是OkHttp3提供的一个拦截器,主要用于记录HTTP请求和响应的详细信息,包括请求的URL、方法、头部信息,以及响应的状态码、响应体等内容。(下文会结合具体例子,详细说明)
2、如何使用HttpLoggingInterceptor?
- 代码示例:
// 创建日志拦截器实例并设置日志级别
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY);// 创建OkHttpClient并添加拦截器
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();
2.1 日志级别
-
HttpLoggingInterceptor允许指定日志的级别,来控制需要记录的详细程度,包括:
NONE
:不记录任何日志。BASIC
:记录请求类型、URL、响应状态码以及响应时间。HEADERS
:记录请求和响应的头部信息,以及BASIC级别的信息。BODY
:记录请求和响应的头部信息、body内容,以及BASIC级别的信息。注意,记录body内容可能会消耗资源,并且会读取body数据,这可能会影响请求的执行。
-
这其实对应的就是请求/响应的三部分。
- 请求
- 请求行
- 请求头(Header)
- 请求体 (Body)
- 响应
- 状态行
- 响应头
- 响应体
- 请求
2.2 如何看日志?
2.2.1 日志级别:BODY
- 示例
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: --> POST https://api.openai.com/v1/chat/completions
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/json; charset=utf-8
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 197
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: Authorization: Bearer xxx
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息:
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: {"messages":[{"content":"1 + 1 = ?","role":"user"}],"model":"gpt-3.5-turbo-0613","n":1,"stream":false,"temperature":1.0,"frequency_penalty":0.0,"max_tokens":2048,"presence_penalty":0.0,"top_p":1.0}
二月 17, 2024 9:04:42 下午 okhttp3.internal.platform.Platform log
信息: --> END POST (197-byte body)
二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息: <-- 200 https://api.openai.com/v1/chat/completions (1599ms)
二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息: date: Sat, 17 Feb 2024 13:04:43 GMT
二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息: content-type: application/json
......
二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息:
二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息: {"id": "chatcmpl-8tEexggs1EAHChI6OAhJAIyqCVjxg","object": "chat.completion","created": 1708175083,"model": "gpt-3.5-turbo-0613","choices": [{"index": 0,"message": {"role": "assistant","content": "1 + 1 = 2"},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 13,"completion_tokens": 7,"total_tokens": 20},"system_fingerprint": null
}二月 17, 2024 9:04:43 下午 okhttp3.internal.platform.Platform log
信息: <-- END HTTP (458-byte body)
2.2.2 日志级别:BASIC
- 示例
二月 17, 2024 9:52:49 下午 okhttp3.internal.platform.Platform log
信息: --> POST https://api.openai.com/v1/chat/completions (197-byte body)
二月 17, 2024 9:52:51 下午 okhttp3.internal.platform.Platform log
信息: <-- 200 https://api.openai.com/v1/chat/completions (1879ms, unknown-length body)
2.2.3 日志级别:HEADERS
- 示例
二月 17, 2024 9:54:37 下午 okhttp3.internal.platform.Platform log
信息: --> POST https://api.openai.com/v1/chat/completions
二月 17, 2024 9:54:37 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/json; charset=utf-8
二月 17, 2024 9:54:37 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 197
二月 17, 2024 9:54:37 下午 okhttp3.internal.platform.Platform log
信息: Authorization: Bearer xxx
二月 17, 2024 9:54:37 下午 okhttp3.internal.platform.Platform log
信息: --> END POST
二月 17, 2024 9:54:39 下午 okhttp3.internal.platform.Platform log
信息: <-- 200 https://api.openai.com/v1/chat/completions (1756ms)
二月 17, 2024 9:54:39 下午 okhttp3.internal.platform.Platform log
信息: date: Sat, 17 Feb 2024 13:54:39 GMT
二月 17, 2024 9:54:39 下午 okhttp3.internal.platform.Platform log
信息: content-type: application/json
...
二月 17, 2024 9:54:39 下午 okhttp3.internal.platform.Platform log
信息: <-- END HTTP
2.2.4 日志级别:NONE
- 没有任何的日志。那还不如别添加这个拦截器。
从调试和监控HTTP调用的角度,我会选日志级别BODY。