1

I am trying to setup a project with spring boot. Although I have defined all the dependencies for log4j, and added the log4j2.xml configuration file, the logs printed doesn't have the pattern defined in xml file. I checked the external libraries pulled by maven, and I see logback dependencies, which are not in my pom, I have even added exclusions. I am using spring boot 3 with Java 17. I have also tried excluding spring-boot-starter-logging. Nothing seems to work.

Here is the pom.xml file

<?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>3.0.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.geek8080</groupId>
<artifactId>db_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>db_service</name>
<description>DB Service for web app</description>
<properties>
    <java.version>17</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>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

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

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </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>

Here is the log4j2.xml file, I have placed this in src/main/resources,

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Properties>
    <Property name="logpath-location">/app/logs</Property>
    <Property name="logfile-name">db_service.log</Property>
    <Property name="archive">${logpath-location}/archive/dbservice</Property>
    <Property name="interval">10</Property>
</Properties>

<Appenders>
    <Console name="Console">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %C.%M():%L %X - %m%n"/>
    </Console>

    <RollingFile name="RollingFileAppender" fileName="${logpath-location}/${logfile-name}"
                 filePattern="${archive}/${logfile-name}.%d{yyyy-MM-dd-HH}.gz">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c.%M ():%L %X - %m%n"/>
        <Policies>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <Logger name="com.indiantraditionalsnacks.db_service" level="DEBUG" additivity="false">
        <AppenderRef ref="Console" level="INFO"/>
        <AppenderRef ref="RollingFileAppender" level="DEBUG"/>
    </Logger>

    <Root level="INFO" includeLocation="true">
        <AppenderRef ref="Console" level="INFO"/>
        <AppenderRef ref="RollingFileAppender" level="DEBUG"/>
    </Root>
</Loggers>
1

1 Answer 1

1

I have noticed that your "pom.xml" doesn't include org.springframework.boot:spring-boot-starter dependency, which is the core Spring Boot Starter. Try adding it and add exclusion to "org.springframework.boot:spring-boot-starter-logging" from that dependency

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

1 Comment

That was not the issue here, I have added the spring-boot-starter-web, which already has spring-boot-starter as a dependency. I don't know how this was fixed, I deleted the local maven repository and tried building the project again, it worked fine.

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.