title: log4j.xml configuration with <rollingPolicy> and <triggeringPolicy>
body: <p>I try to configure log4j.xml in such a way that file will be rolled upon file size, and the rolled file's name will be i.e: "C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log"
  I followed this discussion: <a href="http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz" rel="noreferrer">http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz</a></p>
  
  <p>Finally it worked for me only when I add:   </p>
  
  <pre><code>try {
     Thread.sleep(1);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
  </code></pre>
  
  <p>to the method:  </p>
  
  <pre><code>public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
              String filename, long fileLength)  
  </code></pre>
  
  <p>which make it works.</p>
  
  <p>The question is if there is a better way to make it work?
  since this method call many times and slow my program.</p>
  
  <p>Here is the code:  </p>
  
  <pre><code>package com.mypack.rolling;
  
  import org.apache.log4j.rolling.RollingPolicy;
  import org.apache.log4j.rolling.RolloverDescription;
  import org.apache.log4j.rolling.TimeBasedRollingPolicy;
  
  /**
   * Same as org.apache.log4j.rolling.TimeBasedRollingPolicy but acts only as
   * RollingPolicy and NOT as TriggeringPolicy.
   *
   * This allows us to combine this class with a size-based triggering policy
   * (decision to roll based on size, name of rolled files based on time)
   *
   */
  public class CustomTimeBasedRollingPolicy implements RollingPolicy {
  
   TimeBasedRollingPolicy timeBasedRollingPolicy = new TimeBasedRollingPolicy();
  
   /**
    * Set file name pattern.
    * @param fnp file name pattern.
    */
   public void setFileNamePattern(String fnp) {
    timeBasedRollingPolicy.setFileNamePattern(fnp);
   }
   /*
   public void setActiveFileName(String fnp) {
    timeBasedRollingPolicy.setActiveFileName(fnp);
   }*/
  
   /**
    * Get file name pattern.
    * @return file name pattern.
    */
   public String getFileNamePattern() {
    return timeBasedRollingPolicy.getFileNamePattern();
   }
  
   public RolloverDescription initialize(String file, boolean append) throws SecurityException {
    return timeBasedRollingPolicy.initialize(file, append);
   }
  
   public RolloverDescription rollover(String activeFile) throws SecurityException {
    return timeBasedRollingPolicy.rollover(activeFile);
   }
  
   public void activateOptions() {
    timeBasedRollingPolicy.activateOptions();
   }
  }
  
  
  
  package com.mypack.rolling;
  
  import org.apache.log4j.helpers.OptionConverter;
  import org.apache.log4j.Appender;
  import org.apache.log4j.rolling.TriggeringPolicy;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.spi.OptionHandler;
  
  /**
   * Copy of org.apache.log4j.rolling.SizeBasedTriggeringPolicy but able to accept
   * a human-friendly value for maximumFileSize, eg. "10MB"
   * 
   * Note that sub-classing SizeBasedTriggeringPolicy is not possible because that
   * class is final
   */
  public class CustomSizeBasedTriggeringPolicy implements TriggeringPolicy, OptionHandler {
  
   /**
    * Rollover threshold size in bytes.
    */
   private long maximumFileSize = 10 * 1024 * 1024; // let 10 MB the default max size
  
   /**
    * Set the maximum size that the output file is allowed to reach before
    * being rolled over to backup files.
    * 
    * &lt;p&gt;
    * In configuration files, the &lt;b&gt;MaxFileSize&lt;/b&gt; option takes an long
    * integer in the range 0 - 2^63. You can specify the value with the
    * suffixes "KB", "MB" or "GB" so that the integer is interpreted being
    * expressed respectively in kilobytes, megabytes or gigabytes. For example,
    * the value "10KB" will be interpreted as 10240.
    * 
    * @param value
    *            the maximum size that the output file is allowed to reach
    */
   public void setMaxFileSize(String value) {
    maximumFileSize = OptionConverter.toFileSize(value, maximumFileSize + 1);
   }
  
   public long getMaximumFileSize() {
    return maximumFileSize;
   }
  
   public void setMaximumFileSize(long maximumFileSize) {
    this.maximumFileSize = maximumFileSize;
   }
  
   public void activateOptions() {
   }
  
   public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
              String filename, long fileLength) {
    try {
     Thread.sleep(1);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
  
    boolean result = (fileLength &gt;= maximumFileSize);
  
    return result;
   }
  
  }
  </code></pre>
  
  <p>and the log4j.xml:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
  
  &lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true"&gt;
   &lt;appender name="console" class="org.apache.log4j.ConsoleAppender"&gt;
    &lt;param name="Target" value="System.out" /&gt;
    &lt;layout class="org.apache.log4j.PatternLayout"&gt;
     &lt;param name="ConversionPattern" value="%d [%t] %-5p %c -&gt; %m%n" /&gt;
    &lt;/layout&gt;
   &lt;/appender&gt;
  
   &lt;appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender"&gt;
    &lt;param name="file" value="C:/temp/test/test_log4j.log" /&gt;
    &lt;rollingPolicy class="com.mypack.rolling.CustomTimeBasedRollingPolicy"&gt;
     &lt;param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log" /&gt;
    &lt;/rollingPolicy&gt;
    &lt;triggeringPolicy class="com.mypack.rolling.CustomSizeBasedTriggeringPolicy"&gt;
     &lt;param name="MaxFileSize" value="200KB" /&gt;
    &lt;/triggeringPolicy&gt;
    &lt;layout class="org.apache.log4j.PatternLayout"&gt;
     &lt;param name="ConversionPattern" value="%d [%t] %-5p %c -&gt; %m%n" /&gt;
    &lt;/layout&gt;
   &lt;/appender&gt;
  
   &lt;logger name="com.mypack.myrun" additivity="false"&gt;
    &lt;level value="debug" /&gt;
    &lt;appender-ref ref="FILE" /&gt;
   &lt;/logger&gt;
  
   &lt;root&gt;
    &lt;priority value="debug" /&gt;
    &lt;appender-ref ref="console" /&gt;
   &lt;/root&gt;
  
  &lt;/log4j:configuration&gt;
  </code></pre>