Return-Path: Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: (qmail 98654 invoked from network); 18 Nov 2006 03:30:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Nov 2006 03:30:21 -0000 Received: (qmail 17813 invoked by uid 500); 18 Nov 2006 03:30:18 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 17791 invoked by uid 500); 18 Nov 2006 03:30:17 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 17780 invoked by uid 99); 18 Nov 2006 03:30:17 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Nov 2006 19:30:17 -0800 X-ASF-Spam-Status: No, hits=0.3 required=10.0 tests=MAILTO_TO_SPAM_ADDR,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: domain of jak-tomcat-user@m.gmane.org designates 80.91.229.2 as permitted sender) Received: from [80.91.229.2] (HELO ciao.gmane.org) (80.91.229.2) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Nov 2006 19:30:04 -0800 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1GlGth-0000O5-QK for users@tomcat.apache.org; Sat, 18 Nov 2006 04:29:41 +0100 Received: from pool-71-107-249-55.lsanca.dsl-w.verizon.net ([71.107.249.55]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Nov 2006 04:29:41 +0100 Received: from wbarker by pool-71-107-249-55.lsanca.dsl-w.verizon.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Nov 2006 04:29:41 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: users@tomcat.apache.org From: "Bill Barker" Subject: Re: How to use logging in Tomcat correctly (declare Logger Static or not) ? Date: Fri, 17 Nov 2006 19:29:37 -0800 Lines: 124 Message-ID: References: <928711.20131.qm@web32812.mail.mud.yahoo.com> X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: pool-71-107-249-55.lsanca.dsl-w.verizon.net X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Sender: news X-Virus-Checked: Checked by ClamAV on apache.org "Mon Cab" 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