Home > Mobile >  Log4j2 is not taking the whole configuration
Log4j2 is not taking the whole configuration

Time:07-19

I'm trying to log my application, but I have some problems.

First of all, the pattern I have set is not the same as the pattern the console prints.

My pattern:

<Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%d{dd-MM-yyyy HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n HH:mm:ss.SSS%d{dd-MM-yyyy}0 | [%-1p]" />
</Console>

What I see in the console:

enter image description here

Another issue is that the log files are empty, and I do not why.

This is my configuration:

Log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="trace">
    <Properties>
        <Property name="basePath">C:/temp/logs</Property>
    </Properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%d{dd-MM-yyyy HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n HH:mm:ss.SSS%d{dd-MM-yyyy}0 | [%-1p]" />
        </Console>

        <RollingFile name="testController"
                     fileName="${basePath}/testController.log"
                     filePattern="${basePath}/testController-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <!-- Max 10 files will be created everyday -->
            <DefaultRolloverStrategy max="10">
                <Delete basePath="${basePathr}" maxDepth="10">
                    <!-- Delete all files older than 30 days -->
                    <IfLastModified age="365d" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

Path

enter image description here

Pom

<?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 https://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.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.victor</groupId>
    <artifactId>test-microservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-microservice</name>
    <description>Test microservice</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- LOMBOK -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- LOG4J2 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.2</version>
        </dependency>

        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.3.7</version>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

I think that the log4j2.xml is taking by the application, because when I start it, there is not a error about the xml configuration, but I do not understand why is not taking the pattern I set and why the logs are empty.

CodePudding user response:

You should use the spring-boot-starter-log4j2 dependency, and if you need something more find gained, have a look inside that pom.

  • Related