tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Barker" <wbar...@wilshire.com>
Subject Re: How to use logging in Tomcat correctly (declare Logger Static or not) ?
Date Sat, 18 Nov 2006 03:29:37 GMT

"Mon Cab" <futon33@yahoo.com> wrote in message 
news:928711.20131.qm@web32812.mail.mud.yahoo.com...
>
>>
>> Good day
>>
>> I am having a hard time understanding how to use commons logging on
> Tomcat.   The struts.apache.org suggest the following usage (presumably
> for  business objects) (taken from:
>> http://struts.apache.org/1.x/userGuide/building_controller.html )
>>
>>
>> package com.foo;
>>                     // ...
>>                     import org.apache.commons.logging.Log;
>>                     import org.apache.commons.logging.LogFactory;
>>                     ...
>>                     public class User {
>>                     // ...
>>                     private static Log log =
>> LogFactory.getLog(User.class);
>>                     // ...
>>                     public void setBar(Bar bar) {
>>
>>                     log.trace("Setting bar to " + bar);
>>                     this.bar = bar;
>>                     }
>>                     // ...
>>                     }
>>
>>
>> The suggestion seems to be that it is OK to declare my Logger as a
>> static variable in the business object.  This is ideal for me
>> because,
>> I will be accessing the Logger from both instance and static methods,
>> and so I do not want to have to declare this as an instance variable.
>>
>>
>> However, I am wondering whether this is OK, given that my business
>> objects will be being accessed from different servlet threads.  Can
>> anyone tell me whether this is OK?, and if not, what is the correct
>> way
>> to do this?
>>

I think that there may be a race condition with SimpleLog.  But JULI and 
Log4j are thread-safe, so it's fine to use a static instance of Log with 
these.

>> My second question is whether the same usage is valid for Action (or
> servlet)  classes. I saw the following example, at
>>
> http://www.mobilefish.com/developer/struts/struts_quickguide_log4j.html
>> but am not certain as to whether using a static variable would be OK
>> here.  Would it?
>>
>> public class DemoServlet extends Servlet {
>>
>>       private static Log log =
>> LogFactory.getLog("com.mobilefish.DemoAction");
>>
>>       public service  etc etc      {}
>
>> }
>>
>> The commons logging documentation notes the following:
>>
>> Note that for application code, declaring the log member as "static"
>> is
>> more efficient as one Log object is created per class, and is
>> recommended. However this is not safe to do for a class which may be
>> deployed via a "shared" classloader in a servlet or j2ee container or
>> similar environment. If the class may end up invoked with different
>> thread-context-classloader values set then the member must not be
>> declared static. The use of "static" should therefore be avoided in
>> code within any "library" type project.
>>
>> I'm not clear as to what the above means, from a practical
>> perspective
>> in the context of a web applicaiton.  What does this mean in simple
>> terms?
>>

It means that you have potential gotus if you use a static instance in a 
class that is in a jar in $CATALINA_HOME/shared/lib or 
$CATALINA_HOME/common/lib.  It's fine to use a static instance if the class 
is living in a jar in WEB-INF/lib.

>> Taken from:
>>
> http://jakarta.apache.org/commons/logging/guide.html#Configuring_Log4J
>>
>>
>>
>>
>>
>>
>>
> ____________________________________________________________________________________
>> The all-new Yahoo! Mail beta
>> Fire up a more powerful email and get things done faster.
>> http://new.mail.yahoo.com
>>
>>
>
>
>
>
> ____________________________________________________________________________________
> Sponsored Link
>
> $420k for $1,399/mo.
> Think You Pay Too Much For Your Mortgage?
> Find Out! www.LowerMyBills.com/lre
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 




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


Mime
View raw message