struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gaurav.Go...@ercgroup.com
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,
Gaurav

-----Original Message-----
From: Eddie Bush [mailto:ekbush@swbell.net]
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:

-- commons-logging.properties -- (this is in the top of my 
WEB-INF/classes directory)
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jF
actory

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

# A1 is set to be a ConsoleAppender.
# You could easily change it to a different sort.  I just did this for a 
test
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.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:

log4j.logger.com.mycompany.MyClass=FATAL
-or-
log4j.logger.com.mycompany.MyClass=DEBUG

... in your log4j.properties file.

Regards,

Eddie

Gaurav.Goyal@ercgroup.com 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
do
>the PropertyConfigurator.configure in some kind of an initializer servlet.
>Is my understanding right?
>
>-----Original Message-----
>From: Eddie Bush [mailto:ekbush@swbell.net]
>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 
>available.
>
>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 
>instantiated
>        - 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
>
>Regards,
>
>Eddie
>
>Gaurav.Goyal@ercgroup.com wrote:
>
>>Hello,
>>	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
static
>>Log handle which probably can be accessed globally to log messages. Should
>>
>i
>
>>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
>>
>beta
>
>>with iPlanet WS 6 (different story!).
>>
>>Thanks,
>>Gaurav
>>
>
>
>
>--
>To unsubscribe, e-mail:
><mailto:struts-user-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail:
><mailto:struts-user-help@jakarta.apache.org>
>
>--
>To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>
>



--
To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>

--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message