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

2024-03-13 Thread Piers Uso Walter
Hi all,

I have a small web app that runs in Tomcat 9 and uses log4j 2 for logging.
This works in one case and does not work in another case:


Case 1:
When I deploy the war file to Tomcat (copying the war file to the Tomcat 
webapps folder), everything works as expected.
The log file is created (if missing) and the app’s log output appears in the 
log file.

In the catalina log file I see that the Log4j context has been initialized (the 
two 'GW.war' lines are being written to stdout by my app):

13-Mar-2024 20:49:02.229 INFORMATION [Catalina-utility-1] 
org.apache.catalina.startup.HostConfig.deployWAR Deploying web application 
archive [/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/webapps/GW.war]
13-Mar-2024 20:49:02.475 INFORMATION [Catalina-utility-1] 
org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for 
TLDs yet contained no TLDs. Enable debug logging for this logger for a complete 
list of JARs that were scanned but no TLDs were found in them. Skipping 
unneeded JARs during scanning can improve startup time and JSP compilation time.
GW.war: ServletContextListener started
GW.war: System property 
log4j.configurationFile=/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/conf/ilink-LDAP-GW-log4j2.xml
2024-03-13T19:49:02.609143Z Catalina-utility-1 INFO Log4jServletContextListener 
triggered a Log4j context initialization.
13-Mar-2024 20:49:02.646 INFORMATION [Catalina-utility-1] 
org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application 
archive [/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/webapps/GW.war] has 
finished in [417] ms


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:

13-Mar-2024 20:49:34.329 INFORMATION [main] 
org.apache.catalina.startup.HostConfig.deployWAR Deploying web application 
archive [/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/webapps/GW.war]
13-Mar-2024 20:49:34.609 INFORMATION [main] 
org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for 
TLDs yet contained no TLDs. Enable debug logging for this logger for a complete 
list of JARs that were scanned but no TLDs were found in them. Skipping 
unneeded JARs during scanning can improve startup time and JSP compilation time.
GW.war: ServletContextListener started
GW.war: System property 
log4j.configurationFile=/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/conf/ilink-LDAP-GW-log4j2.xml
13-Mar-2024 20:49:34.781 INFORMATION [main] 
org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application 
archive [/Users/piers/Documents/Tomcat/apache-tomcat-9.0.86/webapps/GW.war] has 
finished in [452] ms


I am not sure what I might be doing wrong here.

I see that the deployment is being reported using different logger names 
(thread names?) in both cases: [Catalina-utility-1] vs. [main].
I don’t know if this should make a difference though.

The app uses the servlet API 3.1.
I included log4j-web-2.23.0.jar in WEB-INF/lib for proper initialization.
(also included are log4j-api-2.23.0.jar, log4j-core-2.23.0.jar, and 
log4j-slf4j-impl-2.23.0.jar in case this might be relevant)

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.

This is my web.xml:

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">




de.ilink.cloud.ucc.gw.LdapGwServletContextListener





I’m stuck at this point and would appreciate any ideas of what might be going 
on here.


Piers

-- 
Piers Uso Walter 
ilink Kommunikationssysteme GmbH



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



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

2024-03-13 Thread Piotr P. Karwasz
Hi Piers,

On Wed, 13 Mar 2024 at 22:29, Piers Uso Walter  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:
> 
> 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">
>
> 
> 
> 
> de.ilink.cloud.ucc.gw.LdapGwServletContextListener
> 
>
> 

You should add:


log4jConfiguration
path_to_your_config_file_or_resource


Also if your context listener uses logging, you might want to register
a `Log4jServletContextListener` manually, so it shuts down **after**
your listener:



org.apache.logging.log4j.web.Log4jServletContextListener




de.ilink.cloud.ucc.gw.LdapGwServletContextListener


Piotr

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