55

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414

6 Answers6

80

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 4
    Feature is "fixed" and is in logback 1.0.4. – David Roussel Jul 19 '12 at 10:05
  • 2
    To be absolutely clear, you can now enable Logback debugging with a system property, e.g. `-Dlogback.debug=true`. Via http://gordondickens.com/wordpress/2013/07/18/logback-config-showing-debug-level/. – Rich Dougherty Jun 29 '15 at 21:07
  • 1
    ...except that it is currently not possible to enable this if you're using Groovy configuration: [setting logback.debug property with groovy config throws exceptions when getting loggers](https://jira.qos.ch/browse/LOGBACK-1180). – ben3000 Jul 18 '17 at 07:52
7

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>
David Roussel
  • 5,788
  • 1
  • 30
  • 35
  • 10
    you can give the property a default value on your logback.xml in case it's not defined, like this: `` That way you don't need to set it onthe main method if not set. – Chirlo Jul 12 '12 at 08:29
  • Thanks I missed that. Now you mention it I can see it in the docs http://logback.qos.ch/manual/configuration.html#defaultValuesForVariables – David Roussel Jul 19 '12 at 10:04
  • Thank you for both comments. In Eclipse I can no set an environment variable in the run configuration to turn off logging when running mvn site, but have a default level of debug when running single tests in Eclipse. Thanks. – Gene De Lisa Jan 31 '14 at 23:32
  • If you like my answer, please up vote it. That's how this site works. :-) – David Roussel Feb 02 '14 at 09:02
2

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

Andres
  • 1,090
  • 14
  • 30
2

You can set the status listener class via system property:

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

See: Logback manual

deve
  • 487
  • 1
  • 6
  • 24
1

Well, It's pretty easy. Either you can use

log.level = debug

inside the application.properties of Spring boot.

or you can also set this in the configuration file of logback.xml

<root level="${log.level}">
    <appender-ref ref="ANY_APPENDER" />
</root>
-1

In eclipse you can have multiple run configurations. Open your main class. Go to Debug dropdown on eclipse toolbar and select Debug configurations. Click the New launch configuration icon at the top left. Give your launch configuration a better name. Click the Arguments tab under the name and enter -Dlog.level=debug or whatever you want. Click Close or Debug

You can do this again and specify -Dlog.level=warn for example.

AixNPanes
  • 1,170
  • 3
  • 14
  • 33