Peter Mularien
Peter Mularien

Reputation: 2638

How can I determine what log configuration source Logback actually used?

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).

Upvotes: 40

Views: 20660

Answers (4)

Frederic Close
Frederic Close

Reputation: 9639

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.

Upvotes: 10

Kenny Cason
Kenny Cason

Reputation: 12328

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.

Upvotes: 1

Michael R
Michael R

Reputation: 1813

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.

Upvotes: 43

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280175

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.

Upvotes: 32

Related Questions