在Spring Boot Web应用程序中使用桥JAR从log4j 1.x迁移到log4j 2.x时无法生成日志 [英] Not able to generate log when migrate from log4j 1.x to log4j 2.x using bridge jar in spring boot web application

查看:37
本文介绍了在Spring Boot Web应用程序中使用桥JAR从log4j 1.x迁移到log4j 2.x时无法生成日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从log4j1.x迁移到log4j2.x。

Followed this link - https://logging.apache.org/log4j/2.x/manual/migration.html

但我没有看到更改后会生成日志。我想不出我错过了什么。

以下是详细信息- 现有的log4j版本-

 <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

替换为-

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

我可以看到log4j-1.2.17.jar被这四个JAR替换-

  1. log4j-jcl2.6.2.jar
  2. log4j-core-2.1.jar
  3. log4j-api-2.1.jar
  4. log4j-1.2-api-2.6.2.jar

这是现有的配置文件(文件名/usr/local/log4j.properties)-

log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/access.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
   

已替换此系统属性值-

logging.config=/usr/local/log4j.properties

使用这两行

log4j1.compatibility=true
log4j.configuration=/usr/local/log4j.properties

推荐答案

Spring Boot在非常早期的阶段重新配置您的日志框架。这就是为什么在应用程序启动期间,您的外部log4j.properties文件会被Spring替换。

如果不提供logging.config属性,将使用固定的类路径资源列表(参见list)或应用默认配置。

最近的Log4j版本上,您只需设置系统属性:

logging.config=/usr/local/log4j.properties
log4j.configurationFactory=org.apache.log4j.config.Log4j1ConfigurationFactory

但是,您不能使用最新版本,因为:

  1. 突破性的变化(参见LOG4J2-1547)中的log4j-core使2.7.0版和更高版本与Spring Boot 1.2.x、
  2. 不兼容
  3. aseries of security vulnerabilities将您的选择进一步限制为2.3.2版。

使用2.3.2版需要将log4j.properties文件转换为Log4j 2.x格式(可以使用转换器from this question):

<?xml version="1.0"?>
<Configuration name="Log4j1">
  <Appenders>
    <RollingFile name="file" fileName="/var/log/access.log"
    filePattern="/var/log/access.log.%i">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
      <Policies>
        <SizeBasedTriggeringPolicy size="5MB" />
      </Policies>
      <DefaultRolloverStrategy max="10" />
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="file" />
    </Root>
  </Loggers>
</Configuration>

并设置系统属性:

logging.config=/path/to/the/file/above.xml

备注:Spring Boot提供了一系列启动器,可以拉取正确的依赖项。要使用Log4j 2.x,只需排除标准spring-boot-starter-logging,包含spring-boot-starter-log4j2即可。不需要显式的Log4j依赖项(如果在代码中使用Log4j 1.x,则log4j-1.2-api除外):

  <properties>
    <log4j2.version>2.3.2</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>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-1.2-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
  </dependencies>

这篇关于在Spring Boot Web应用程序中使用桥JAR从log4j 1.x迁移到log4j 2.x时无法生成日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆