升级我在 springboot-starter-parent 1.5.6 中使用的 log4j1.2.17 的方法

问题描述 投票:0回答:2
我的代码中包含的

Log4j1.2.17 不是直接使用的,而是从另一个 JAR 文件调用的 –

log4j-over-slf4j1.7.25
并且这个 Jar 是从
spring-boot-starter-1.5.6.RELEASE.jar

使用的
  1. 我尝试更新下面的依赖项中的log4J版本,但看起来这个

    log4j:log4j:jar:2.16.0
    甚至不存在,我必须寻找其他方法。

    <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <!--<version>1.2.17</version>-->
         <version>2.16.0</version>
    </dependency>
    
  2. 我尝试删除上述依赖项,但它也不起作用

spring spring-boot log4j log4j2 slf4j
2个回答
0
投票

没有2.16.0版本的

log4j:log4j
神器。 Log4j 2.x 引入了突破性的架构更改和新的
org.apache.logging.log4j
组 ID。

从 Log4j 1.x 切换到 Log4j 2.x 并同时将所有日志记录 API 重定向到 Log4j API 的最简单方法是使用 Spring Boot starter。只需删除

log4j:log4j
依赖项(也作为传递依赖项)并添加:

<properties>
  <log4j2.version>2.20.0</log4j2.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
      <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <scope>runtime</scope>
  </dependency>
  ...
</dependencies>

备注:这适用于 Spring Boot 1.5.x 以上,旧版本的 Spring Boot 无法升级到最新的 Log4j 2.x 版本,如这个问题中所述。

编辑:如果您的代码中有

org.apache.log4j.Logger
,则您直接使用Log4j 1.x。

最面向未来的解决方案是按照迁移指南的选项 2 迁移到 Log4j 2.x API 或 SLF4J。甚至还有一个 OpenRewrite 配方

如果您没有时间或资源来迁移,您可以通过添加(以及上面的运行时依赖项)Log4j 1.2 到 2.x API 桥作为依赖项来推迟迁移:

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-1.2-api</artifactId>
</dependency>

您只需确保

log4j:log4j
与您的应用程序捆绑在一起,否则您会遇到意外冲突(
log4j-1.2-api
log4j:log4j
 替代品)。


0
投票
我通过将 log4j 框架替换为 reload4j 解决了上述问题,如下所示,并且我的代码通过了漏洞扫描。希望可以帮到你

<dependency> <groupId>ch.qos.reload4j</groupId> <artifactId>reload4j</artifactId> <version>1.2.25</version> </dependency>
此外,我必须从代码中排除以下两个依赖项

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
    
© www.soinside.com 2019 - 2024. All rights reserved.