22

Using log4j, how can I find out what is the name and path oft the current DOMConfigurator file log4j.xml is used, to reset the file name using the PropertyConfigurator.configureAndWatch method, which needs the name and path of this file to see if it has changed.

The API document shows me how to configure log4j to reload the config, but I cannot find a way to see the filename and path it picked up automatically. The application is running standalone withat any application server.

Thanks.

Manic
  • 221
  • 1
  • 2
  • 4
  • Possible duplicate of [Is it possible to make log4j display which file it used to configure itself?](https://stackoverflow.com/questions/3752921/is-it-possible-to-make-log4j-display-which-file-it-used-to-configure-itself) – Line Oct 17 '18 at 08:43

2 Answers2

27

I am afraid you have no chance to get the automatically picked up path from the API. As I understand log4j's source code the detected path will just be used and not be stored.

At least you can use the -Dlog4j.debug property to output log4j-internal debugging information on startup and you will get some information like this:

log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@11b86e7.
log4j: Using URL [file:/C:/develop/workspace/foobar/target/classes/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator

The 'log4j: Using URL ...' line comes from the LogManager class. You can check the initialization process from here. As I see the URL will not be stored for later information.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 1
    I use log4j from Eclipse and after adding this to .launch file I don't see any output similar to what you posted. – Line Oct 17 '18 at 08:26
  • 1
    @Line I am not sure about the .launch file (never have seen/used such file) but if you place it to the `VM arguments` section of the `Run configuration` it should work. – FrVaBe Oct 18 '18 at 16:53
  • I tried and it didn't. From what I understand, .launch file is representation of what is in Eclipse "configurations". – Line Oct 19 '18 at 07:48
  • @Line The Q&A is about the outdated [log4j 1](https://logging.apache.org/log4j/1.2/) framework. Maybe you are on [log4j 2](https://logging.apache.org/log4j/2.x/) already? – FrVaBe Oct 19 '18 at 09:03
  • I am. But it still worked on separate, test project. Just not on the one I care about :) – Line Oct 19 '18 at 09:32
2

You can use same process as log4j use on static initialization at LogManager class. Be aware of other initializations and external configurations e.g. from Spring's org.springframework.web.util.Log4jConfigListener.

  public static URL getLog4jConfig() {
    String override = OptionConverter.getSystemProperty("log4j.defaultInitOverride", null);
    if (override == null || "false".equalsIgnoreCase(override)) {
      String configurationOptionStr = OptionConverter.getSystemProperty("log4j.configuration", null);

      URL url;

      if (configurationOptionStr == null) {
        url = Loader.getResource("log4j.xml");
        if (url == null) {
          url = Loader.getResource("log4j.properties");
        }
      } else {
        try {
          url = new URL(configurationOptionStr);
        } catch (MalformedURLException ex) {
          url = Loader.getResource(configurationOptionStr);
        }
      }
      return url;
    } else {
      return null;
    }
  }
vklidu
  • 201
  • 2
  • 5
  • There is no `getSystemProperty` in `org.apache.logging.log4j.core.util.OptionConverter`. – Line Oct 17 '18 at 08:32