> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# API 进阶配置

### 1. 日志配置 (Logging)

LangChain4j 使用 SLF4J 作为日志接口，支持集成各种日志后端，如 Logback 或 Log4j。

#### 纯 Java 环境

要启用对每个请求和响应的日志记录，可以在创建模型实例时通过以下方式设置：

```java theme={"system"}
OpenAiChatModel.builder()
    ...
    .logRequests(true)    // 启用请求日志
    .logResponses(true)   // 启用响应日志
    .build();
```

确保在依赖项中包含 SLF4J 的日志后端，比如 Logback

#### Spring Boot 环境

在 Spring Boot 集成中，可以通过 `application.properties` 文件配置日志级别：

> **只有日志级别调整为debug级别，同时配置以上 langchain 日志输出开关才有效**

```yaml theme={"system"}
logging:
  level:
    root: debug
```

### 2. 监控 (Observability)

LangChain4j 提供了一些语言模型（如 `ChatModel` 和 `StreamingChatModel`），支持通过 `ChatModelListener` 监听以下事件：

* 请求到达 LLM
* LLM 返回的响应
* 错误处理

事件监听的例子如下：

```java theme={"system"}
ChatModelListener listener = new ChatModelListener() {

    @Override
    public void onRequest(ChatModelRequestContext requestContext) {
        ChatModelRequest request = requestContext.request();
        Map<Object, Object> attributes = requestContext.attributes();
        // 记录请求
    }

    @Override
    public void onResponse(ChatModelResponseContext responseContext) {
        ChatModelResponse response = responseContext.response();
        ChatModelRequest request = responseContext.request();
        Map<Object, Object> attributes = responseContext.attributes();
        // 记录响应
    }

    @Override
    public void onError(ChatModelErrorContext errorContext) {
        Throwable error = errorContext.error();
        ChatModelRequest request = errorContext.request();
        ChatModelResponse partialResponse = errorContext.partialResponse();
        Map<Object, Object> attributes = errorContext.attributes();
        // 记录错误
    }
};

ChatModel model = OpenAiChatModel.builder()
        ...
        .listeners(List.of(listener))  // 添加监听器
        .build();
```

### 3. 重试机制 (Retry Configuration)

LangChain4j 支持重试配置，可用于处理失败的请求。

可以通过 `maxRetries` 来配置重试逻辑，指定重试次数和延迟等参数：

```java theme={"system"}
ChatModel model = OpenAiChatModel.builder()
    ...
    .maxRetries(5)  // 设置重试策略
    .build();
```

### 4. 超时配置 (Timeout Configuration)

对于超时配置，可以通过 `timeout` 设置请求的超时时间：

```java theme={"system"}
ChatModel model = OpenAiChatModel.builder()
    ...
    .timeout(Duration.ofSeconds(10)) // 设置10秒超时
    .build();
```

<Card title="PIG AI应用开发平台 | 适合中大型企业构建自主可控的AI中台" icon="link" href="https://ai.pig4cloud.com">
  **为Java开发者提供全栈式AI工程化解决方案**，强类型/高可维护性架构，内置30+主流大模型支持。

  * **🔍 知识引擎体系**：RAG 知识引擎全自动化多模态解决方案
  * **📝 AI-OCR 中枢**：复杂非标场景高精度识别
  * **⚙️ 业务智能融合**：函数编排 + Chat2SQL，无缝对接现有业务系统
  * **🛡️ N维风控体系**：敏感词/IP/Token/User 规则控制引擎
</Card>

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/docs/04Chat 常用配置.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/docs/04-chat-common-configuration.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
