I can do some lazy initialization, but there is still a basic problem.

Looking at the source code for LogManager, I find that it has a static block of 
code. This static block will run as I try to do anything with LogManager. I can 
get it down to LogManager.setRepositorySelector(rs,guard) without trigerring 
the static block in LogManager. The mere call to setRepositorySelector runs the 
static block first, which runs code that calls 
OptionConverter.selectAndConfigure(...) unless log4j.defaultInitOverride is set.

So there is no way to prepare log4j before it initializes unless the user is 
kind enough to set log4j.defaultInitOverride or define log4j.configuratorClass. 
Both of these require the user - instead of the library handling the logging on 
behalf of the user - to setup the environment correctly and without conflicting 
with any other application.

Thank you,
Jay Turner

-----Original Message-----
From: Scott Deboy [mailto:scott.de...@gmail.com] 
Sent: Monday, July 18, 2011 5:47 PM
To: Log4J Users List
Subject: Re: How do I hook in before log4j starts?

Can you lazily initialize the ref to the root logger in your
LoggerRepositoryHandler instead of passing it in to the constructor?  It
seems like that should resolve your problem.

Scott

On Mon, Jul 18, 2011 at 3:02 PM, Turner, Jay
<Jay.Turner@sabre-holdin gs.com>wrote:

> The inherited code did have some "public static final
> org.apache.log4j.Logger ..." calls, thank you. Changing those allowed my
> code to be called first. However a Catch-22 still exists. Trying:
>
>    private static final Object guard = new Object();
>    private static final LoggerRepositoryHandler handler =
> setupLoggerRepositoryHandler();
>
>    private static LoggerRepositoryHandler setupLoggerRepositoryHandler() {
>        LoggerRepositoryHandler handle =
>            new
> LoggerRepositoryHandler(org.apache.log4j.LogManager.getLoggerRepository().getRootLogger());
>
>        org.apache.log4j.LogManager.setRepositorySelector(new
> RepositorySelector() {
>            public LoggerRepository getLoggerRepository() {
>                return handler;
>            }}, guard
>        );
>
>        return handle;
>    }
>
> This gets called first, but the
> org.apache.log4j.LogManager.getLoggerRepository() call to link my repository
> with the default repository invokes the log4j setup, which completes and
> uses the default repository before my new repository (with the
> parseUnrecognizedElement method) is setup. If I set the selector first then
> when I call getLoggerRepository() it will go through my selector which has
> no default repository setup.
>
> I can't get ahold of the log4j system to link in my
> parseUnrecognizedElement method first without letting log4j setup everything
> first.
>
> How has anyone every hooked parseUnrecognizedElement into log4j before it
> starts up?
>
> I just need to process multiple non-logger/appender-specific myKey=myValue
> parameters like this:
>
>    <?xml version="1.0" encoding="UTF-8"?>
>    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
>
>    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";
> debug="true">
>
>        <param name="myKey" value="myValue"/>
>
> Thank you,
> Jay Turner
>
> > From: Scott Deboy ... more likely a static logger declaration or a logger
> instance
> > be initialized prior to your configuration code being ran.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to