0

I am using Log4j2 for logging with Spring Boot , but it is not creating the log file. Given below is my configuration for Log4j2 and dependencies i added.

Log4j2 configuration -

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>
        
        <!-- File Appender -->
        <File name="File" fileName=".logs/app.log">
          <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
 
    <Loggers>
        <!-- LOG everything at INFO level -->
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="File" />
        </Root>
 
        <!-- LOG "com.baeldung*" at TRACE level -->
        <Logger name="com.ams" level="trace">
            <AppenderRef ref="File" />
        </Logger>
    </Loggers>
 
</Configuration>

Pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- Add Log4j2 Dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

Is there any other configuration or dependency i need to add ? Because according to the blogs on web log4j2 should create the log file with the configuration provided above.

1
  • Did you managed to find the solution for your problem? if yes what was the solution / cause? Commented Apr 5, 2024 at 7:23

2 Answers 2

4

Maybe this link will help you. In my case i had to exclude the spring boot startet logging in the pom xml because otherwise the default logging logback is active and log4j won't be used.

    <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>
Sign up to request clarification or add additional context in comments.

Comments

-1

Probably, this is late, but still would like to answer the question anyway. I added the spring-boot-starter-log4j2 dependency to the pom and excluded the spring-boot-starter-logging dependency. Also, credit goes to @medTech for his answer as it made me try a different variation of his solution.
Please note I am using spring boot 2.2.6.RELEASE, this is not tested with 1.x version.

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

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.