how to configure log4j (1.2) in JBoss 6?

2013-05-20 Thread Piers Uso Walter
Hi,

I am migrating an application from JBoss 4 to JBoss 6 and stumble over the fact 
that log4j does not seem to be supported by JBoss 6 out of the box any more.

I've researched this quite extensively on the web, but have only found 
questions from other people, no answers. I hope someone in the log4j community 
has successfully used log4j (1.2) with JBoss 6 and might be able to help.


Our previous setup with a log4j.xml configuration file in server/default/conf/ 
no longer works. That configuration files does not seem to be detected and used 
by JBoss 6.
Basic logging (as configured in server/default/deploy/jboss-logging.xml) works, 
but I have not been successful in configuring log4j appenders in that file. 
From what I understand, this file does not really configure log4j, as JBoss 6 
uses a new logging mechanism.
 
For example, I've modified jboss-logging.xml to use a log4j console appender 
and a log4j file appender instead of the standard appenders (see attached 
file). This did not work. The log file ${jboss.server.log.dir}/log4j.log is not 
created, the console log stays at level INFO (instead of DEBUG).
 
What would I have to do in order to get JBoss 6 to use log4j for my 
applications?
One requirement we have is to store the log4j configuration in a central file 
and not within the application packages (because for debugging purposes we ask 
our customers to modify the configuration).

I'm using JBoss 6.1 final, in case that makes a difference.
 
Thanks a lot
Piers

-- 
Piers Uso Walter piers.wal...@ilink.de
ilink Kommunikationssysteme GmbH




jboss-logging.xml
Description: XML document

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

Re: how to configure log4j (1.2) in JBoss 6?

2013-05-23 Thread Piers Uso Walter
Hi Remko,

yes, I know :-)

The JBoss forums are full of questions like mine, but the JBoss community seems 
to have long moved on from JBoss 6, so I have not found any answers there. The 
way to integrate log4j seems to have changed from JBoss 6 to newer versions, so 
understandably it seems that no one working on or with JBoss 7 or 8 wants to 
take the time to research how that used to work back in JBoss 6.

I'll keep working on it off and on as my other tasks allow and will report back 
when I've got it to work.

With kind regards,
Piers


Am 23.05.2013 um 00:17 schrieb Remko Popma:

 Piers,
 
 To be honest, this sounds more like a question that should be directed to the 
 JBoss people...
 
 Kind regards,
 Remko
 
 Sent from my iPhone
 
 On 2013/05/20, at 22:45, Piers Uso Walter piers.wal...@ilink.de wrote:
 
 Hi,
 
 I am migrating an application from JBoss 4 to JBoss 6 and stumble over the 
 fact that log4j does not seem to be supported by JBoss 6 out of the box any 
 more.
 
 I've researched this quite extensively on the web, but have only found 
 questions from other people, no answers. I hope someone in the log4j 
 community has successfully used log4j (1.2) with JBoss 6 and might be able 
 to help.
 
 
 Our previous setup with a log4j.xml configuration file in 
 server/default/conf/ no longer works. That configuration files does not seem 
 to be detected and used by JBoss 6.
 Basic logging (as configured in server/default/deploy/jboss-logging.xml) 
 works, but I have not been successful in configuring log4j appenders in that 
 file. From what I understand, this file does not really configure log4j, as 
 JBoss 6 uses a new logging mechanism.
 
 For example, I've modified jboss-logging.xml to use a log4j console appender 
 and a log4j file appender instead of the standard appenders (see attached 
 file). This did not work. The log file ${jboss.server.log.dir}/log4j.log is 
 not created, the console log stays at level INFO (instead of DEBUG).
 
 What would I have to do in order to get JBoss 6 to use log4j for my 
 applications?
 One requirement we have is to store the log4j configuration in a central 
 file and not within the application packages (because for debugging purposes 
 we ask our customers to modify the configuration).
 
 I'm using JBoss 6.1 final, in case that makes a difference.
 
 Thanks a lot
 Piers
 
 -- 
 Piers Uso Walter piers.wal...@ilink.de
 ilink Kommunikationssysteme GmbH
 
 
 jboss-logging.xml
 
 -
 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
 


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



Re: how to configure log4j (1.2) in JBoss 6?

2013-05-23 Thread Piers Uso Walter
Hi Brett,

this sounds like an interesting idea that's different from what I've tried so 
far. I'll see if I'll get that to work...

With kind regards,
Piers


Am 23.05.2013 um 01:31 schrieb Brett Randall:

 Piers,
 
 I've not done this on JBoss 6.
 
 If your application uses Spring Framework, you could initialize log4j using
 their org.springframework.web.util.Log4jConfigListener as described in this
 blog [1].
 
 If you aren't using Spring, you can still take a similar approach - create
 a ServletContextListener and use it to configure log4j with a file of your
 choosing.  It sounds like you'll want something that's not on the
 classpath, or at least is added to the classpath externally, or perhaps an
 absolute or relative file path.
 
 [1]
 http://rocksolutions.wordpress.com/2011/06/12/log4j-configuration-in-spring-application/
 
 
 On 20 May 2013 23:45, Piers Uso Walter piers.wal...@ilink.de wrote:
 
 Hi,
 
 I am migrating an application from JBoss 4 to JBoss 6 and stumble over the
 fact that log4j does not seem to be supported by JBoss 6 out of the box any
 more.
 
 I've researched this quite extensively on the web, but have only found
 questions from other people, no answers. I hope someone in the log4j
 community has successfully used log4j (1.2) with JBoss 6 and might be able
 to help.
 
 
 Our previous setup with a log4j.xml configuration file in
 server/default/conf/ no longer works. That configuration files does not
 seem to be detected and used by JBoss 6.
 Basic logging (as configured in server/default/deploy/jboss-logging.xml)
 works, but I have not been successful in configuring log4j appenders in
 that file. From what I understand, this file does not really configure
 log4j, as JBoss 6 uses a new logging mechanism.
 
 For example, I've modified jboss-logging.xml to use a log4j console
 appender and a log4j file appender instead of the standard appenders (see
 attached file). This did not work. The log file
 ${jboss.server.log.dir}/log4j.log is not created, the console log stays at
 level INFO (instead of DEBUG).
 
 What would I have to do in order to get JBoss 6 to use log4j for my
 applications?
 One requirement we have is to store the log4j configuration in a central
 file and not within the application packages (because for debugging
 purposes we ask our customers to modify the configuration).
 
 I'm using JBoss 6.1 final, in case that makes a difference.
 
 Thanks a lot
 Piers
 
 --
 Piers Uso Walter piers.wal...@ilink.de
 ilink Kommunikationssysteme GmbH
 
 
 
 
 -
 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



Re: how to configure log4j (1.2) in JBoss 6?

2013-05-23 Thread Piers Uso Walter
Hi Nick,

 The solution is to configure JBoss to disable parent-first class loading. The 
 method by which you do this has changed from one JBoss version to another.

Thanks a lot! From my (somewhat limited) understanding of the problem, this 
sounds exactly what I need to do, so this will be the next step that I'll 
attempt (might take some time, though).

With kind regards,
Piers


Am 23.05.2013 um 01:42 schrieb Nick Williams:

 Piers,
 
 IIRC, JBoss 6, by default, prefers the parent class loaders over child class 
 loaders. This means that, when a class path resource is referenced, it is 
 first searched for in the server class loaders and _then_ in the web 
 application class loader. This is similar to the default behavior in 
 WebSphere and WebLogic, and counter to the default behavior in GlassFish and 
 Tomcat (and even earlier versions of JBoss), which search for a resources 
 _first_ in the web application class loader and _then_ in the server class 
 loaders (IMO, the correct behavior).
 
 The consequence of this is that the log4j.properties/log4j.xml file shipped 
 with JBoss override the log4j.xml file shipped with your application.
 
 The solution is to configure JBoss to disable parent-first class loading. The 
 method by which you do this has changed from one JBoss version to another. 
 Used to be a setting in /WEB-INF/jboss-web.xml, but it looks like it may have 
 been moved. You should consult the JBoss documentation for instructions on 
 disabling parent-first class loading. Once you do this, for your application 
 only, Log4j will use your configuration files (this only works if the Log4j 
 JAR files are also included in your /WEB-INF/lib directory).
 
 Nick
 
 On May 22, 2013, at 5:17 PM, Remko Popma wrote:
 
 Piers,
 
 To be honest, this sounds more like a question that should be directed to 
 the JBoss people...
 
 Kind regards,
 Remko
 
 Sent from my iPhone
 
 On 2013/05/20, at 22:45, Piers Uso Walter piers.wal...@ilink.de wrote:
 
 Hi,
 
 I am migrating an application from JBoss 4 to JBoss 6 and stumble over the 
 fact that log4j does not seem to be supported by JBoss 6 out of the box any 
 more.
 
 I've researched this quite extensively on the web, but have only found 
 questions from other people, no answers. I hope someone in the log4j 
 community has successfully used log4j (1.2) with JBoss 6 and might be able 
 to help.
 
 
 Our previous setup with a log4j.xml configuration file in 
 server/default/conf/ no longer works. That configuration files does not 
 seem to be detected and used by JBoss 6.
 Basic logging (as configured in server/default/deploy/jboss-logging.xml) 
 works, but I have not been successful in configuring log4j appenders in 
 that file. From what I understand, this file does not really configure 
 log4j, as JBoss 6 uses a new logging mechanism.
 
 For example, I've modified jboss-logging.xml to use a log4j console 
 appender and a log4j file appender instead of the standard appenders (see 
 attached file). This did not work. The log file 
 ${jboss.server.log.dir}/log4j.log is not created, the console log stays at 
 level INFO (instead of DEBUG).
 
 What would I have to do in order to get JBoss 6 to use log4j for my 
 applications?
 One requirement we have is to store the log4j configuration in a central 
 file and not within the application packages (because for debugging 
 purposes we ask our customers to modify the configuration).
 
 I'm using JBoss 6.1 final, in case that makes a difference.
 
 Thanks a lot
 Piers
 
 -- 
 Piers Uso Walter piers.wal...@ilink.de
 ilink Kommunikationssysteme GmbH
 
 
 jboss-logging.xml
 
 -
 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
 
 
 
 -
 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



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

2024-03-14 Thread Piers Uso Walter
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 :
> 
> 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
> 


-
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-20 Thread Piers Uso Walter
Hi Piotr,

Thanks for your email.

Here is what I’m trying to do:
The previous version of our app stores the log4j configuration within the war 
file.
This works fine but is annoying if one wants to change any logging setting 
(unpacking the war, changing the configuration, repacking the war, redeploying).

For the next version of our app, I would like to move the log4j configuration 
to a place outside of the war.
For Tomcat I would like to use $catalina.home/conf/ilink/GW-log4j2.xml
and for Wildlfy I would like to use $jboss.home.dir/conf/ilink/GW-log4j2.xml
(with $catalina.home and $jboss.home.dir being system properties that are 
defined in the two servers)

Since the configuration file path is only known at runtime I cannot use the 
servlet initialization parameter ‘log4jConfiguration’ in web.xml.
So I looked at log4j's automatic configuration [1].
This led me to the idea to use a ServletContextListener to determine if the app 
is running in WIldFly or in Tomcat and then setting the system property 
$log4j.configurationFile accordingly.

But as you point out, the ServletContextListener is executed too late.
That explains why it does not work in my case 2 (when the Tomcat service is 
restarted).
I still don’t understand why it does work in my case 1 (when the app is being 
redeployed in a running Tomcat), but that is not so important as this does not 
seem to be the correct approach anyway.

Also, thanks for pointing out what should have been obvious: that other web 
apps on the same server will be affected when I set the 
`log4j2.configurationFile` property.
Not a good idea.

Am I trying something unusual here when I attempt to make the app decide where 
the log4j configuration file is located?
Is there any other way in which I can achieve this?
Is there an API for this?

Thanks,
Piers

[1] 
https://logging.apache.org/log4j/2.x/manual/configuration.html#automatic-configuration



> Am 15.03.2024 um 07:45 schrieb Piotr P. Karwasz :
> 
> Hi Piers,
> 
> On Thu, 14 Mar 2024 at 14:08, Piers Uso Walter  wrote:
>> 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.
> 
> Can provide more details as to where the path to the configuration
> file is actually stored? If you change the `log4j2.configurationFile`
> property, other web apps on the same server will be affected, which
> might be an unpleasant surprise to users. There are less invasive ways
> to do that.
> 
> I also wonder if maintaining container specific code is worth the
> effort. Many application servers have a detailed guide to configuring
> logging (e.g. WildFly[1]). Users might be unwilling to learn yet
> another way to configure logging.
> 
> Sure, Tomcat is an exception, that is why I maintain a small set of
> Log4j Core plugins and Tomcat extensions[2] to help users
> administering logging at a global level.
> 
> Piotr
> 
> [1] https://docs.wildfly.org/31/Admin_Guide.html#Logging
> [2] https://github.com/copernik-eu/log4j-tomcat
> 
> -
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
> 



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-31 Thread Piers Uso Walter
Hi Jeff,

Thanks for your suggestion.
I ended up doing it slightly different (see my response to Thad Humphries).

However, I’ll keep your idea in mind because the ability to both include a 
standard configuration in the app and also optionally modifying this 
configuration via an external config file seems interesting. I can easily see 
that we might want to do this in the future.

With kind regards

Piers

-- 
Piers Uso Walter 
ilink Kommunikationssysteme GmbH
Kurfuerstendamm 67, 10707 Berlin, Germany
+49 (30) 28526-185


> Am 21.03.2024 um 07:19 schrieb jeffrey.tho...@t-systems.com:
> 
> Hi Piers,
> 
> I am not from Log4j Team - just a user like you :)
> 
> We have a similar situation with internal distribution configuration and 
> external "customer" configuration.
> 
> What we are now doing is programmatically creating a CompositeConfiguration 
> with the internal config and external config and reconfiguring Log4j early in 
> our startup.
> 
> Cheers, Jeff



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

2024-03-31 Thread Piers Uso Walter
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.


-


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

 
 
 
com.mycorp.server.rest.listeners.LogListener
 



log4jServletFilter

org.apache.logging.log4j.web.Log4jServletFilter


log4jServletFilter
/*
REQUEST
FORWARD
INCLUDE
ERROR
ASYNC 



isLog4jAutoInitializationDisabled
true




-


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


With kind regards

Piers

-- 
Piers Uso Walter 
ilink Kommunikationssysteme GmbH
Kurfuerstendamm 67, 10707 Berlin, Germany
+49 (30) 28526-185


> Am 31.03.2024 um 22:54 schrieb Piotr P. Karwasz :
> 
> Hi Piers,
> 
> On Sun, 31 Mar 2024 at 22:37, Piers Uso Walter  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: 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-31 Thread Piers Uso Walter
Thad,


Thanks so much. This looks exactly what I want to do.
But somehow I never got your code to work.


I finally modified your approach a bit, and now it works for me.
I am now able to dynamically set the location of the log4j configuration file 
in my app.
And this works not just when the app is deployed/redeployed, but also when the 
Tomcat service is restarted after the app has been deployed.


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.

-

LogListener.java:
public class LogListener extends Log4jServletContextListener {
@Override
 public void contextDestroyed(ServletContextEvent event) {
 super.contextDestroyed(event);
}

@Override
public void contextInitialized(ServletContextEvent event)
{
logConfigPath = … // Determine the path of the log 
configuration file

Configurator.initialize(null, logConfigPath);
super.contextInitialized(event);
}
}


web.xml:

 
 
com.mycorp.server.rest.listeners.LogListener


-


Your email was instrumental for me being able to find this solution.
Thanks again :-)


With kind regards

Piers

-- 
Piers Uso Walter 
ilink Kommunikationssysteme GmbH
Kurfuerstendamm 67, 10707 Berlin, Germany
+49 (30) 28526-185


> Am 21.03.2024 um 13:43 schrieb Thad Humphries :
> 
> Piers,
> 
> I've configured two webapps with the log4j2 configuration outside of the
> *war files. The approach I'll describe is working in Tomcat 8.5 and Tomcat
> 9.
> 
> First I create a LogListener class for the webapp:
> 
> public class LogListener implements ServletContextListener {
> 
>  private static final Logger LOGGER = LogManager.getLogger();
>  private Log4jServletContextListener listener = new
> Log4jServletContextListener();
> 
>  @Override
>  public void contextDestroyed(ServletContextEvent event) {
>listener.contextDestroyed(event);
>  }
> 
> 
>  @Override
>  public void contextInitialized(ServletContextEvent event) {
>String loggerPath = "WEB-INF/classes/log4j2.xml";
> 
>ServletContext context = event.getServletContext();
>String fileString = context.getInitParameter("log4jConfiguration");
>if (fileString != null && fileString.length() > 0
>&& !fileString.equals("*")) {
>  if ((new File(fileString)).exists()) {
>loggerPath = fileString;
>  }
>}
>event.getServletContext().setInitParameter(
>  Log4jWebSupport.LOG4J_CONFIG_LOCATION, loggerPath);
>listener.contextInitialized(event);
>LOGGER.info("logging properties from " + loggerPath);
>  }
> }
> 
> 
> I declare the LogListener in web.xml and add some filtering:
> 
>  
>
> 
> org.apache.logging.log4j.web.Log4jServletContextListener
>  
>  
>
> 
> com.mycorp.server.rest.listeners.LogListener
>  
> 
>  
>  
>log4jServletFilter
> 
> org.apache.logging.log4j.web.Log4jServletFilter
>  
>  
>log4jServletFilter
>/*
>REQUEST
>FORWARD
>INCLUDE
>ERROR
>ASYNC
>  
> 
>  
>isLog4jAutoInitializationDisabled
>true
>  
> 
> Finally, in the Tomcat context file, META-INF/context.xml, I add the
> log4jConfiguration that LogListener wants:
> 
>  name="log4jConfiguration" override="false"
> value="/Users/admin/local/etc/my_logger.xml"/>
> 
> 
> When I start Tomcat, the first entry in the log is
> 
> 12:23:23.439 INFO  LogListener.contextInitialized() - logging properties
> from /Users/admin/local/etc/my_logger.xml
> 
> At one time I had this working in Jetty, but I no longer remember where to
> set its context.
> 
> We've installed these apps on numerous servers, both Linux (of various
> distros) and macOS. AFAIK, they're also running on a number of Windows
> servers as well. Logging works everywhere.
> 
> On Wed, Mar 20, 2024 at 12:09 PM Piers Uso Walter 
> wrote:
> 
>> Hi Piotr,
>> 
>> Thanks for your email.
>> 
>> Here is what I’m trying to do:
>> The previous version of our app stores the log4j configuration within the
>> war file.
>> This works fine but is annoying if one wants to change any logging setting
>> (unpacking the war, changing the configu