Run Logback in Debug

Solution 1:

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

Solution 2:

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>

Solution 3:

You can set the status listener class via system property:

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

See: Logback manual

Solution 4:

I could not make it work using the chosen answer. However, the following worked:

java -Dlogback.configurationFile=/path/to/config-debug.xml com.domain.Main

Just add a file (config-debug.xml in this example) somewhere on your server and leave it there when you need to debug. Like the following.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{dd-MMM-yyyy HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Run your application using the afore mentioned -D parameter.

When things are back to normal, remove the -D parameter and restart your application.

Source: Chapter 3: Logback configuration