# 9. 日志

从 Spring Framework 5.0 开始，Spring 在`spring-jcl`模块中实现了自己的 Commons Logging 桥接器。该实现检查类路径中是否存在 Log4j 2.x API 和 SLF4J 1.7 API，并使用找到的第一个作为日志实现，回退到 Java 平台的核心日志工具（也称为*JUL*或`java.util.logging`）如果 Log4j 2.x 和 SLF4J 都不可用。

将 Log4j 2.x 或 Logback（或其他 SLF4J 提供程序）放入您的类路径中，无需任何额外的桥梁，并让框架自动适应您的选择。有关详细信息，请参阅 [Spring Boot 日志记录参考文档](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging)。

Spring 的 Commons Logging 变体仅用于核心框架和扩展中的基础设施日志记录目的。对于应用程序代码中的日志记录需求，更喜欢直接使用 Log4j 2.x、SLF4J 或 JUL。

可以通过 `org.apache.commons.logging.LogFactory` 检索 `Log` 实现，如下例所示。

```java
public class MyBean {
    private final Log log = LogFactory.getLog(getClass());
    // ...
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/9.-ri-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
