简体   繁体   English

Not able to generate log when migrate from log4j 1.x to log4j 2.x using bridge jar in spring boot web application

[英]Not able to generate log when migrate from log4j 1.x to log4j 2.x using bridge jar in spring boot web application

I am trying to migrate from log4j1.x to log4j2.x.我正在尝试从 log4j1.x 迁移到 log4j2.x。

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

But I am not seeing logs are generated after changing it.但我没有看到更改后生成日志。 I can't figure out what I am missing.我无法弄清楚我错过了什么。

Here is the detail - Exisging log4j version -这是详细信息 - 现有 log4j 版本 -

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

This is replaced with -这被替换为 -

<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>

I can see log4j-1.2.17 .jar is replaced with these four jars-我可以看到log4j-1.2.17 .jar 被这四个罐子代替了-

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

This is the existing configuration file (filename /usr/local/log4j.properties ) -这是现有的配置文件(文件名/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
   

Replaced this system property value -替换了这个系统属性值——

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

with these two lines用这两行

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

Spring Boot reconfigures your logging framework at a very early stage. Spring Boot 在很早的阶段重新配置您的日志框架。 That's why your external log4j.properties file is replaced by Spring during your application startup.这就是为什么在应用程序启动期间您的外部log4j.properties文件被 Spring 替换的原因。

If you don't provide a logging.config property, a fixed list of classpath resources will be used (cf. list ) or a default configuration is applied.如果您不提供logging.config属性,则将使用固定的类路径资源列表(参见list )或应用默认配置。

On a recent Log4j version you just need to set the system properties:最近的 Log4j 版本上,您只需要设置系统属性:

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

However you can not use a recent version because:但是,您不能使用最新版本,因为:

  1. a breaking change (cf. LOG4J2-1547 ) in log4j-core renders version 2.7.0 and later incompatible with Spring Boot 1.2.x, log4j-core中的重大更改(参见LOG4J2-1547 )呈现版本 2.7.0 及更高版本与 Spring Boot 1.2.x 不兼容,
  2. a series of security vulnerabilities restricts your choice even further to version 2.3.2.一系列安全漏洞将您的选择进一步限制到 2.3.2 版本。

Using version 2.3.2 you need to convert your log4j.properties file into the Log4j 2.x format (you can use the converter from this question ):使用 2.3.2 版,您需要将log4j.properties文件转换为 Log4j 2.x 格式(您可以使用此问题中的转换器):

<?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>

and set the system property:并设置系统属性:

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

Remark: Spring Boot provides a series of starters, which pull the correct dependencies.备注: Spring Boot 提供了一系列启动器,可以拉取正确的依赖。 In order to use Log4j 2.x you just need to exclude the standard spring-boot-starter-logging and include spring-boot-starter-log4j2 .为了使用 Log4j 2.x,您只需要排除标准spring-boot-starter-logging并包括spring-boot-starter-log4j2 No explicit Log4j dependencies are needed (except log4j-1.2-api if you use Log4j 1.x in your code):不需要明确的 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM