[logback-user] Problem with sizeAndTimeBasedRollingPolicy

Ulrich Kriegel ulrich.kriegel at gmail.com
Wed Aug 7 05:33:59 UTC 2024


Hi there,

I tried to configure a SizeAndTimeBasedRollingPolicy programmatically (see below)
		Logger statisticsLogger = (Logger) LoggerFactory.getLogger("statistics"); 
		int MAX_HISTORY = 1095; 
		FileSize MAX_FILE_SIZE = FileSize.valueOf("250MB"); private static final String PATTERN = "%d %-5level %logger %msg%n";

		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

		String filename = "log/"+applicationContext.serviceGroupKey()+"-statistics.log";
		String rollingFilenamePattern="log/"+applicationContext.serviceGroupKey()+"statistics-%d{yyyy-MM-dd_HH}.log.gz";

		PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();

		patternLayoutEncoder.setPattern(PATTERN);
		patternLayoutEncoder.setContext(loggerContext);
		patternLayoutEncoder.start();

		RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
		rollingFileAppender.setFile(filename);
		rollingFileAppender.setName("statistics");
		rollingFileAppender.setEncoder(patternLayoutEncoder);
		rollingFileAppender.setContext(loggerContext);
		rollingFileAppender.setImmediateFlush(true);
		rollingFileAppender.setAppend(false);
		
		patternLayoutEncoder.setParent(rollingFileAppender);


		SizeAndTimeBasedRollingPolicy<Object> rollingFilePolicy = new SizeAndTimeBasedRollingPolicy<>();
//		TimeBasedRollingPolicy<Object> rollingFilePolicy = new TimeBasedRollingPolicy<>();
		rollingFilePolicy.setContext(loggerContext);
		rollingFilePolicy.setParent(rollingFileAppender);
		rollingFilePolicy.setFileNamePattern(rollingFilenamePattern);
		rollingFilePolicy.setMaxHistory(MAX_HISTORY);
		rollingFilePolicy.setMaxFileSize(MAX_FILE_SIZE); // comment out for TimeBasedRollingPolicy
		rollingFilePolicy.start();
		rollingFileAppender.setRollingPolicy(rollingFilePolicy);
		rollingFileAppender.start();

		statisticsLogger.addAppender(rollingFileAppender);
		statisticsLogger.setAdditive(false);

	
However, no logfile is produced. If the SizeAndTimeBasedRollingPolicy is replaced by a TimeBasedRollingPolicy, everything works fine.

What is my mistake.

Thanks in advance

- Ulrich


More information about the logback-user mailing list