Skip to content

Logback debug

  • by

1. Overview

In this article, we will learn the LogBack debug feature.

Assume your application has stopped writing to your log file, and you have made no changes recently. You had to troubleshoot and find the root cause to fix it.

In the first place, you had to figure out what is going on within the LogBack library. So you need to see the logs of your Log Back library files in your console.

In this article, we will discuss the various ways to enable those logs in the console.

2. Logback debug

The Log Back automatically displays its internal logs on the console whenever there is any warning or error during the parsing of the configuration file.

However, you can use the below code to get the logs every time even without the presence of error or warning while parsing the configuration.

public static void main(String[] args) {
  LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  StatusPrinter.print(lc);
}

But errors can also appear post configuration, meaning when application is running. As mentioned, there are other possible scenarios like application stopped writing to log file, disk is full or log files not archived. 

Now let’s see to get the logs while app is running.

3. LogBack debug using StatusListener

You can use Status Listener to print the internal logs during configuration and also post-configuration i.e., while application is running.

The below configuration script uses the <statusListener/> element to print the logs on the console.

<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <!-- rollover daily -->
      <fileNamePattern>${LOG_PATH}output-%d{yyyy-MM-dd-HH-mm}.%i.txt</fileNamePattern>
       <maxFileSize>50KB</maxFileSize>    
       <maxHistory>60</maxHistory>
       <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>


  <root level="DEBUG">
    <appender-ref ref="ROLLING" />
  </root>

</configuration>

Following are the LogBack library logs printed on the console.

StatusListner works only when the following conditions are met:

  1. availability of the configuration file
  2. well-formed configuration file.

If the Log Back finds a malformed configuration file, then it automatically displays its internal status on the console.

However, if the configuration file not found, then the Log Back will not print its internal status logs. Note that this is not an error condition. Log Back doesn’t have any idea on the configuration file or status listener in the first place.  

Let’s discuss the below status listeners available for us.

3.1. OnConsoleStatusListener

This listener will display the Log Back library log messages in the console as below:

21:40:54,594 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
21:40:54,594 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [ROLLING]
21:40:54,596 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@236567414 - setting totalSizeCap to 20 GB
21:40:54,597 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@236567414 - Archive files will be limited to [50 KB] each.
21:40:54,597 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@236567414 - No compression will be used
21:40:54,597 |-INFO in c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@236567414 - Will use the pattern D:/log/output-%d{yyyy-MM-dd-HH-mm}.%i.txt for the active file
21:40:54,598 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@512535ff - The date pattern is 'yyyy-MM-dd-HH-mm' from file name pattern 'D:/log/output-%d{yyyy-MM-dd-HH-mm}.%i.txt'.
21:40:54,598 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@512535ff - Roll-over every minute.
21:40:54,613 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@512535ff - Setting initial period to Tue Sep 28 11:18:03 IST 2021
21:40:54,614 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
21:40:54,615 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING] - Active log file name: D:\log\spring.log
21:40:54,615 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLING] - File property is set to [D:\log\spring.log]
21:40:54,615 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
21:40:54,615 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [ROLLING] to Logger[ROOT]
21:40:54,615 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

21:40:54,698 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [D:\log\spring.log] to [D:\log\output-2021-09-28-11-18.0.txt]
21:40:54,699 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - first clean up after appender initialization
21:40:54,699 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Multiple periods, i.e. 336 periods, seem to have elapsed. This is expected at application start.
21:40:54,797 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Removed  0 Bytes of files

3.2. OnErrorConsoleStatusListener

 This status listener will display the Log Back internal log statements on System.err as below:

OnErrorStatusListener System.err
OnErrorStatusListener System.err

3.3. NopStatusListener

This status listener will not display any Log Back library log statements. You may wonder why it is required in first place.

We have previously mentioned that our Log Back automatically prints the error that appears during the configuration. When you register the status listener, the automatic logging is disabled.

Thus, by setting NopStatusListener as a status listener, you can avoid internal log statements printing altogether.

4. LogBack debug XML configuration

If you are using the XML configuration script in your project, then you can enable the StatusListener easily by adding the debug attribute with value true in the configuration element:

<configuration debug="true">
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <!-- rollover daily -->
      <fileNamePattern>${LOG_PATH}output-%d{yyyy-MM-dd-HH-mm}.%i.txt</fileNamePattern>
       <maxFileSize>50KB</maxFileSize>    
       <maxHistory>60</maxHistory>
       <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>


  <root level="DEBUG">
    <appender-ref ref="ROLLING" />
  </root>

</configuration>

This is strictly equivalent to adding the OnConsoleStatusListener in the configuration script.

5. LogBack debug using VM arguments

You can run Log Back in debugging mode by using the VM argument -Dlogback.debug=true.

LogBack debug using VM arguments
LogBack debug using VM arguments

6. Enable LogBack debugging using system property

You can register a status listener by setting the "logback.statusListenerClass" Java system property with the name of the listener class you may wish to register.

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

7. Enable log programmatically

You can collect the logs programmatically as well:

public static void main(String[] args) throws JoranException {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    StatusManager statusManager = lc.getStatusManager();
    OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
    statusManager.add(onConsoleListener);
  
    Logger logger = LoggerFactory.getLogger("myApp");
    logger.info("Entering application.");

    logger.info("Exiting application.");
}

This only prints the log messages following your configuration.

8. Conclusion

To sum up, we have learned the different ways of enabling the Log Back debugging mode.

Leave a Reply

Your email address will not be published. Required fields are marked *