Log4j 最新版本带有 slf4j-log4j12

Sub*_*odh 3 java logging log4j slf4j log4j2

使用slf4j-log4j12版本 2.0.0-alpha5 依赖项进行日志记录,从而拉取 Apache Log4j \xc2\xbb 1.2.17。需要将 Log4j 升级到最新版本,即 log4j-core \xc2\xbb 2.17.1,这样我就不必在代码库中进行更改。

\n

以下是 POM 片段和示例代码:

\n
  <dependencies>\n    <dependency>\n        <groupId>org.slf4j</groupId>\n        <artifactId>slf4j-log4j12</artifactId>\n        <version>2.0.0-alpha5</version>\n    </dependency>\n   </dependencies>\n
Run Code Online (Sandbox Code Playgroud)\n

示例代码:

\n
import org.apache.log4j.Logger;\npublic class Entry {\n    final static Logger logger = Logger.getLogger(Entry.class);\n    public static void main(String[] args) {\n        logger.info("Through Logging");\n        System.out.println("Logging tests");\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

log4j2有sl4j依赖吗?

\n

Pio*_*asz 5

slf4j-log4j12是从 SLF4J 到 Log4j 1.2 的桥梁(绑定):org.slf4j.Logger代码中提交到 a 的所有消息都将发送到org.apache.log4j.Logger同名的 a。这不是正确的方向。

幸运的是,现在所有主要日志系统之间都有桥接器/绑定/适配器。从您的问题中尚不清楚您是否需要 SLF4J,因此:

  • 如果您不使用 SLF4J,则可以使用log4j-1.2-apiLog4j 1.x 和 Log4j 2.x API 之间的直接桥梁。它是Log4j 1.x 的替代品,因此您需要删除该log4j工件并添加:

    <!-- From Log4j 1.x to Log4j 2.x API -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.19.0</version>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果你想通过SLF4J,你可以使用log4j-over-slf4j( Log4j 1.x的替代品,将Log4j 1.x连接到SLF4J)和log4j-slf4j18-impl(从SLF4J到Log4j 2.x API的桥梁):

    <!-- From Log4j 1.x to SLF4J -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>2.0.5</version>
    </dependency>
    <!-- From SLF4J 2.0+ to Log4j 2.x API -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j2-impl</artifactId>
        <version>2.19.0</version>
        <scope>runtime</scope>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)

当然,您还需要 Log4j 2.x API 的实现,例如 Log4j 2.x Core:

<!-- From Log4j 2.x API to Log4j 2.x Core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.19.0</version>
    <scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)