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?

0

Thank you for the information Jacky Liu.   

Unfortunately that wasn't sufficient.   After a lot of troubleshooting this is where I stand:

  • Tomcat 10 runs perfectly and uses log4j when launched from the command line, either with "catalina.bat run" or “startup.bat”.
  • Each application runs, but now gives an error like “ERROR StatusLogger Reconfiguration failed: No configuration found for '5d9a609c' at 'null' in 'null'” when being launched.   A simple google indicates that startup is failing to find %CATALINA_HOME%\log4j2\conf\log4j2-tomcat.xml.   So… even though catalina.bat reads and finds the various settings in setenv.bat like classpath and location of the log4j2.x
  • I even added -Dlog4j.configurationFile and -Djava.util.logging.manager from setenv.bat to the intelliJ configuration and -Dlog4j.debug.   None of that made any difference.

Basically… it seems that IntelliJ is simply ignoring part or all of the configuration done in Tomcat.

0
Hi Bruno,

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.
0

I uploaded the simple REST service project zRestLog4jTest.   The upload ID is Upload id: 2025_03_12_5bpogn9g7BMtawzbfCmnGA (file: zRestLog4jTest.zip)
 

NOTES:

  • The REST service runs fine.
  • The project runs on Tomcat 10.   Tomcat 10 is configured to use log4j 2.20.0.  
  • When I run the project from IntelliJ, Tomcat finds its log4j2-tomcat.xml config file and creates the logging files and folders specified there.   Tomcat files like catalina.txt and localhost.txt use the proper pattern from the config file.
  • But… the REST service application is NOT using the server's log4j2-tomcat.xml specified when log.info(msg) is called.   It does display on the console (not on the file), but it seems to be using standard JULI logging, ignoring the configuration that tells it to use log4j.
  • Also, when the application loads onto tomcat I get the errors:  “ERROR StatusLogger Reconfiguration failed: No configuration found for '5e2de80c' at 'null' in 'null'” and “ERROR StatusLogger No configuration found for context '/zRestLog4jTest'.”

Below are screenshots of the run configuration:

For the Tomcat 10 log4j2 configuration, I have:

  • Config file in %CATALINA_HOME%\log4j2\conf\log4j2-tomcat.xml
  • JAR files for version 2.20.0 of log4j in %CATALINA_HOME%\log4j2\lib.  Includes the jars for log4j-api, log4j-appserver, log4j-core, log4j-jul, log4j-web.
  • %CATALINA_HOME%\conf\logging.properties was modified by replacing the handlers line with “handlers=org.apache.logging.log4j.jul.LogManager” and adding the line “log4j.configurationFile=file:D:/work/soft/tomcat-10.1.7-j17corretto/log4j2/conf/log4j2-tomcat.xml”.
  • %CATALINA_HOME%\bin\setenv.bat was create to contain:
  • set "CLASSPATH=%CATALINA_HOME%\log4j2\lib\*;%CATALINA_HOME%\log4j2\conf"
    set CATALINA_OPTS=%CATALINA_OPTS% -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -Dlog4j.configurationFile=file:%CATALINA_HOME%\log4j2\conf\log4j2-tomcat.xml
    

 

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.

0

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.

 

0

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.

0

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.

0

请先登录再写评论。