[logback-user] Want a new log every time application starts

Steve Cohen scohen at javactivity.org
Thu Mar 8 18:44:21 CET 2012


I want a new log file to be created every time an application starts.  I 
translate this wish into "roll the log upon the first log statement 
emitted by the application after it starts".

It seemed pretty trivial to implement this but I can't get it to work.

1. I created a policy:
==============================================================================
package com.whatever.logging;

import java.io.File;

import ch.qos.logback.core.rolling.TriggeringPolicyBase;

/**
  * This policy causes a log to be rolled over with the first log statement
  * of every program run.
  */
public class LogPerRunTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
	private static boolean rollMeOver = true;
	/* (non-Javadoc)
	 * @see 
ch.qos.logback.core.rolling.TriggeringPolicy#isTriggeringEvent(java.io.File, 
java.lang.Object)
	 */
	@Override
	public boolean isTriggeringEvent(File activeFile, E event) {
		boolean rollStatus = rollMeOver;
		if (rollMeOver) {
			System.out.println("rollover will be triggered");
			rollMeOver = false;
		}
		return rollStatus;
	}

}
=================================================================================
and an appender using that policy:

	<appender name="Conversation"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<File>${logdir}/conversation.log</File>
		<rollingPolicy 
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
			<fileNamePattern>conversation.%i.log</fileNamePattern>
			<minIndex>1</minIndex>
			<maxIndex>6</maxIndex>
		</rollingPolicy>
		<triggeringPolicy	 
class="com.whatever.logging.LogPerRunTriggeringPolicy" />
		<encoder>
			<pattern>%d [%t] %-5p %c{2} - %m%n</pattern>
		</encoder>
	</appender>
=================================================================================
But the log never rolls.  What am I doing wrong?








More information about the Logback-user mailing list