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

1

0

I am trying to migrate from log4j1.x to 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 -

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

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

This is the existing configuration file (filename /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
Enlargement answered 4/1, 2022 at 12:18 Comment(4)
The logging.config property suggests you use Spring Boot. Spring Boot configures logging programmatically, therefore the log4j.configuration property will probably not work. However most recent versions of Spring Boot support Log4j 2.x natively. BTW: the version of Log4j 2.x you are using has several serious security vulnerabilities.Pangenesis
Yes, I am using spring boots. Any suggestion on how to set log4j.configuration?Enlargement
Which version are you using?Pangenesis
We are using spring boot 1.2.4Enlargement
P
1

Spring Boot reconfigures your logging framework at a very early stage. That's why your external log4j.properties file is replaced by Spring during your application startup.

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.

On a recent Log4j version you just need to set the system properties:

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,
  2. a series of security vulnerabilities restricts your choice even further to version 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):

<?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. In order to use Log4j 2.x you just need to exclude the standard spring-boot-starter-logging and include spring-boot-starter-log4j2. No explicit Log4j dependencies are needed (except log4j-1.2-api if you use Log4j 1.x in your code):

  <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>
Pangenesis answered 6/1, 2022 at 7:35 Comment(3)
You are right that logging.config=/usr/local/log4j.properties needs to be used instead of log4j.configuration=/usr/local/log4j.properties. This worked. What tried is defined in the official document and unfortunately did not work.Enlargement
log4j.configuration does work for a couple of seconds between the application start and the logging reconfiguration performed by Spring. Add log4j2.debug=true to your system properties if you want to check how many times the logging system is reconfigured.Pangenesis
I did the same configuration for migrating log4j-1-x to log4j-2-x. But all the logs are generated on the console after starting spring boot application and I am using Log4j 1.x in my code. Log files are generated as defined in log4j2.xml but all files are empty.Rehearing

© 2022 - 2024 — McMap. All rights reserved.