log4j.xml configuration with and





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















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: http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz



Finally it worked for me only when I add:



try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}


to the method:



public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
String filename, long fileLength)


which make it works.



The question is if there is a better way to make it work?
since this method call many times and slow my program.



Here is the 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.
*
* <p>
* In configuration files, the <b>MaxFileSize</b> 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 >= maximumFileSize);

return result;
}

}


and the log4j.xml:



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
</layout>
</appender>

<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="file" value="C:/temp/test/test_log4j.log" />
<rollingPolicy class="com.mypack.rolling.CustomTimeBasedRollingPolicy">
<param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log" />
</rollingPolicy>
<triggeringPolicy class="com.mypack.rolling.CustomSizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="200KB" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
</layout>
</appender>

<logger name="com.mypack.myrun" additivity="false">
<level value="debug" />
<appender-ref ref="FILE" />
</logger>

<root>
<priority value="debug" />
<appender-ref ref="console" />
</root>

</log4j:configuration>









share|improve this question





























    7















    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: http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz



    Finally it worked for me only when I add:



    try {
    Thread.sleep(1);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }


    to the method:



    public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
    String filename, long fileLength)


    which make it works.



    The question is if there is a better way to make it work?
    since this method call many times and slow my program.



    Here is the 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.
    *
    * <p>
    * In configuration files, the <b>MaxFileSize</b> 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 >= maximumFileSize);

    return result;
    }

    }


    and the log4j.xml:



    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
    </layout>
    </appender>

    <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="file" value="C:/temp/test/test_log4j.log" />
    <rollingPolicy class="com.mypack.rolling.CustomTimeBasedRollingPolicy">
    <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log" />
    </rollingPolicy>
    <triggeringPolicy class="com.mypack.rolling.CustomSizeBasedTriggeringPolicy">
    <param name="MaxFileSize" value="200KB" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
    </layout>
    </appender>

    <logger name="com.mypack.myrun" additivity="false">
    <level value="debug" />
    <appender-ref ref="FILE" />
    </logger>

    <root>
    <priority value="debug" />
    <appender-ref ref="console" />
    </root>

    </log4j:configuration>









    share|improve this question

























      7












      7








      7


      3






      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: http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz



      Finally it worked for me only when I add:



      try {
      Thread.sleep(1);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }


      to the method:



      public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
      String filename, long fileLength)


      which make it works.



      The question is if there is a better way to make it work?
      since this method call many times and slow my program.



      Here is the 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.
      *
      * <p>
      * In configuration files, the <b>MaxFileSize</b> 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 >= maximumFileSize);

      return result;
      }

      }


      and the log4j.xml:



      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
      <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out" />
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
      </layout>
      </appender>

      <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
      <param name="file" value="C:/temp/test/test_log4j.log" />
      <rollingPolicy class="com.mypack.rolling.CustomTimeBasedRollingPolicy">
      <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log" />
      </rollingPolicy>
      <triggeringPolicy class="com.mypack.rolling.CustomSizeBasedTriggeringPolicy">
      <param name="MaxFileSize" value="200KB" />
      </triggeringPolicy>
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
      </layout>
      </appender>

      <logger name="com.mypack.myrun" additivity="false">
      <level value="debug" />
      <appender-ref ref="FILE" />
      </logger>

      <root>
      <priority value="debug" />
      <appender-ref ref="console" />
      </root>

      </log4j:configuration>









      share|improve this question














      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: http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz



      Finally it worked for me only when I add:



      try {
      Thread.sleep(1);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }


      to the method:



      public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
      String filename, long fileLength)


      which make it works.



      The question is if there is a better way to make it work?
      since this method call many times and slow my program.



      Here is the 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.
      *
      * <p>
      * In configuration files, the <b>MaxFileSize</b> 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 >= maximumFileSize);

      return result;
      }

      }


      and the log4j.xml:



      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
      <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out" />
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
      </layout>
      </appender>

      <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
      <param name="file" value="C:/temp/test/test_log4j.log" />
      <rollingPolicy class="com.mypack.rolling.CustomTimeBasedRollingPolicy">
      <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss}.log" />
      </rollingPolicy>
      <triggeringPolicy class="com.mypack.rolling.CustomSizeBasedTriggeringPolicy">
      <param name="MaxFileSize" value="200KB" />
      </triggeringPolicy>
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c -> %m%n" />
      </layout>
      </appender>

      <logger name="com.mypack.myrun" additivity="false">
      <level value="debug" />
      <appender-ref ref="FILE" />
      </logger>

      <root>
      <priority value="debug" />
      <appender-ref ref="console" />
      </root>

      </log4j:configuration>






      logging log4j






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 16 '11 at 14:43









      Mike SmithMike Smith

      36112




      36112
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If you add debug output to the method, you will see that the method is called very often, even after the trigger has already fired, but the file size is still growing larger than the maximum file size.



          I assume that the rolling behavior has some kind of buffer which is emptied before the actual (synchronous?) rollover takes place.



          I think it has something to do with the fileNamePattern in the com.mypack.rolling.CustomTimeBasedRollingPolicy. As long as the 'second' in the filename does not change, the CustomSizeBasedTriggeringPolicy.isTriggeringEvent method is continously called with amounts larger than the maximum file size.






          share|improve this answer































            0














            Thanks for your answer.



            I made 2 changes:

            1) I Add milliseconds to the file name pattern:



            <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss_SSS}.log" />  


            2) I Changed



            com.mypack.rolling.CustomSizeBasedTriggeringPolicy.isTriggeringEvent


            to



            public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
            String filename, long fileLength) {

            boolean result = (fileLength >= maximumFileSize);

            if (result) {
            try {
            Thread.sleep(1);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
            }

            return result;
            }


            so now, the Thread.sleep(1) is called only when a new file is created (rolled).






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4705957%2flog4j-xml-configuration-with-rollingpolicy-and-triggeringpolicy%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              If you add debug output to the method, you will see that the method is called very often, even after the trigger has already fired, but the file size is still growing larger than the maximum file size.



              I assume that the rolling behavior has some kind of buffer which is emptied before the actual (synchronous?) rollover takes place.



              I think it has something to do with the fileNamePattern in the com.mypack.rolling.CustomTimeBasedRollingPolicy. As long as the 'second' in the filename does not change, the CustomSizeBasedTriggeringPolicy.isTriggeringEvent method is continously called with amounts larger than the maximum file size.






              share|improve this answer




























                0














                If you add debug output to the method, you will see that the method is called very often, even after the trigger has already fired, but the file size is still growing larger than the maximum file size.



                I assume that the rolling behavior has some kind of buffer which is emptied before the actual (synchronous?) rollover takes place.



                I think it has something to do with the fileNamePattern in the com.mypack.rolling.CustomTimeBasedRollingPolicy. As long as the 'second' in the filename does not change, the CustomSizeBasedTriggeringPolicy.isTriggeringEvent method is continously called with amounts larger than the maximum file size.






                share|improve this answer


























                  0












                  0








                  0







                  If you add debug output to the method, you will see that the method is called very often, even after the trigger has already fired, but the file size is still growing larger than the maximum file size.



                  I assume that the rolling behavior has some kind of buffer which is emptied before the actual (synchronous?) rollover takes place.



                  I think it has something to do with the fileNamePattern in the com.mypack.rolling.CustomTimeBasedRollingPolicy. As long as the 'second' in the filename does not change, the CustomSizeBasedTriggeringPolicy.isTriggeringEvent method is continously called with amounts larger than the maximum file size.






                  share|improve this answer













                  If you add debug output to the method, you will see that the method is called very often, even after the trigger has already fired, but the file size is still growing larger than the maximum file size.



                  I assume that the rolling behavior has some kind of buffer which is emptied before the actual (synchronous?) rollover takes place.



                  I think it has something to do with the fileNamePattern in the com.mypack.rolling.CustomTimeBasedRollingPolicy. As long as the 'second' in the filename does not change, the CustomSizeBasedTriggeringPolicy.isTriggeringEvent method is continously called with amounts larger than the maximum file size.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 16 '11 at 16:47









                  mhallermhaller

                  13.1k13561




                  13.1k13561

























                      0














                      Thanks for your answer.



                      I made 2 changes:

                      1) I Add milliseconds to the file name pattern:



                      <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss_SSS}.log" />  


                      2) I Changed



                      com.mypack.rolling.CustomSizeBasedTriggeringPolicy.isTriggeringEvent


                      to



                      public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
                      String filename, long fileLength) {

                      boolean result = (fileLength >= maximumFileSize);

                      if (result) {
                      try {
                      Thread.sleep(1);
                      } catch (InterruptedException e) {
                      e.printStackTrace();
                      }
                      }

                      return result;
                      }


                      so now, the Thread.sleep(1) is called only when a new file is created (rolled).






                      share|improve this answer




























                        0














                        Thanks for your answer.



                        I made 2 changes:

                        1) I Add milliseconds to the file name pattern:



                        <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss_SSS}.log" />  


                        2) I Changed



                        com.mypack.rolling.CustomSizeBasedTriggeringPolicy.isTriggeringEvent


                        to



                        public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
                        String filename, long fileLength) {

                        boolean result = (fileLength >= maximumFileSize);

                        if (result) {
                        try {
                        Thread.sleep(1);
                        } catch (InterruptedException e) {
                        e.printStackTrace();
                        }
                        }

                        return result;
                        }


                        so now, the Thread.sleep(1) is called only when a new file is created (rolled).






                        share|improve this answer


























                          0












                          0








                          0







                          Thanks for your answer.



                          I made 2 changes:

                          1) I Add milliseconds to the file name pattern:



                          <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss_SSS}.log" />  


                          2) I Changed



                          com.mypack.rolling.CustomSizeBasedTriggeringPolicy.isTriggeringEvent


                          to



                          public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
                          String filename, long fileLength) {

                          boolean result = (fileLength >= maximumFileSize);

                          if (result) {
                          try {
                          Thread.sleep(1);
                          } catch (InterruptedException e) {
                          e.printStackTrace();
                          }
                          }

                          return result;
                          }


                          so now, the Thread.sleep(1) is called only when a new file is created (rolled).






                          share|improve this answer













                          Thanks for your answer.



                          I made 2 changes:

                          1) I Add milliseconds to the file name pattern:



                          <param name="fileNamePattern" value="C:/temp/test/test_log4j-%d{yyyy-MM-dd-HH_mm_ss_SSS}.log" />  


                          2) I Changed



                          com.mypack.rolling.CustomSizeBasedTriggeringPolicy.isTriggeringEvent


                          to



                          public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
                          String filename, long fileLength) {

                          boolean result = (fileLength >= maximumFileSize);

                          if (result) {
                          try {
                          Thread.sleep(1);
                          } catch (InterruptedException e) {
                          e.printStackTrace();
                          }
                          }

                          return result;
                          }


                          so now, the Thread.sleep(1) is called only when a new file is created (rolled).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 17 '11 at 9:06









                          Mike SmithMike Smith

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4705957%2flog4j-xml-configuration-with-rollingpolicy-and-triggeringpolicy%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              Npm cannot find a required file even through it is in the searched directory