Skip to content

Spring-boot ignoring log4j2.xml file #43580

@jesmith17

Description

@jesmith17

I am attempting to implement log4j in my spring-boot (3.4.0) app.

My build is done using Gradle. My depedendencies are as such.

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.fasterxml.jackson.core:jackson-databind'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
    implementation 'org.postgresql:postgresql'
    implementation 'org.springframework.data:spring-data-mongodb:4.4.0'
    implementation 'org.springframework.data:spring-data-commons:3.4.0'
    implementation 'org.springframework.boot:spring-boot-starter-actuator:3.4.0'
    implementation 'org.mongodb:mongodb-driver-sync:5.2.1'
    implementation 'org.mongodb:mongodb-driver-core:5.2.1'
    implementation 'com.google.code.gson:gson:2.11.0'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation "me.paulschwarz:spring-dotenv:4.0.0"
    implementation 'org.apache.logging.log4j:log4j-mongodb:2.24.3'
    implementation "org.springframework.boot:spring-boot-starter-log4j2:3.4.0"
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    modules {
        module("org.springframework.boot:spring-boot-starter-logging") {
            replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
        }
    }

Im using the modules approach to replace the spring-boot-starter-logging with the spring-boot-starter-log4j2 module. I have run gradle dependencies and verified that that is happening.

|    |    |    +--- org.springframework.boot:spring-boot-autoconfigure:3.3.5
|    |    |    |    \--- org.springframework.boot:spring-boot:3.3.5 (*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-logging:3.3.5 -> org.springframework.boot:spring-boot-starter-log4j2:3.4.0
|    |    |    |    +--- org.apache.logging.log4j:log4j-slf4j2-impl:2.24.1 -> 2.23.1
|    |    |    |    |    +--- org.apache.logging.log4j:log4j-api:2.23.1
|    |    |    |    |    +--- org.slf4j:slf4j-api:2.0.9 -> 2.0.16
|    |    |    |    |    \--- org.apache.logging.log4j:log4j-core:2.23.1
|    |    |    |    |         \--- org.apache.logging.log4j:log4j-api:2.23.1
|    |    |    |    +--- org.apache.logging.log4j:log4j-core:2.24.1 -> 2.23.1 (*)
|    |    |    |    \--- org.apache.logging.log4j:log4j-jul:2.24.1 -> 2.23.1
|    |    |    |         \--- org.apache.logging.log4j:log4j-api:2.23.1

In my log4j2.xml file the config looks like this.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Property name="APP_LOG_ROOT">c:/temp</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>
        <RollingFile name="appLog"
                     fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
                     filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="1" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="com.howtodoinjava.app" additivity="false">
            <AppenderRef ref="appLog" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="appLog" />
        </Root>
    </Loggers>
</Configuration>

I would have expected spring to fail on startup with this config. Mainly because I am running this on a Mac and the APP_LOG_ROOT value of c:/temp should fail. But instead spring just continues to do it's logging as if the log4j2.xml is never there .

If I force loading of it by setting 'logger.config=classpath:log4j2.xml' in my application.properties then on start I get a bunch of warnings about how logback-classic is still in place, but nothing actually gets logged. The files in the XML config never get created, and I am still unable to control any of the logging.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions