39

log4j has a property, log4j.debug, which will helpfully provide the user with an indication of which configuration file was actually used to configure the logging system.

I haven't been able to find anything equivalent with the (otherwise superior) Logback logging framework. Is there any way to print (for diagnostic purposes) at runtime, which configuration file Logback used to bootstrap itself?

[edit] To clarify, I'd ideally like a solution that doesn't require me to modify the configuration file itself (since a badly assembled third-party JAR, for example, may be picked up incorrectly, and prior to my logback configuration XML).

Peter Mularien
  • 2,578
  • 1
  • 25
  • 34

4 Answers4

42

You can set a Java system property to output Logback debugging info:

java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener

This is further explained by the Logback documentation for automatic status printing (very bottom mentions forcing status output) and the logback.statusListenerClass property:

In the absence of status messages, tracking down a rogue logback.xml configuration file can be difficult, especially in production where the application source cannot be easily modified. To help identify the location of a rogue configuration file, you can set a StatusListener via the "logback.statusListenerClass" system property (defined below) to force output of status messages. The "logback.statusListenerClass" system property can also be used to silence output automatically generated in case of errors.

Michael R
  • 1,753
  • 20
  • 18
30

If you want to go deep into Logback, you can do the following

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;

public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) throws Exception {
        LoggerContext loggerContext = ((ch.qos.logback.classic.Logger)logger).getLoggerContext();
        URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
        System.out.println(mainURL);
        // or even
        logger.info("Logback used '{}' as the configuration file.", mainURL);
    }
}

It will print the URL of the loaded configuration file.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I get `Logback used 'null' as the configuration file.` (maybe because this is a SpringBoot application?) – john k Apr 11 '22 at 15:57
  • @johnktejik That's possible. Spring Boot [seems to have a lot of logging overrides in its application.properties](https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-logging.html) – Sotirios Delimanolis Apr 11 '22 at 21:25
10

you can set debug="true" in a logback.xml file that you control like this:

<configuration debug="true">

(...)

</configuration

and tho make sure that file is going to be used by logback add following VM argument when you start your program:

-Dlogback.configurationFile=/path/to/yourlogback.xml

This does not really answer to your question but gives you a work around solution.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
Frederic Close
  • 9,389
  • 6
  • 56
  • 67
  • 1
    Thanks; however, this assumes that I know which logback.xml Logback was using! (a classic Catch-22). As indicated in the question, I may not in fact have access to the configuration file (for example, some badly-behaved 3rd party JAR may embed its own configuration). I'll clarify the original question to avoid confusion. – Peter Mularien Sep 20 '13 at 18:05
  • This is a good answer, and helpful in cases where the user knows where the configuration file is; however, in my case that is precisely the question I'm looking to answer :) Thank you for your time, though! – Peter Mularien Oct 04 '13 at 12:49
  • @PeterMularien would it not be better to know exactly where your logback configuration file is , which you can achieve by starting your application with following argument -Dlogback.configurationFile=/path/to/yourlogback.xml – Frederic Close Oct 04 '13 at 15:43
1

Not very scientific, but it works if you just want a quick confirmation.

I simply changed the log entry pattern and observed whether or not it changed in my console/log file.

Kenny Cason
  • 12,109
  • 11
  • 47
  • 72