3.39.0

(feat): 为生成的 SDK 添加日志记录支持。 用户可以通过向客户端构建器的 .logging() 方法传递 LogConfig 来配置日志记录。

1import com.example.api.AcmeApiClient;
2import com.example.api.core.LogConfig;
3import com.example.api.core.LogLevel;
4import com.example.api.core.ConsoleLogger;
5
6AcmeApiClient client = AcmeApiClient.builder()
7 .token("YOUR_TOKEN")
8 .logging(LogConfig.builder()
9 .level(LogLevel.DEBUG) // INFO 是默认值
10 .logger(new ConsoleLogger()) // ConsoleLogger 是默认值
11 .silent(false) // true 是默认值,设置为 false 以启用日志记录
12 .build())
13 .build();

LogConfig 构建器接受以下属性:

  • level(LogLevel):要使用的日志级别。默认为 LogLevel.INFO
  • logger(ILogger):要使用的日志记录器实现。默认为 ConsoleLogger
  • silent(boolean):是否静默日志记录器。默认为 true

LogLevel 枚举支持以下值:

  • DEBUG
  • INFO
  • WARN
  • ERROR

要提供自定义日志记录器,请实现带有 debuginfowarnerror 方法的 ILogger 接口。

HTTP 请求和响应详细信息(方法、URL、状态码、头部、正文)通过添加到 OkHttp 客户端的 LoggingInterceptor 记录。敏感头部(authorizationx-api-keycookiex-csrf-token 等)在日志中自动被编辑。