[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tomcat-user
Subject:    Re: using log4j in a webapp using embedded tomcat
From:       Christopher Schultz <chris () christopherschultz ! net>
Date:       2014-01-29 15:12:04
Message-ID: 52E91A44.2000400 () christopherschultz ! net
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

John,

On 1/28/14, 8:38 PM, John Cartwright - NOAA Federal wrote:
> Thanks for your reply Chris.
> 
> On Mon, Jan 27, 2014 at 12:40 PM, Christopher Schultz < 
> chris@christopherschultz.net> wrote:
> 
>> Are you running one and only one webapp in your Tomcat instance?
>> If not, then the system property will override it for all
>> webapps, which will probably cause confusion (especially if
>> log4j.jar is in the webapp's WEB-INF/lib directory, since log4j
>> will be initialized more than once).
> 
> 
> For this use case, I'm only running one webapp per tomcat instance.
> Good reminder though about confusion in the case of multiple
> webapps.
> 
> 
>> Where exactly in the "class path"?
>> 
> 
> I've tried both specifying it as an -cp argument when executing
> the webapp-runner and including it in the WAR file WEB-INF/lib

If you want to use log4j in your webapp, then WEB-INF/lib is the
proper place to put it. If you want to use log4j for Tomcat's internal
logging, then you'll have to make sure it's accessible to the
Bootstrap/Tomcat class when you invoke it. There are a variety of ways
to do that. Are you creating a ClassLoader for Tomcat's use, or are
you just using the system ClassLoader by default? If the latter, you
should be able to just add log4j.jar to the CLASSPATH environment
variable or use the -cp option when launching the JVM. If the former,
then you'll have to explicitly add that JAR file to the list of
resources the ClassLoader can access.

>> How are you initializing log4j itself? In a
>> ServletContextListener or something similar? Do you manually call
>> "new PropertyConfiguration()" or something like that? If so, why
>> not simply put log4j.properties into WEB-INF/classes and remove
>> the system property? I think that will simplify things and likely
>> work better.
>> 
> 
> I'm simply initializing the logger inside the servlet, e.g.
> 
> static Logger log = Logger.getLogger(HelloWorldExample.class);

I believe this implicitly calls "new PropertyConfigurator()" which
will search the class path (of the thread's context ClassLoader) for
"/log4j.properties". If you put log4j.properties into WEB-INF/classes
and log4j.jar into WEB-INF/lib, everything should work without you
having to play-around with the class paths, ClassLoaders, etc. -- at
least if you only want to use log4j in your web application.

For a web application, embedded should be nothing different than
non-embedded.

> You're right though, I can put the log4j.properties file in
> WEB-INF/classes and that works.  It's just that in this case being
> able to override the logging properties when executing the
> webapp-runner would have been convenient.

You can probably still do it with a system property. Just be aware
that system properties affect all web applications ;)

If you explicitly call "new PropertyConfigurator()" yourself, you can
specify the location of the properties file. If you want to be able to
use system properties, consider locating the file in the classpath as
a default, but also allow the use of a webapp-specific system property
to override the default. Something like
my-webapp.log4j.properties.file or something like that. You'll get
more mileage out of that than trying to use a more "global" system
property.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJS6RpEAAoJEBzwKT+lPKRYCAoQAJ08EqBm6l/wEzSOEcnDaa9B
cF5DOSDy3vUk0VbJ40cFk+CmJhjwm51YPtDkimCtbLl3/xT5cd8hY3bmXioqi3qq
kS+AxDUT+ezB+rCSzaNED/3zwbTZr4+5BRgjO/Pr45cNIqxioDCRfyr+m5k7AwvG
0/9cK9B4vnFVfBVu/JJ3fSeOQTuvEko+o6ycAZwlP9fpukxvXIGaqSaPj1yEYPbK
c6V7lw+8jJwxFsWF3J3BpkIehP2HDXKZ/XvQVmy+Xdmm472xO3T2R7NQhqpNAIda
FnQhjApGUMwIERrYOH2Amsy0p6vnYbj9GkFDUe9es/Aq0soka20d9WmI7Y3xAsyo
AwEVsJKh8rmVB66OaGosc0qIAK2afo3IcWXrzNSV5kW70iRd93aHa/jMEA22BblW
NgbTyTMkA0SzqNJ2jurdGmQsKxsL8KbRBXI+vfEPXn5AQLeuBmRETa/ZlrfPKBTd
lUQOI4GD9pTbpFLwbJqxCcUQO6zguFe9NDVv0kh/uUrUQLw9ExotesXFVo7jFARC
IUAVODQ4LDVe5VFpCg2k/8NMKmXt8rxsQ0REFt7UEvv8N4SATlSw5adTzScK6Cbg
pd1k+zOUpPRjpUgIUH+SndHLKvsJPi+dFACOFo7jl769p/mt04T11lVnQcvPsHt6
kPOQfkxCXGBu4ahplJNd
=cavV
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic