Jacob, thank you for the response. Your prolificacy as evinced by the archives is borne out :-)

classes in jars that are put in WEB-INF/lib are run in exactly the same classloader as those in WEB-INF/classes. You need not do all the checking of appenders and such. In fact, you are probably making bad assumptions on behavior which is why you only ever see the System.out message. Simply do...

logger.info("log message");

I've confirmed that this 'auto discovery and configuration' works.

However, from my reading of the "short" version of the online manual, which states that "Under certain well-defined circumstances however, the static inializer of the Logger class will attempt to automatically configure log4j", but which subsequently severally exemplifies explicitly indicating the log4j configuration file, I came away with the idea that explicitly indicating the configuration file is preferable. Let's assume I prefer explicit indication.


No need to have the extra logMessage() method either. You are hardcoding Level.INFO, so why not just use logger.info() directly in getBar()?

Perhaps, but this shouldn't change working semantics, no?

If you see no output from this, then you haven't configured things properly.

I do see output--WEB-INF/classes use the logger; WEB-INF/lib doesn't. I'm trying to discover where I've erred.

BTW, are you doing anything special in your InitServlet? If not, just let autoconfiguration work for you. It will find log4j.properties and perform configuration in a static initializer by default. No need to do this manually unless you are doing something special. You are really over engineering this. KISS.

No, nothing special in the init servlet but, again, let's say I prefer explicitly indicating the config file to use. Imay need this dynamic ability later, and removing it is as simple as a web.xml edit.

The reason I first call logMessage() is so that it is the one "gateway" to logging (I would, of course, arrange for calls to it not to use the log4j-specific "Level" parameter). Also, remember that Bar is a separate "library", and I don't want its logs going to log4j when not used in a webapp. Hence the check for configuredness, etc. Lastly, I don't have my source files explicitly sprinkled with logging calls--calls to logging are woven in by AspectJ after the fact; again, AFAICT, this shouldn't change semantics.

I would like to KISS; but the S is relative :-)

So, in summary, I'm glad "autoconfigure" works, and it's a starting point for me to get to where I want to go. But I'm still unclear why the setup I described below doesn't work. I'd be grateful for any insight.

Regards,

--A


>I want its components, packed in separate jars, to use the same logger(s),
>if possible.  My configuration is shown below:
>
>j2ee web app
>=======
>    webroot/
>        WEB-INF/
>            classes/
>                log4jInitServlet (uses log4j props file)
>                log4j.properties
>                com.foo/ (classes in this package use logger)
>
>So far so good. I'm able to initialize a logger, and to log from any of the
>classes in "classes/".
>
>Now, I want to use this already-initialized logger to log from classes
>deployed in jar files in WEB-INF/lib/ .  Is this possible?  Thus far my
>attempts have been unsuccessful.
>
>For example, I have a separate java project, whose classes reside in package
>"com.bar", and which have been jar-ed and placed in WEB-INF/lib.  One of
>those classes is something like this:
>
>package com.bar;
>
>import org.apache.log4j.*;
>
>class Bar {
>    static Logger logger = Logger.getLogger(Bar.class.getName());
>
>    public void getBar() {
>        logMessage( "called getBar", Level.INFO);
>    }
>
>    private void logMessage(String msg, Level lvl) {
> if ( logger != null && logger.getAllAppenders().hasMoreElements() )
>            logger.log(lvl, msg);
>        else
>            System.out.println("sysout:" + msg + " : " + lvl.toString());
>    }
>}
>
>The idea is that Bar provides some functionality to the webapp, and I want >calls to it logged. Again, though, I'd like Bar's logging to use the same
>logger(s) already-initialised by the webapp.  My props file is as below:
>
>log4j.rootLogger=ERROR, A1
>log4j.appender.A1=org.apache.log4j.ConsoleAppender
>log4j.appender.A1.layout=org.apache.log4j.PatternLayout
>
># Set levels
>log4j.logger.com.foo=INFO
>log4j.logger.com.bar=DEBUG
>
>However, all log output from bar says "sysout", meaning it isn't using the
>logger.

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to