Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

logback

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

like image 590
Peter Mularien Avatar asked Sep 20 '13 17:09

Peter Mularien


People also ask

Which of the following file is used by Logback logging system?

properties file will be defined as properties in the logback. xml file.

How do I specify the path for Logback xml?

You may specify the location of the default configuration file with a system property named "logback. configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

What is Logback configuration?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations.

How do I read application properties in Logback xml?

xml , you can use <springProperty> to access properties from Spring's environment including those configured in application. properties . This is described in the documentation: The tag allows you to surface properties from the Spring Environment for use within Logback.


2 Answers

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.

like image 115
Michael R Avatar answered Oct 04 '22 07:10

Michael R


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.

like image 42
Sotirios Delimanolis Avatar answered Oct 04 '22 08:10

Sotirios Delimanolis