You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kiss Izolda <ki...@gmail.com> on 2012/09/16 12:19:42 UTC
Problem with using log4j Tomcat and web application level
Dear Tomcat Users,
I have configured Tomcat to use log4j according to
http://tomcat.apache.org/tomcat-7.0-doc/logging.html I have used the same
log4j.properties.
My web application is using log4j as well. There is no log4j.jar in
WEB-INF/lib.
I have found entries in the web application's root logger's appender's
file, which was in the Tomcat's root logger's appender's file
(${catalina.base}/logs/catalina.) when there was no webapp added. For
example: 2012-09-16 11:19:57,132 INFO
org.apache.catalina.startup.Catalina.start:652 - Server startup in 2028 ms
Debugging of writing this entry the parent of Category
org.apache.catalina.startup.Catalina seemed to be the web application's
root logger.
After googling on it, I have found, that the problem of "one application
can steal others applications root logger" is known.
I am using Windows7, Tomcat 7.0.26, log4j 1.2.9, and tried with Tomcat
7.0.30, log4j 1.2.17 as well and the result was the same.
The web application's log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="MYAPPENDER" class="org.apache.log4j.FileAppender">
<param name="File"
value="${catalina.base}/logs/myappendersfile.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %c.%M:%L - %m%n"
/>
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="MYAPPENDER" />
</root>
</log4j:configuration>
How can I prevent Tomcat from using the web application's root logger?
Thanks in advance,
Izolda
Re: Problem with using log4j Tomcat and web application level
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Mark,
On 9/17/12 1:36 PM, Mark Eggers wrote:
> So really, for me, it boils down to this.
>
> 1. Place log4j.xml in WEB-INF/classes 2. Write a servlet context
> listener a. contextInitialized - just write something to the log b.
> contextDestroyed - shut down logging if the app started it
Yup. Here's mine:
...
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
...
public class Log4jListener
implements ServletContextListener
{
private Logger logger;
public void contextInitialized(ServletContextEvent e)
{
// Trigger loading of the log4j.properties file from the
classpath.
new PropertyConfigurator();
logger = Logger.getLogger(this.getClass());
logger.info("Log4j initialized");
}
public void contextDestroyed(ServletContextEvent e)
{
if(LogManager.class.getClassLoader()
.equals(this.getClass().getClassLoader()))
{
if(null != logger)
logger.info("Log4j was loaded by application
classloader; shutting down.");
LogManager.shutdown();
}
else
{
if(null != logger)
logger.info("Log4j was loaded by some other
ClassLoader; not shutting down.");
}
}
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
iEYEARECAAYFAlBXewMACgkQ9CaO5/Lv0PA0OwCeINt42NSM5okJtTKbNra46If/
AlUAoLfG43X8GgMV5fJeQsaV8G1sEB57
=Nuh2
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Problem with using log4j Tomcat and web application level
Posted by Mark Eggers <it...@yahoo.com>.
On 9/17/2012 9:31 AM, Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Izolda,
>
> On 9/16/12 6:19 AM, Kiss Izolda wrote:
>> I have configured Tomcat to use log4j according to
>> http://tomcat.apache.org/tomcat-7.0-doc/logging.html I have used
>> the same log4j.properties. My web application is using log4j as
>> well. There is no log4j.jar in WEB-INF/lib.
>
> You need to put one in there if you want application-level logging to
> work the way you expect.
>
>> How can I prevent Tomcat from using the web application's root
>> logger?
>
> Tomcat will not use the web application's logger if you configure the
> two logging systems separately. You need:
>
> - - log4j.jar in CATALINA_BASE/lib (for Tomcat's logging)
> - - log4j.xml in CATALINA_BASE/lib (for Tomcat's logging config)
> - - log4j.jar in webapp's WEB-INF/lib (for app logging)
> - - log4j.xml in webapp's WEB-INF/lib (for app logging config)
>
> Also, remember that you have to trigger a configuration operation
> before you try to use the logger in your webapp, so make sure you do
> that. I'm not sure how to trigger an XML configuration, but for a
> property-file-based config, you just have to instantiate a new
> "PropertyConfigurator" object. You can probably do something similar
> for an XML-based configuration.
>
> - -chris
Chris,
Here's how I understand things to work. It appears to work in
(admittedly small) sample applications I write.
I've always just placed my log4j.xml file in WEB-INF/classes, at the
root of that directory tree. This pretty easy to do with a NetBeans or
Maven project. I don't know about Eclipse, since I'm not very familiar
with it.
Then I write a servlet context listener to start and stop logging.
Normally I don't do much there.
According to the documentation, the sequence for log4j startup is:
1. Check if override has been set (log4j.defaultInitOverride).
If so, you're on your own.
2. look for a systems property log4j.configuration.
According to the documentation, this is preferred. However, since you
need different log4j.xml files (one for Tomcat, one for each web app),
this isn't going to work very well.
3. Convert the resource to a URL
This will fail obviously if it's empty.
4. If step 3 fails, then search for a log4j.xml or log4j.properties
Basically, this will try first the thread context classloader, and then
the classloader that loaded this class. Finally, use the system
classloader. So, if you have a log4j.xml file in WEB-INF/classes and one
in CATALINA_BASE/lib, your web application should find the one in
WEB-INF/classes first.
5. If no suitable log4j.xml (or log4j.properties) file is found, abort
So really, for me, it boils down to this.
1. Place log4j.xml in WEB-INF/classes
2. Write a servlet context listener
a. contextInitialized - just write something to the log
b. contextDestroyed - shut down logging if the app started it
You could add additional log4j configuration mechanisms in the
contextInitialed method (like change log file names based on host name)
if you wish.
. . . . just my two cents.
/mde/
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Problem with using log4j Tomcat and web application level
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Izolda,
On 9/16/12 6:19 AM, Kiss Izolda wrote:
> I have configured Tomcat to use log4j according to
> http://tomcat.apache.org/tomcat-7.0-doc/logging.html I have used
> the same log4j.properties. My web application is using log4j as
> well. There is no log4j.jar in WEB-INF/lib.
You need to put one in there if you want application-level logging to
work the way you expect.
> How can I prevent Tomcat from using the web application's root
> logger?
Tomcat will not use the web application's logger if you configure the
two logging systems separately. You need:
- - log4j.jar in CATALINA_BASE/lib (for Tomcat's logging)
- - log4j.xml in CATALINA_BASE/lib (for Tomcat's logging config)
- - log4j.jar in webapp's WEB-INF/lib (for app logging)
- - log4j.xml in webapp's WEB-INF/lib (for app logging config)
Also, remember that you have to trigger a configuration operation
before you try to use the logger in your webapp, so make sure you do
that. I'm not sure how to trigger an XML configuration, but for a
property-file-based config, you just have to instantiate a new
"PropertyConfigurator" object. You can probably do something similar
for an XML-based configuration.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
iEYEARECAAYFAlBXUGMACgkQ9CaO5/Lv0PCHbwCgi3OM1T1yZlQQdEktcV1+q3Zw
gb4AoKDupX+E2n9YxZq9OnaEdhAE9HiE
=2csn
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org