Logging

自 Spring Framework 5.0 以来,Spring 自带了在 spring-jcl 模块中实现的 Commons Logging 桥接器。该实现检查 classpath 中是否存在 Log4j 2.x API 和 SLF4J 1.7 API,并使用找到的第一个作为日志实现,如果既没有 Log4j 2.x,也没有 SLF4J 可用,则回退到 Java 平台的核心日志记录设施(也称为 JULjava.util.logging)。

Since Spring Framework 5.0, Spring comes with its own Commons Logging bridge implemented in the spring-jcl module. The implementation checks for the presence of the Log4j 2.x API and the SLF4J 1.7 API in the classpath and uses the first one of those found as the logging implementation, falling back to the Java platform’s core logging facilities (also known as JUL or java.util.logging) if neither Log4j 2.x nor SLF4J is available.

在类路径中放入 Log4j 2.x 或 Logback(或另一个 SLF4J 提供程序),无需任何额外的桥接,让框架自动适应您的选择。有关更多信息,请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging[SpringBoot Logging Reference Documentation].

Put Log4j 2.x or Logback (or another SLF4J provider) in your classpath, without any extra bridges, and let the framework auto-adapt to your choice. For further information see the Spring Boot Logging Reference Documentation.

Spring 的 Commons Logging 变体仅旨在用于核心框架和扩展中的基础设施日志记录目的。

Spring’s Commons Logging variant is only meant to be used for infrastructure logging purposes in the core framework and in extensions.

对于应用程序代码中的日志记录需求,优先直接使用 Log4j 2.x、SLF4J 或 JUL。

For logging needs within application code, prefer direct use of Log4j 2.x, SLF4J, or JUL.

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

A Log implementation may be retrieved via org.apache.commons.logging.LogFactory as in the following example.

  • Java

  • Kotlin

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