0

I am upgrading log4j1 to logj42 in one of the springBoot porject (maven, Java 1.8) with log4j-core,log4j-api as maven dependency.

Log file mentioned in log4j2.xml as Rolling file appender is not creating log file, I am able to see the logs in console but no log file. Please reply if anyone has faced same issue.

1
  • Please show your xml file. Commented Sep 21, 2020 at 5:16

2 Answers 2

1

By default spring boot contains an integration with logback logging library. So you'll have to exclude the "default" and add a special starter that will handle the integration:

In the pom.xml make sure you have the following:

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

Optional: If you want to use an asynchronous logging, add also the following dependency:

<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.3.6</version> <!-- check the actual version it might be different -->
</dependency>

After this step you can start configuring the log4j2. There are plenty of sample configurations and tutorials about this, examples: here and here.

I won't dive into possible configurations of log4j2, but from the spring boot's standpoint I'll mention that you should place the log4j2's configurations into src/main/resources/log4j2.xml

Sign up to request clarification or add additional context in comments.

Comments

0

You can try with the below log4j2.xml. This is working for me.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
    <Properties>
        <Property name="basePath">/home/user/myapp/logs</Property>
    </Properties>
    <Appenders>
        <!-- File Appender -->
        <File name="FILE" fileName="${basePath}/my-app.log" append="true">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
        </File>
        <!-- Console Appender -->
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.abc.xyz" level="debug" />
        <Root level="info">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="FILE" />
        </Root>
    </Loggers>
</Configuration>
  1. Change the log file name and path accordingly.
  2. Also change the package for the logger. <Logger name="com.abc.xyz" level="debug" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.