Hi Piotr,

Thanks for your explanation.

Indeed, once I hard code the path to the log4j configuration file into web.xml, 
logging works in both cases (app undeployment/deployment as well as Tomcat 
shutdown/restart).

However, what I was trying to achieve by using a servlet context listener was 
to be able to set the location of the log4j configuration file at run time.
I’m trying to make the app compatible with different app servers where 
configuration files are typically placed in different locations.
So I’m figuring out at run time which app server the app is running in, and 
based on that I know where to expect the log4j configuration file.

Is there any way to do this? Or am I stuck with using a fixed path via web.xml?

Thanks,
Piers



> Am 13.03.2024 um 23:35 schrieb Piotr P. Karwasz <piotr.karw...@gmail.com>:
> 
> Hi Piers,
> 
> On Wed, 13 Mar 2024 at 22:29, Piers Uso Walter <piers.wal...@ilink.de> wrote:
>> 2024-03-13T19:49:02.609143Z Catalina-utility-1 INFO 
>> Log4jServletContextListener triggered a Log4j context initialization.
>> 
>> Case 2:
>> However, if I stop and restart the Tomcat service (with the war file having 
>> been previously deployed), the app no longer logs anything and I don’t see a 
>> catalina log line about Log4j context initialization:
> 
> The message in the first line is not logged through Log4j Core, but
> the status logger (an internal logging system of the Log4 API). You
> can set the level of the status logger using the
> `log4j2.StatusLogger.level` property. By default it only shows `ERROR`
> messages, but it changes level based on the `status` property
> 
>> I configured a servlet context listener in order to be able to set the 
>> system property log4j.configurationFile upon application start.
>> This allows me to place the log4j configuration file outside of the war file.
> 
> A servlet context listener is not the right tool for this, since it is
> executed **after** Log4j's `ServletContainerInitializer`.
> You should rather specify the property as a servlet context init
> parameter. See the current documentation:
> 
> https://logging.apache.org/log4j/2.x/manual/webapp.html#configuration
> 
> or a preview of the documentation of the next major version:
> 
> https://logging.staged.apache.org/log4j/jakarta/latest/#log4j-jakarta-web-configuration
> 
>> This is my web.xml:
>> <?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>
>>                <!-- Adding a servlet context listener to execute code before 
>> the application is being initialized. -->
>>                
>> <listener-class>de.ilink.cloud.ucc.gw.LdapGwServletContextListener</listener-class>
>>        </listener>
>> 
>> </web-app>
> 
> You should add:
> 
>    <context-param>
>        <param-name>log4jConfiguration</param-name>
>        <param-value>path_to_your_config_file_or_resource</param-value>
>    </context-param>
> 
> Also if your context listener uses logging, you might want to register
> a `Log4jServletContextListener` manually, so it shuts down **after**
> your listener:
> 
>    <listener>
>        
> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
>    </listener>
>    <listener>
>        <!-- Adding a servlet context listener to execute code before
> the application is being initialized. -->
>        
> <listener-class>de.ilink.cloud.ucc.gw.LdapGwServletContextListener</listener-class>
>    </listener>
> 
> Piotr
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to