Re: log4j initialization does not seem to happen when Tomcat service is started

Piers Uso Walter <[email protected]> Sun, 31 Mar 2024 23:38:57 +0200
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <[email protected]>
Hi Piotr,

Thanks for the reminder.
I did in fact do this, it had already been included in Thad’s solution.

The idea of my email was to describe changes I made to Thad’s code.
I now realize that it would have been smarter (and more useful to future mailing list readers) if I had included the rest as well.

So here’s my full web.xml.
Together with the LogListener.java I posted earlier, this achieves what I was trying to do.


-----------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

	 <listener>
	 <!-- This listener *must* be first for log4j to work. -->
	 <listener-class>com.mycorp.server.rest.listeners.LogListener</listener-class>
	 </listener>

	<!-- See https://logging.apache.org/log4j/2.x/manual/webapp.html -->
	<filter>
		<filter-name>log4jServletFilter</filter-name>
		<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>log4jServletFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>ERROR</dispatcher>
		<dispatcher>ASYNC</dispatcher> <!-- Servlet 3.0 w/ disabled auto-initialization only; not supported in 2.5 -->
	</filter-mapping>

	<context-param>
		<param-name>isLog4jAutoInitializationDisabled</param-name>
		<param-value>true</param-value>
	</context-param>

</web-app>

-----------------------------------------------------


Thanks as well for your two other suggestions.
This is very much appreciated.


With kind regards

Piers

-- 
Piers Uso Walter <[email protected]>
ilink Kommunikationssysteme GmbH
Kurfuerstendamm 67, 10707 Berlin, Germany
+49 (30) 28526-185


> Am 31.03.2024 um 22:54 schrieb Piotr P. Karwasz <[email protected]>:
> 
> Hi Piers,
> 
> On Sun, 31 Mar 2024 at 22:37, Piers Uso Walter <[email protected]> wrote:
>> 
>> Thad,
>> 
>> 
>> Thanks so much. This looks exactly what I want to do.
>> But somehow I never got your code to work.
>> 
>> ...
>> 
>> I made these changes to your code:
>> 
>> 1.
>> LogListener is now a subclass of Log4jServletContextListener, instead of just implementing ServletContextListener.
>> So I’m only declaring a single listener in web.xml.
>> 
>> 2.
>> I’m not using the servlet init parameter Log4jWebSupport.LOG4J_CONFIG_LOCATION to set the config file location.
>> Instead, I’m initializing a log4j Configurator from the file.
> 
> Consider also setting the servlet init parameter
> `isLog4jAutoInitializationDisabled` to `true` to disable the servlet
> container initializer.
> 
> Piotr
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>