Skip to content

Commit 68b93b1

Browse files
committed
feat: add DEBUG_PRINT_PAYLOAD config option
Add a config option to control whether to print request/response payloads. Default is false (no printing), set to true for debugging/learning. Usage: DEBUG_PRINT_PAYLOAD=true # print payloads DEBUG_PRINT_PAYLOAD=false # default, silent mode
1 parent 0459cb0 commit 68b93b1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/main/java/learn/claude/code/common/AnthropicClient.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public class AnthropicClient {
6060
/** 模型 ID,如 "claude-sonnet-4-20250514",决定了使用哪个版本的 Claude */
6161
private final String model;
6262

63+
/** 是否打印请求/响应报文,用于调试,默认 false */
64+
private final boolean debugPrintPayload;
65+
6366
/**
6467
* Gson 实例,开启 PrettyPrinting 是为了调试时日志可读性更好。
6568
* 设为 static final 是因为 Gson 是线程安全的,全局共享一个实例即可。
@@ -136,13 +139,16 @@ public AnthropicClient() {
136139
this.apiKey = getConfig("ANTHROPIC_API_KEY",
137140
getConfig("ANTHROPIC_AUTH_TOKEN", ""));
138141
this.model = getConfig("MODEL_ID", "");
142+
// 读取调试开关,默认不打印报文
143+
this.debugPrintPayload = Boolean.parseBoolean(getConfig("DEBUG_PRINT_PAYLOAD", "false"));
139144

140145
// 启动时打印配置来源,方便调试 —— 生产环境中应该用日志框架替代 System.out
141146
// 注意:API Key 只打印前 8 位,这是安全最佳实践,防止密钥泄露到日志中
142147
System.out.println("[AnthropicClient] base_url = " + baseUrl);
143148
System.out.println("[AnthropicClient] model = " + model);
144149
System.out.println("[AnthropicClient] api_key = " +
145150
(apiKey.isEmpty() ? "(NOT SET!)" : apiKey.substring(0, Math.min(8, apiKey.length())) + "..."));
151+
System.out.println("[AnthropicClient] debug = " + debugPrintPayload);
146152
}
147153

148154
public String getModel() {
@@ -236,7 +242,9 @@ private String doPost(String path, String jsonBody) {
236242
}
237243

238244
// ===== 发送请求体 =====
239-
System.out.println("=====请求报文=====\n"+jsonBody.toString());
245+
if (debugPrintPayload) {
246+
System.out.println("=====请求报文=====\n" + jsonBody);
247+
}
240248
byte[] bodyBytes = jsonBody.getBytes(StandardCharsets.UTF_8);
241249
conn.setRequestProperty("Content-Length", String.valueOf(bodyBytes.length));
242250
try (OutputStream os = conn.getOutputStream()) {
@@ -263,14 +271,16 @@ private String doPost(String path, String jsonBody) {
263271
if (status < 200 || status >= 300) {
264272
throw new RuntimeException("API Error (HTTP " + status + "): " + sb.toString());
265273
}
266-
// 格式化 JSON 响应报文
274+
// 格式化 JSON 响应报文(仅在调试模式下打印)
267275
String responseStr = sb.toString();
268-
try {
269-
JsonObject responseJson = JsonParser.parseString(responseStr).getAsJsonObject();
270-
System.out.println("=====响应报文=====\n" + GSON.toJson(responseJson));
271-
} catch (Exception e) {
272-
// JSON 解析失败时直接输出原始字符串
273-
System.out.println("=====响应报文=====\n" + responseStr);
276+
if (debugPrintPayload) {
277+
try {
278+
JsonObject responseJson = JsonParser.parseString(responseStr).getAsJsonObject();
279+
System.out.println("=====响应报文=====\n" + GSON.toJson(responseJson));
280+
} catch (Exception e) {
281+
// JSON 解析失败时直接输出原始字符串
282+
System.out.println("=====响应报文=====\n" + responseStr);
283+
}
274284
}
275285
return responseStr;
276286

src/main/resources/claude.properties.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ ANTHROPIC_API_KEY=your-api-key-here
1313
# 模型 ID(必填)
1414
# 推荐: claude-sonnet-4-20250514
1515
# MODEL_ID=claude-sonnet-4-20250514
16+
17+
# 是否打印请求/响应报文(可选,默认 false)
18+
# 设为 true 可用于调试和学习 API 交互过程
19+
DEBUG_PRINT_PAYLOAD=false

0 commit comments

Comments
 (0)