I am trying to leverage log4j2 within my spring-boot app. Long term the goal is to leverage some of the DB appenders that log4j2 supports, but initially just trying to make sure it’s loading log4j instead of logback.
I have my log4j2.xml
file located in my src/main/resources
folder.
Im using gradle for my build and have the following config
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")
}
}
The modules component at the bottom seems to be correctly replacing all instances of spring-boot-start-logging with spring-boot-starter-log4j2. I was able to verify that with the output of the ./gradlew dependencies
which produced this output for every line that had spring-boot-starter-logging.
| | | +--- 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
I also verified that I don’t have any instances of slf4j implementations other than the one shown above from log4j2.
When I run the app, the log4j2.xml file is completely ignored. I know this because it references an invalid path for where to put the FileAppender output that shoudl throw an error (this was done on purpose to make testing easier).
If I try to force the loading of the file by setting
logging:
config: classpath:log4j2.xml
in my application.yml file, I just get a new block at the top of my console on execution complaining about logback-classic and then I get the SPRING ANSI art and that’s it. No other calls to the Log4j impl actually come through and no files are ever created.
I feel like there has to be something simple I am missing as all of the articles I can find seem to imply that I have done all of the correct things to make this work, but I sure can’t find the issue.
I am expecting to get an error because log4j2.xml is attempting to write to a log file located at c:/temp
which is an invalid path on my Mac.
I also attempted to try using the log4j2.yml file to see if that was working, and what I get when I do that via the
logger:
config: classpath:log4j2.yml
entry is an error message stating that only XML formats are valid here.
Josh Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1