Re: Struggling to get trace/debug logging from Tomcat components
чт, 13 мар. 2025 г. в 16:42, Christopher Schultz : > > All, > > I really should be able to figure this out for myself but I'm frustrated > and might just need someone to show me that I'm spelling something wrong. > > I'm trying to get all logs from the CsrfPReventionFilter. I know it's > working because I get 403 responses when it's enabled and don't get them > when it's not. But it's 100% silent and I'm getting no logs. > > I've added this to the bottom of the stock conf/logging.properties file: > > org.apache.catalina.filters.CsrfPreventionFilter.level = ALL > org.apache.catalina.filters.CsrfPreventionFilter.handlers = > java.util.logging.ConsoleHandler > > I shouldn't need the second line, since the default .handlers is > ConsoleHandler and the async handler for catalina.[date]. > > I'm not seeing anything more detailed than INFO in catalina.out or > catalina.[date]. But... > > 1catalina.org.apache.juli.AsyncFileHandler.level = ALL > ... > java.util.logging.ConsoleHandler.level = ALL > > > ??! > > This is Tomcat 9.0.98 on Linux with a stock conf/logging.properties file > (plus my .level=ALL for this filter) > > What am I missing? 1. It may be that your web application has its own logging.properties file in classpath, thus it is preferred over the default one. One option to prevent such behaviour is to reconfigure Tomcat to use the java.util.logging.LogManager from JRE instead of its own class. 2. > org.apache.catalina.filters.CsrfPreventionFilter.handlers = > java.util.logging.ConsoleHandler If those are two separate lines, the first one lacks trailing slash, and results in an empty value for "handlers". 3. If logging does not help, consider remote debugging. 4. BTW, looking at 9.0.x - CsrfPreventionFilter extends CsrfPreventionFilterBase - CsrfPreventionFilterBase implements getLogger() method declared by FilterBase with its own logger. private final Log log = LogFactory.getLog(CsrfPreventionFilterBase.class); - CsrfPreventionFilter does not implement getLogger(), This should not affect you, as getLogger() is only used by FilterBase to log misconfiguration warnings, and that is not your use case. Best regards, Konstantin Kolinko - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Struggling to get trace/debug logging from Tomcat components
Konstantin, On 3/13/25 10:55 AM, Konstantin Kolinko wrote: чт, 13 мар. 2025 г. в 16:42, Christopher Schultz : All, I really should be able to figure this out for myself but I'm frustrated and might just need someone to show me that I'm spelling something wrong. I'm trying to get all logs from the CsrfPReventionFilter. I know it's working because I get 403 responses when it's enabled and don't get them when it's not. But it's 100% silent and I'm getting no logs. I've added this to the bottom of the stock conf/logging.properties file: org.apache.catalina.filters.CsrfPreventionFilter.level = ALL org.apache.catalina.filters.CsrfPreventionFilter.handlers = java.util.logging.ConsoleHandler I shouldn't need the second line, since the default .handlers is ConsoleHandler and the async handler for catalina.[date]. I'm not seeing anything more detailed than INFO in catalina.out or catalina.[date]. But... 1catalina.org.apache.juli.AsyncFileHandler.level = ALL ... java.util.logging.ConsoleHandler.level = ALL ??! This is Tomcat 9.0.98 on Linux with a stock conf/logging.properties file (plus my .level=ALL for this filter) What am I missing? 1. It may be that your web application has its own logging.properties file in classpath, thus it is preferred over the default one. There is a logging.properties in my application's webapps/WEB-INF/classes/logging.properties file but I wouldn't expect that to interfere with Tomcat's logging. One option to prevent such behaviour is to reconfigure Tomcat to use the java.util.logging.LogManager from JRE instead of its own class. I will try that if I get desperate. :) 2. org.apache.catalina.filters.CsrfPreventionFilter.handlers = java.util.logging.ConsoleHandler If those are two separate lines, the first one lacks trailing slash, and results in an empty value for "handlers". It's on one line. I think my (or your) email client separated them. 3. If logging does not help, consider remote debugging. :/ 4. BTW, looking at 9.0.x - CsrfPreventionFilter extends CsrfPreventionFilterBase - CsrfPreventionFilterBase implements getLogger() method declared by FilterBase with its own logger. private final Log log = LogFactory.getLog(CsrfPreventionFilterBase.class); - CsrfPreventionFilter does not implement getLogger(), This should not affect you, as getLogger() is only used by FilterBase to log misconfiguration warnings, and that is not your use case. Ooh, that's a good call. Let me check. -chris - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
SOLVED (90%): Re: Has Tomcat 10 dropped support for using log4j2 as its default logger?
I found a solution. It is not perfect but it works. Man, I miss the old days of simple log4j-tomcat configurations. Feel free to use, reproduce, modify (especially if you have improvements) as you wish. *Configuring Tomcat for log4j2 logging* *NOTES*: ·A simpler configuration might be possible. ·This configuration is focused on *Windows* and routes *BOTH Tomcat’s logging and the web applications logging* to the Tomcat log4j2 config file.Most examples on the internet are only for one or the other, and are usually described in Linux/Unix terms. ·This configuration does not prevent the following exception when Tomcat is stops: Handler error java.lang.ClassCastException: class org.apache.logging.log4j.jul.LogManager cannot be cast to class java.util.logging.Handler (org.apache.logging.log4j.jul.LogManager is in unnamed module of loader 'app'; java.util.logging.Handler is in module java.logging of loader 'bootstrap') *Environment variables* ·%CATALINA_HOME% refers to the environment variable that Tomcat instructs you to create. *Tomcat Configuration* ·Create log4j2 folder structure in the tomcat folder %CATALINA_HOME%\log4j2 .\conf .\lib ·Place log4j2-tomcat.xml in *.\conf* ·Place the desired version of jars in *.\lib*.Include log4j-api, log4j-core, log4j-appserver, log4j-jul, log4j-web. ·Edit logging.properties: oComment the handlers line and replace it with: handlers=org.apache.logging.log4j.jul.LogManager ·Create setenv.bat with the following content.Edit the location of the configuration file to match the one you are using.NOTE that we are *NOT using %CATALINA_HOME% for the configurationFile location*, we are explicitly spelling it out.This is to avoid conflicts between Windows’s path format and Java’s. 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:/D:/work/soft/tomcat-10.1.7-j17corretto/log4j2/conf/log4j2-tomcat.xml *Webapp Configuration* (this example was tested with a simple SpringBoot REST service that deploys as a WAR on Tomcat, but should also work for web applications) ·Maven POM should include the following in the appropriate locations · org.apache.logging.log4j log4j-api ${log4j2.version} org.apache.logging.log4j log4j-core ${log4j2.version} org.apache.logging.log4j log4j-slf4j-impl ${log4j2.version} org.slf4j slf4j-api ${slf4j.version} org.apache.logging.log4j log4j-jakarta-web ${log4j2.version} runtime ·And note the portion of spring-boot-starter.It is necessary to avoid the default spring boot logging mechanism so that log4j can be used instead. · org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging ·Include the class Log4jContextListener below into the project.You may drop the System.out output, but I found them to be convenient for troubleshooting. ·@WebListener public class Log4jContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { String log4jConfigFile = System./getProperty/("log4j.configurationFile"); if(log4jConfigFile != null && log4jConfigFile.startsWith("file:/")) { log4jConfigFile = log4jConfigFile.substring(6); } File file = new File(log4jConfigFile); if(!file.exists()) { System./out/.println("Log4j config file "+log4jConfigFile+" does not exist"); }else{ System./out/.println("Log4j config file "+log4jConfigFile+" exists"); } LoggerContext ctx = (LoggerContext) LogManager./getContext/(false); ctx.setConfigLocation(file.toURI()); } } *IntelliJ Configuration* ·With the above configuration there is no longer a need to use a special IntelliJ Run/Debug configuration for log4j2 to work properly. ·Not only should proper logging end up in the log files, the console display should also respect the logging levels and be formatted as per the patterns specified in log4j2-tomcat.xml. On 3/11/2025 6:06 PM, Piotr P. Karwasz wrote: Hi Mark, On 11.03.2025 09:46, Mark Thomas wrote: On 10/03/2025 21:58, Piotr P. Karwasz wrote: If you are looking for instructions on how to replace Tomcat's default logging backend with Log4j Core, there is a dedicated section[2] in our new Integrating Log4j Core with Jakarta EE Guide[3] [2] https://logging.apache.org/log4j/2.x/jakarta.html#replace-tomcat Thanks for this. I have added this link to Tomcat's logging documentation and it will be in the April release round. Thanks! If someone from the Tomcat team could review the "Integrating with Jakarta EE"[1] page, I would appreciate it. For example I would like to hear your thoughts about the log separation section[2]. Tomcat documentation says: "Keep in mind that the alternative logging framework will need to be capable of working in an environment where different loggers with the same name may exist in different class
Struggling to get trace/debug logging from Tomcat components
All, I really should be able to figure this out for myself but I'm frustrated and might just need someone to show me that I'm spelling something wrong. I'm trying to get all logs from the CsrfPReventionFilter. I know it's working because I get 403 responses when it's enabled and don't get them when it's not. But it's 100% silent and I'm getting no logs. I've added this to the bottom of the stock conf/logging.properties file: org.apache.catalina.filters.CsrfPreventionFilter.level = ALL org.apache.catalina.filters.CsrfPreventionFilter.handlers = java.util.logging.ConsoleHandler I shouldn't need the second line, since the default .handlers is ConsoleHandler and the async handler for catalina.[date]. I'm not seeing anything more detailed than INFO in catalina.out or catalina.[date]. But... 1catalina.org.apache.juli.AsyncFileHandler.level = ALL ... java.util.logging.ConsoleHandler.level = ALL ??! This is Tomcat 9.0.98 on Linux with a stock conf/logging.properties file (plus my .level=ALL for this filter) What am I missing? -chris - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Struggling to get trace/debug logging from Tomcat components
Konstantin, On 3/13/25 12:34 PM, Christopher Schultz wrote: Konstantin, On 3/13/25 10:55 AM, Konstantin Kolinko wrote: чт, 13 мар. 2025 г. в 16:42, Christopher Schultz : All, I really should be able to figure this out for myself but I'm frustrated and might just need someone to show me that I'm spelling something wrong. I'm trying to get all logs from the CsrfPReventionFilter. I know it's working because I get 403 responses when it's enabled and don't get them when it's not. But it's 100% silent and I'm getting no logs. I've added this to the bottom of the stock conf/logging.properties file: org.apache.catalina.filters.CsrfPreventionFilter.level = ALL org.apache.catalina.filters.CsrfPreventionFilter.handlers = java.util.logging.ConsoleHandler I shouldn't need the second line, since the default .handlers is ConsoleHandler and the async handler for catalina.[date]. I'm not seeing anything more detailed than INFO in catalina.out or catalina.[date]. But... 1catalina.org.apache.juli.AsyncFileHandler.level = ALL ... java.util.logging.ConsoleHandler.level = ALL ??! This is Tomcat 9.0.98 on Linux with a stock conf/logging.properties file (plus my .level=ALL for this filter) What am I missing? 1. It may be that your web application has its own logging.properties file in classpath, thus it is preferred over the default one. There is a logging.properties in my application's webapps/WEB-INF/ classes/logging.properties file but I wouldn't expect that to interfere with Tomcat's logging. One option to prevent such behaviour is to reconfigure Tomcat to use the java.util.logging.LogManager from JRE instead of its own class. I will try that if I get desperate. :) 2. org.apache.catalina.filters.CsrfPreventionFilter.handlers = java.util.logging.ConsoleHandler If those are two separate lines, the first one lacks trailing slash, and results in an empty value for "handlers". It's on one line. I think my (or your) email client separated them. 3. If logging does not help, consider remote debugging. :/ 4. BTW, looking at 9.0.x - CsrfPreventionFilter extends CsrfPreventionFilterBase - CsrfPreventionFilterBase implements getLogger() method declared by FilterBase with its own logger. private final Log log = LogFactory.getLog(CsrfPreventionFilterBase.class); - CsrfPreventionFilter does not implement getLogger(), This should not affect you, as getLogger() is only used by FilterBase to log misconfiguration warnings, and that is not your use case. Ooh, that's a good call. Let me check. This isn't the problem, somehow. Or at least not the whole problem. Switching to the JDK's LogManager worked. -chris - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org