Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1393

The ch.qos.logback.core.rolling.helper.FileFilterUtil.afterLastSlash function may caused NPE problem



      I debug the logback codes and found the afterLastSlash function may caused NullPointException . Because of the function as follows.

      public static String afterLastSlash(String sregex) {
                int i = sregex.lastIndexOf('/');
                if (i == -1) { 
                return sregex;
               } else {
               return sregex.substring(i + 1);

      if the "sregex" is a null String . The function will throw the exception and the program logic will be interrupted .

      I will show your this case how to happed . 

      I create the a spring-boot project use the logback as log framework . The logback configuration is 

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration debug="false">
      <property name="LOG_HOME" value="log" />
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
      <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
      <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

      <logger name="com.mindsphere.china.poc.connectivity" level="DEBUG" additivity="false">
      <appender-ref ref="STDOUT" />
      <appender-ref ref="FILE" />

      <root level="ERROR">
      <appender-ref ref="STDOUT" />
      <appender-ref ref="FILE" />


      The program write the log into the file until the file size is small than 10MB ,When the log file size is more than 10MB . The logback continue write log will trigger the SizeBasedTriggeringPolicy . The TimeBasedRollingPolicy class will call the rollover() function . 

      public void rollover() throws RolloverFailure {

      // when rollover is called the elapsed period's file has
      // been already closed. This is a working assumption of this method.

      String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();

      String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);

      but the function timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName() return null . So the program could be broken and lose the logs.



      I have choose another way to configuration the logback.xml and get the right result .But I think this is still a issues .So if you still want to have more details can connect will me by emails jbjiangbiao@icloud.com






            • Assignee:
              logback-dev Logback dev list
              JIANGBIAO Jiang Biao
            • Votes:
              0 Vote for this issue
              1 Start watching this issue


              • Created: