Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Logback in Debug

I've recently switched from log4j to logback and am wondering if there is an easy way to run logback in debug mode, similar to log4j's log4j.debug property. I need to see where it is picking up my logback.xml from.

The docs mention using a StatusPrinter to print out logback's internal status, but that would require code changes.

like image 717
dogbane Avatar asked Sep 27 '10 08:09

dogbane


People also ask

Where do I put the Logback file?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

Does slf4j use Logback?

Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.

Does log4j use Logback?

Logback uses the same concepts as Log4j. So it's no surprise that even if they are using different file formats, their configurations are very similar. The following code snippet shows the same configuration as I used with Log4j.

How do I use Logback xml in Spring boot?

To configure Logback for a Spring Boot project via XML, create the logback. xml or logback-spring. xml file under the src/main/resources folder. The configuration in XML file will override the logging properties in the application.


2 Answers

[EDIT]

This has been fixed in Logback 1.0.4. You can now use -Dlogback.debug=true to enable debugging of the logback setup.

-- Old Answer --

Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback.xml. Please submit a feature request.

like image 74
Aaron Digulla Avatar answered Sep 23 '22 14:09

Aaron Digulla


This is how I do it. I set a system property called 'log.level', then I reference it in logback.xml.

Edit: The downside is that you MUST have 'log.level' always set. The way I deal with this is to check in my main method and set it to INFO if not already set, be sure to do this before you first logging calls. Then I can override on the command line, and have a sensible default.

Here is how it looks in my logback.xml:

<configuration>     <logger name="com.mycompany.project" level="${log.level}" />     <logger name="httpclient" level="WARN" />     <logger name="org.apache" level="WARN" />     <logger name="org.hibernate" level="WARN" />     <logger name="org.hibernate.cfg.AnnotationBinder" level="WARN" />     <logger name="org.hibernate.cfg.annotations" level="WARN" />     <logger name="org.quartz" level="WARN" />     <logger name="org.springframework" level="WARN" />      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-16thread] %-5level %-35.35logger{30} - %msg%n</pattern>         </encoder>     </appender>     <root level="${log.level:-INFO}">         <appender-ref ref="STDOUT" />     </root> </configuration> 
like image 20
David Roussel Avatar answered Sep 24 '22 14:09

David Roussel