struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject RE: Logging in Business Classes
Date Mon, 16 Sep 2002 16:37:29 GMT
Eddie, Just tried out the approach you suggested for logging and it works
like a charm. Thanks for the guidance. I had a tough time figuring this out
from the log4j user guide.

Thanks Again,

-----Original Message-----
From: Eddie Bush []
Sent: Saturday, September 14, 2002 11:36 PM
To: Struts Users Mailing List
Subject: Re: Logging in Business Classes

Make sure you're not using the commons-logging.jar out of any of the 1.1 
beta packages.  I spent a great deal of time fiddling with this tonight 
so I could give you an answer, and that's why it took me so danged long. 
 I'd recommend grabbing a copy right from the commons before you get 
started good on this -- save yourself some time.

I'm using the jdk1.4 logger by default, since it ... "just works" for me 
:-)  You want to use log4j.  Here's what I did:

-- -- (this is in the top of my 
WEB-INF/classes directory)

-- -- (also in top-level classes directory)
# Only show me INFO and above (suppress DEBUG) messages for org.apache
# I don't care what the commons is doing unless it's really bad

# A1 is set to be a ConsoleAppender.
# You could easily change it to a different sort.  I just did this for a 

# A1 uses PatternLayout.
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

That's all I had to do for configuration.  So far as your classes go, 
you'll want to do something like the following in each of them:

package com.mycompany;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MyClass
    private static Log log = LogFactory.getLog(MyClass.class);
    public void myFunction(...)
        if (log.isDebugEnabled())
            log.debug("Hi!  Did you think you'd ever get this to work?!");

You could then control the output (like I did for struts/commons stuff 
above) by doing:

... in your file.


Eddie wrote:

>Thanks for the insight.
>"Your business objects shouldn't know a thing about where they're 
>being used" makes a lot of sense.
>Also had a quick look at log4j user's guide. Doesn't seem too difficult to
>use. So i guess all i need to do is set the system property
>org.apache.commons.logging.LogFactory to org.apache.log4j.Logger and then
>the PropertyConfigurator.configure in some kind of an initializer servlet.
>Is my understanding right?
>-----Original Message-----
>From: Eddie Bush []
>Sent: Saturday, September 14, 2002 6:39 PM
>To: Struts Users Mailing List
>Subject: Re: Logging in Business Classes
>I know where you're going with this, and I wouldn't do it.  Don't 
>let/make your business objects tie themselves to Struts.
>Suggestion:  Instantiate your own class-level logger in each of your 
>classes (class-static member -- private + no setter or getter)
>If you build your logging upon the commons-logging that struts uses 
>(which would be fine -- hey, it's there, use it) you'll have the ability 
>to use either Log4J -or- the JDK1.4 logger.  That's what commons logging 
>does -- give you a generic interface so that it doesn't matter which is 
>There are a myriad of reasons for doing things as I've suggested.  A 
>couple of them are:
>    - Your business objects shouldn't know a thing about where they're 
>being used
>    - The name of the logger is typically the name of the class it is 
>        - using one of the struts loggers would not give clear 
>indication where the logging came from
>        - using a class-level logger will tell you exactly which class 
>the logging came from
> wrote:
>>	Does anyone know how to send messages to the log in business classes
>>in Struts 1.0.2. I noticed that in Struts 1.1 b2 ActionServlet has a
>>Log handle which probably can be accessed globally to log messages. Should
>>extend or modify my ActionServlet to have a static log handle? Has anyone
>>done something like this? Unfortunately I am not able to use Struts 1.1
>>with iPlanet WS 6 (different story!).
>To unsubscribe, e-mail:
>For additional commands, e-mail:
>To unsubscribe, e-mail:
>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message