0

I am a beginner and have a problem with setting log4j2 in my Spring Boot application. I've tried to do it myself for about a week now, read many hints, posts and tutorials and still didn't find solution. Could anyone pls help me with that? I would like to create different files for different log levels like error.log, warn.log and so on.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.assessment.ww</groupId>
    <artifactId>Assessment_Exercise</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Assessment_Exercise</name>
    <description>Spring Boot Project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <File name="File" target="logs/app.log">
            <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>

    <Loggers>
<!--        Log everything -->
        <Logger name="com.assessment.ww.Assessment_Exercise" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Logger>

<!--        Log everything in Spring Boot -->
        <Logger name="org.springframework.boot" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Logger>

<!--        Log everything in Spring Core -->
        <Logger name="org.springframework.core" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Logger>

        <Root level="all">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

starting class

package com.assessment.ww.Assessment_Exercise;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AssessmentExerciseApplication {

    private static final Logger logger = LogManager.getLogger(AssessmentExerciseApplication.class);

    public static void main(String[] args) {

        SpringApplication.run(AssessmentExerciseApplication.class, args);

        logger.info("Starting SB app");
    }
}

I also have such a warning - maybe it will help: enter image description here

It prints my logger.info message (but why without formatting?) but do not create any files. enter image description here

Thank You in advance for any advices.

wojciechw
  • 71
  • 1
  • 13
  • Check this link. https://stackoverflow.com/questions/1839647/how-to-configure-log4j-to-log-different-log-levels-to-different-files-for-the-sa – Sambit Jul 22 '19 at 14:57
  • Thank You for comment but it still doesn't work. Whatever I do it does not create a file. It also does not change the pattern of log in console what is pretty wierd because I have set a pattern in Console in log4j2.xml (looks like it does not read log4j2.xml file). It prints my logger messages in the console though. – wojciechw Jul 22 '19 at 16:21
  • I pasted standard log4j2.xml data from apache log4j configuration site into different project (also simple Spring Boot + Maven - pretty similar to this one) and it works fine in the console output (logs in the console are shown with a pattern that I define in the log4j2.xml Console section). Is it possible that my problem has something to do with Maven? – wojciechw Jul 22 '19 at 16:52

1 Answers1

2

Finally, I've found a solution. The problem was Logback - even that I disabled it in spring-boot-starter-web dependency in pom.xml, it was still active in spring-boot-starter-data-jpa and spring-boot-starter-actuator. So now my pom.xml looks like this:

...
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-data-jpa</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-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
...
wojciechw
  • 71
  • 1
  • 13