Confused by Tomcat log4j logging in IDEA
Answered
log4j2-tomcat.xml appears to listen to my loggers for my custom code, but not for spring. Why and how to correct? Is there "something else" that needs to be configured that is overriding tomcat's logger configuration? Or could it be that IDEA caches Tomcat's config and something needs to be done to force a refresh from the file in Tomcat's folder?
Details:
- I am using Java 17, Maven, Tomcat 9, configured to use log4j2.
- Relevant Loggers in log4j-tomcat.xml are:
<Root level="info">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="MAIN"/>
</Root>
<Logger name="my.domain" level="debug" /> works fine
<Logger name="org.springframework" level="warn" /> still returns INFO
Example of an unexpected/unwanted INFO line (file is optional and does not exist intentionally):
2022-07-29 08:35:24.372 INFO 33208 --- [on(4)-127.0.0.1] o.s.c.a.ConfigurationClassParser : Properties location [classpath:application_PROD.properties] not resolvable: class path resource [application_PROD.properties] cannot be opened because it does not exist
Please sign in to leave a comment.
You may need some additional configuration for Spring, see https://stackoverflow.com/a/18188394/104891.
It doesn't looks like IntelliJ IDEA specific issue.
Although that link is ancient and no longer valid for log4j2, I think you are correct in saying that I needed some additional dependencies in the POM. I found a related post that gives some hints at https://stackoverflow.com/questions/16341020/logging-spring-using-log4j2.
The core of the solution appears to be to add dependencies for log4j-slf4j-impl and slf4j-api as follows:
Curiously though, even though the repositories contain slf4j--api 1.7.25 (the recommended one) and 1.7.36... when I tried with those version I get errors on the Sync tab as if it was failing to retrieve it, but I can even see them in the local repository location where it gets cached. I also see some strange dependency problems in the Maven tab of IntelliJ that makes me think I failed to do something basic. And the issue is definitely with slf4j-api, the dependency problems go away when I comment it out. Any hints on what I might be doing wrong would be welcome.
>I also see some strange dependency problems in the Maven tab of IntelliJ
Are there any hints showns when you hover over the error in Maven tool window or in pom.xml files?
Can you build from the command line Maven: `mvn clean compile` when using these dependencies? Can you execute Maven `compile` goal from the Maven tool window in IDE?
The dependency issue seems gone this morning. Maybe all that was needed was to close and reopen IntelliJ or maybe reboot the computer.
Spring is still not paying attention to the configuration when running in Tomcat, but since it is a Maven configuration issue and not IntelliJ I will try to figure it out on my own.