建站快车是什么,怎么做网站策划,免费企业网站认证,网站更改了资料 百度什么时侯来抓取目录 一、前情回顾二、HttpLoggingInterceptor1、HttpLoggingInterceptor拦截器是做什么的#xff1f;2、如何使用HttpLoggingInterceptor#xff1f;2.1 日志级别2.2 如何看日志#xff1f;2.2.1 日志级别#xff1a;BODY2.2.2 日志级别#xff1a;BASIC2.2.3 日志级别2、如何使用HttpLoggingInterceptor2.1 日志级别2.2 如何看日志2.2.1 日志级别BODY2.2.2 日志级别BASIC2.2.3 日志级别HEADERS2.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; charsetutf-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; charsetutf-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 HTTP2.2.4 日志级别NONE
没有任何的日志。那还不如别添加这个拦截器。 从调试和监控HTTP调用的角度我会选日志级别BODY。