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.
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.
Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.
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.
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.
[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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With