Benjamin Asbach created LOG4J2-2883:
---------------------------------------

             Summary: Size based rolling stops working after first time base 
rolling
                 Key: LOG4J2-2883
                 URL: https://issues.apache.org/jira/browse/LOG4J2-2883
             Project: Log4j 2
          Issue Type: Bug
    Affects Versions: 2.13.3
            Reporter: Benjamin Asbach


When configuring a logger with time based rolling and size based rolling. The 
size based rolling fails after the first time based rolling.

Note: This is slightly related to 
[LOG4J2-2875|https://issues.apache.org/jira/browse/LOG4J2-2875]. With no 
modifications the sample code below shows the problem, but also IOExceptions 
occur which seems not to be related to this issue.

{code:java}
import java.awt.Desktop;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.util.Arrays.asList;
import static java.util.Collections.sort;
import java.util.List;
import java.util.Properties;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import 
org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class RollingTest {

  @Test
  public void sizeBasedRollingBrokenAfterTimeBasedRolling() throws Exception {
    System.setProperty("log4j2.debug", "");

    Path tempDirectory = Files.createTempDirectory("log4j2");
    System.out.println(tempDirectory);

    Properties log4j2Properties = new Properties();
    log4j2Properties.put("appender.localmsglog.type", "RollingFile");
    log4j2Properties.put("appender.localmsglog.name", "localmsglog");
    log4j2Properties.put("appender.localmsglog.filePattern", 
tempDirectory.toFile().getAbsolutePath() + 
"/%d{yyyy_MM_dd_mm}/messages-$${date:yyyy_MM_dd_HH_mm_ss_S}.log");
    log4j2Properties.put("appender.localmsglog.policies.type", "Policies");
    log4j2Properties.put("appender.localmsglog.policies.1.type", 
"SizeBasedTriggeringPolicy");
    log4j2Properties.put("appender.localmsglog.policies.1.size", "10KB");
    log4j2Properties.put("appender.localmsglog.policies.2.type", 
"TimeBasedTriggeringPolicy");

    log4j2Properties.put("rootLogger.level", "info");
    log4j2Properties.put("rootLogger.appenderRef.1.ref", "localmsglog");

    PropertiesConfiguration config = new PropertiesConfigurationBuilder()
            .setRootProperties(log4j2Properties)
            .build();

    System.out.println("Test directory: " + tempDirectory.toString());
    Desktop.getDesktop().open(tempDirectory.toFile());
    org.apache.logging.log4j.core.config.Configurator.reconfigure(config);

    LoggerContext context = LoggerContext.getContext(false);
    Logger log = context.getRootLogger();

    for (int i = 0; i < 1000; i++) {
      log.info("nHq6p9kgfvWfjzDRYbZp");
    }
    Thread.sleep(60000);
    for (int i = 0; i < 1000; i++) {
      log.info("nHq6p9kgfvWfjzDRYbZp");
    }

    File tempDirectoryAsFile = tempDirectory.toFile();
    List<File> loggingFolders = asList(tempDirectoryAsFile.listFiles());
    sort(loggingFolders);
    // Check if two folders were created
    assertEquals(2, loggingFolders.size());
    
    // Check if first folder contains 3 log files
    assertEquals(3, loggingFolders.get(0).listFiles().length);

    // Check if second folder contains 3 log files as well
    assertEquals(3, loggingFolders.get(1).listFiles().length);
  }
}
{code}




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to