当前位置: 首页 > news >正文

认识并使用HttpLoggingInterceptor

目录

  • 一、前情回顾
  • 二、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。

http://www.lryc.cn/news/301053.html

相关文章:

  • 内存块与内存池
  • 【FPGA开发】HDMI通信协议解析及FPGA实现
  • [NSSRound#16 Basic]Web
  • [职场] 会计学专业学什么 #其他#知识分享#职场发展
  • docker (五)-docker存储-数据持久化
  • 飞行路线(分层图+dijstra+堆优化)(加上题目选数复习)
  • 云计算基础-快照与克隆
  • 使用 RAG 创建 LLM 应用程序
  • 第13章 网络 Page744~746 asio核心类 ip::tcp::endPoint
  • 面试浏览器框架八股文十问十答第一期
  • 多线程的基本原理学习
  • C/C++进制转换
  • 使用 Coze 搭建 TiDB 助手
  • Arduino程序简单入门
  • QT+OSG/osgEarth编译之八十三:osgdb_ogr+Qt编译(一套代码、一套框架,跨平台编译,版本:OSG-3.6.5插件库osgdb_ogr)
  • 开年炸裂-Sora/Gemini
  • vue前端系统启动报错Module not found: Error: Can‘t resolve ‘sass-loader‘
  • HTML | DOM | 网页前端 | 常见HTML标签总结
  • 乡政府|乡政府管理系统|基于Springboot的乡政府管理系统设计与实现(源码+数据库+文档)
  • 存储系统如何规避数据静默错误SDC?
  • 《Linux 简易速速上手小册》第8章: 安全性与加固(2024 最新版)
  • Ubuntu Desktop 显示文件路径
  • 【Java程序设计】【C00270】基于Springboot的moba类游戏攻略分享平台(有论文)
  • 【旧文更新】【优秀毕设】人脸识别打卡/签到/考勤管理系统(OpenCV+最简基本库开发、可移植树莓派 扩展网络图像推流控制 验证码及Excel邮件发送等功能)
  • 模型 4i(趣味、利益、互动、个性)理论
  • 解线性方程组(二)——Jacobi迭代法求解(C++)
  • 信息安全技术基础知识
  • 使用Taro开发鸿蒙原生应用——快速上手,鸿蒙应用开发指南
  • C语言指针(初阶)
  • Python循环语句——for循环的嵌套使用