How can you find what URL was used in log4j default initialization?

java, log4j, logging

Solution

If you are willing to use AspectJ LTW (load-time weaving), you can take a look at the static initialisation of `LogManager` mentioned by Ian Roberts. In log4j 1.2.14 it looks like this:

static {
    // (...)
    // if there is no default init override, then get the resource
    // specified by the user or the default config file.
    if (override == null || "false".equalsIgnoreCase(override)) {
        // (...)
        URL url = null;

        // (...)    
        // If we have a non-null url, then delegate the rest of the
        // configuration to the OptionConverter.selectAndConfigure method.
        if (url != null) {
            LogLog.debug("Using URL [" + url + "] for automatic log4j configuration.");
            OptionConverter.selectAndConfigure(
                url, configuratorClassName, LogManager.getLoggerRepository()
            );
        } else {
            LogLog.debug("Could not find resource: [" + configurationOptionStr + "].");
        }
    }
}

Obviously if a default URL could be determined then `OptionConverter.selectAndConfigure(URL, ..)` will be called at one point within the static block in order to initialise log4j with that URL.

By means of AspectJ it is pretty simple to catch that method invocation:

import java.net.URL;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.LogManager;

public aspect Log4jAspect {
    before(URL defaultURL) :
        within(LogManager) &&
        cflow(staticinitialization(LogManager)) &&
        call(* OptionConverter.selectAndConfigure(URL, ..)) &&
        args(defaultURL, ..)
    {
        System.out.println("log4j default URL = " + defaultURL);
    }
}

In prose this code means:

- If we are within class LogManager and

- within the control flow of the static class initialisation and

- `OptionConverter.selectAndConfigure`is called,

- then capture the first argument (the URL) and

- print it to the console (you could just as well do something else).

If there is no default URL, nothing will be printed. Instead of printing the URL you could assign it to a static member of any class or whatever you like.

This is a solution for your problem, I tested it and it works. I would be happy to receive the bounty for answering your question, even though maybe the solution uses a technology you did not have in mind. But it solves the problem. :-)

Edit: It is also possible to explicitly intercept the log call in the case that no default URL is found, even though I do not think it is necessary. I just wanted to mention it.

Problem

Log4j default initialization goes through a procedure to find and use a URL to configure with. Afterward, how can you find out what URL was ultimately used, without having to code the same procedure yourself? (If you have to code it yourself, you might not get it exactly the same as log4j does, and also it might change in a future release.)

Original source