IntelliJ ignoring Tomcat 10 log4j2 configuration, probably due to configuration caching - how to fix?
I have been trying to get the log4j2 configuration to work on IntelliJ IDEA. Sadly, it is not working and it appears to be caused by caching of the Tomcat configuration in the …\system\tomcat\… folder instead of getting the configuration from the Tomcat installation.
This is what I see during application launch. Notice the CATALINA_BASE value pointing to the intelliJ tomcat folder, a folder that does not contain log4j2-tomcat.xml nor the log4j2 libraries even though they are specified in Tomcat's setenv.bat and get displayed during this startup:
Using CATALINA_BASE: "C:\data\intellij_customDirs\system\tomcat\23ec3ebf-94c7-4f27-b4c6-77a394a9b26b"
Using CATALINA_HOME: "D:\work\soft\tomcat-10.1.7-j17corretto"
Using CATALINA_TMPDIR: "D:\work\soft\tomcat-10.1.7-j17corretto\temp"
Using JRE_HOME: "D:\work\soft\jdk17.0.1_12corretto"
Using CLASSPATH: "D:\work\soft\tomcat-10.1.7-j17corretto\log4j2\lib\log4j-api-2.20.0.jar;D:\work\soft\tomcat-10.1.7-j17corretto\log4j\lib\log4j2-core-2.20.0.jar;D:\work\soft\tomcat-10.1.7-j17corretto\log4j\lib\log4j2-jul-2.20.0.jar;D:\work\soft\tomcat-10.1.7-j17corretto\log4j2\conf;D:\work\soft\tomcat-10.1.7-j17corretto\bin\bootstrap.jar;D:\work\soft\tomcat-10.1.7-j17corretto\bin\tomcat-juli.jar"
Using CATALINA_OPTS: ""
Connected to the target VM, address: '127.0.0.1:65407', transport: 'socket'
Could not load Logmanager "org.apache.logging.log4j.jul.LogManager"
And this is the content of setenv.bat:
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
set "JAVA_OPTS=%JAVA_OPTS% -Dlog4j.configurationFile=${CATALINA_HOME}\log4j2\conf\log4j2-tomcat.xml"
set "CLASSPATH=%CATALINA_HOME%\log4j2\lib\log4j-api-2.20.0.jar;%CATALINA_HOME%\log4j\lib\log4j2-core-2.20.0.jar;%CATALINA_HOME%\log4j\lib\log4j2-jul-2.20.0.jar;%CATALINA_HOME%\log4j2\conf"
I already verified that the LogManager class is present in log4j2-jul-2.20.0.jar pointed to in the configuration and classpath.
QUESTIONS:
1) Do I need to do something other than Invalidating the caches or even Repairing the IDE to make the Tomcat configuration get refreshed inside of IDEA?
2) Is there anything else I should be doing so that Tomcat reads from the classpath when launched inside of IDEA?
请先登录再写评论。
Hello,
Can you refer to this issue link to see if it helps?
https://youtrack.jetbrains.com/issue/DOC-19891/Add-detailed-explain-for-Deploy-applications-configured-in-Tomcat-instance-at-RunDebugConfigurationTomcatServer.html
Also, you can change the startup script to your own:
Thank you for the information Jacky Liu.
Unfortunately that wasn't sufficient. After a lot of troubleshooting this is where I stand:
Basically… it seems that IntelliJ is simply ignoring part or all of the configuration done in Tomcat.
That sounds like a bug. Can you share a simple log4j app, and I'll create an issue report for you? Please upload it to https://uploads.jetbrains.com/ and paste the upload ID here. The uploaded file is only visible to JetBrains employees.
I uploaded the simple REST service project zRestLog4jTest. The upload ID is Upload id: 2025_03_12_5bpogn9g7BMtawzbfCmnGA (file: zRestLog4jTest.zip)
NOTES:
Below are screenshots of the run configuration:
For the Tomcat 10 log4j2 configuration, I have:
The above setup may still be failing to make webapps log properly using the Tomcat log4j configuration file, but it is a compilation of disjointed instructions that I could find and it is the one that I was able to assemble that comes closest to a functioning setup.
Just in case it helps with testing, I uploaded my log4j2-tomcat.xml as Upload id: 2025_03_12_HU2gDvGM5EM6wpDCQxPkTY (file: log4j2-tomcat.xml). It is a bit complex, but easy to follow.
I hope you can find a way to tweak this to make it work.
SOLUTION:
The issue was in how to specify -Dlog4j.configurationFile when in a Windows filesystem without confusing Java.
In my case it needed to be:
-Dlog4j.configurationFile=file:/D:/work/soft/tomcat-10.1.7-j17corretto/log4j2/conf/log4j2-tomcat.xml
Once I set it like that in both setenv.bat and the IntelliJ run configuration it worked.
It would be ideal to use %CATALINA_HOME% as part of that, but I am not sure if it can be done since that is a Windows environment variable using the format: D:\work\soft\tomcat-10.1.7-j17corretto.
Next I will attempt to remove the extra run configuration portions to see if it will inherit from Tomcat without it.
---
EDIT: It was also necessary to add a Log4jContextListener class to the webapp so that it would use the Tomcat log4j2-tomcat.xml as its logging configuration.
In any case… problem solved.
Bruno Genovese Thanks for the update. It helps! It sounds more like a run configuration issue where the IDE doesn't pick system environments from the Tomcat script file/OS. Do you still need an issue report for this? If yes, I'll create one.
No need to create an issue.
All said and done… it really is a documentation problem, but it isn't in just one place. It really has aspects of IntelliJ, Tomcat and log4j.
Ultimately I'd say it was caused by the Apache team. In old versions it used to be trivial to configure Tomcat for log4j: Add the libraries, add a log4j2.xml and all would work. Now there are so many variants and so many different “solutions” to be found when googling or using chatGPT that finding the one that works for your versions combination of IntelliJ, log4j and Tomcat is quite difficult.
TBH, if I were to start over I would just stick to the default JULI logging and forget about log4j. The only reason to keep using log4j is the massive amount of old code that relies on it.