Multiple Projects Using Same Logger

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple Projects Using Same Logger

Aron Bock
Hi,

>I'm a new log4j user.  I want to use it in a j2ee web application--however,
>I want its
>components, packed in separate jars, to use the same logger(s), if
>possible.

I've since discovered that if Bar uses a non-static, local, logger, things
work as I want.  I don't know this is advisable, and shall continue
checking.

Thus my new logMessage() is:

private void logMessage(String msg, Level lvl) {
        Logger logger = Logger.getLogger(this.getClass().getName());
        if ( logger.isDebugEnabled()
                &&  
Logger.getRootLogger().getAllAppenders().hasMoreElements() )
            logger.log(lvl, msg);
        else
            System.out.println("sysout:" + msg );
}

The caveat here, I suppose, is that RootLogger will need an appender--even
if a do-nothing one.  I did read that there are [edge] cases in which the
configured-ness of non-root loggers does not bubble up to the root logger.  
Insight from those who've already been down this path would be much
appreciated.

( Incidentally, to elaborate on a prior post, the reason I use logMessage()
is because I expect to want to log context information as well, and I'd like
NDC push/pop to be isolated to and encapsulated in logMessage )

Regards,

--A

_________________________________________________________________
Don?t just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Multiple Projects Using Same Logger

Jacob Kjome
At 12:05 AM 5/22/2005 +0000, you wrote:
 >Hi,
 >
 >>I'm a new log4j user.  I want to use it in a j2ee web application--however,
 >>I want its
 >>components, packed in separate jars, to use the same logger(s), if
 >>possible.
 >
 >I've since discovered that if Bar uses a non-static, local, logger, things
 >work as I want.  I don't know this is advisable, and shall continue
 >checking.
 >
 >Thus my new logMessage() is:
 >
 >private void logMessage(String msg, Level lvl) {
 >        Logger logger = Logger.getLogger(this.getClass().getName());
 >        if ( logger.isDebugEnabled()
 >                &&
 >Logger.getRootLogger().getAllAppenders().hasMoreElements() )


Ok, well, first of all, you changed your code from before.  Now you are
getting appenders attached to the root logger, instead of the current local
logger.  Change one thing at a time.  I bet this code would work in your
previous case from your first email.  I think you are confused.  The change
of the logger  from static to a non-static local variable makes no
difference unless you are extending this class, in which case this
getClass().getName() could return different names.  It wouldn't have to be
a local variable, though.  An instance variable would work fine in that case.

Also, Whether a class was loaded from WEB-INF/classes or a jar in
WEB-INF/lib is totally and completely indistinguishable from Log4j's point
of view.  You either configure Log4j and all classes in the classloader use
the same configuration or you don't configure and no classes have a log4j
configuration.  One or the other, not classes in WEB-INF/classes have
configuration but classes in jar in WEB-INF/lib don't.  That's quite simply
impossible.  What matters is logger names.  As such, the check makes little
to no sense unless you are sometimes using this library in cases where you
do not configure Log4j at all.  But then why use Log4j in the first
place?  The whole point is to remove usage of System.out for
logging.  Well, I guess you have your own requirements, so I won't second
guess that this may simply be something you have to support.  Looks messy
to me, IMHO.

 >            logger.log(lvl, msg);
 >        else
 >            System.out.println("sysout:" + msg );
 >}
 >
 >The caveat here, I suppose, is that RootLogger will need an appender--even
 >if a do-nothing one.  I did read that there are [edge] cases in which the
 >configured-ness of non-root loggers does not bubble up to the root logger.
 >Insight from those who've already been down this path would be much
 >appreciated.
 >

See my previous email.  The isConfigured() utility method from
http://wiki.apache.org/logging-log4j/UsefulCode should help you out here.

 >( Incidentally, to elaborate on a prior post, the reason I use logMessage()
 >is because I expect to want to log context information as well, and I'd like
 >NDC push/pop to be isolated to and encapsulated in logMessage )
 >

Ok, that's actually a valid reason to have a separate message.  In the code
you posted, there was no reason at all.


Jake

 >Regards,
 >
 >--A
 >
 >_________________________________________________________________
 >Don't just search. Find. Check out the new MSN Search!
 >http://search.msn.click-url.com/go/onm00200636ave/direct/01/
 >
 >
 >---------------------------------------------------------------------
 >To unsubscribe, e-mail: [hidden email]
 >For additional commands, e-mail: [hidden email]
 >
 >  


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]