Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2148

CronTriggeringPolicy renaming behavior

    XMLWordPrintableJSON

Details

    • Question
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.8
    • None
    • Appenders
    • None

    Description

      Hi.
      I tried CronTriggeringPolicy(version 2.8) for save daily log files at midnight.

      My requirementare as follows:
      1) Log file should be of max size 30MB, beyond which new log file should be generated.
      2) Every midnight log file should be rolled over (to "bck" subfolder).
      3) The following filePattern for the rollovered log files: "l4j2_cpm-javascript-%d

      {yyyyMMdd-HHmmssSSS}

      .log.gz2"

      I have the followig Log4j2 configuration to achieve these requirements:

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="debug" monitorInterval="10">
      	<Properties>
      		<Property name="filePath">logs/l4j2_</Property>
      		<Property name="fileBackupPath">logs/bck/l4j2_</Property>
      		<Property name="fileSuffix">log.bz2</Property>
      		<Property name="fileDatePattern">yyyyMMdd-HHmmssSSS</Property>
      		<Property name="basicPatternLayout">%d %-5p [%t] - %m%n</Property>		
      	</Properties>
      	<Appenders>		
      		<!-- CPM - javascript log -->
      		<RollingFile name="CPMCommonJavaScriptAppender" fileName="${filePath}cpm-javascript.log"
      			filePattern="${fileBackupPath}cpm-javascript-%d{${fileDatePattern}}.${fileSuffix}"
      			createOnDemand="true">
      			<PatternLayout pattern="${basicPatternLayout}" />
      			<Policies>
      				<OnStartupTriggeringPolicy />
      				<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
      				<SizeBasedTriggeringPolicy size="30MB" />
      			</Policies>
      			<DefaultRolloverStrategy>
      				<Delete basePath="logs/bck" maxDepth="1">
      					<IfFileName glob="l4j2_cpm-javascript*.${fileSuffix}">
      						<IfAny>
      							<IfAccumulatedFileSize exceeds="200 MB" />
      							<IfAccumulatedFileCount exceeds="25" />
      						</IfAny>
      					</IfFileName>
      				</Delete>
      			</DefaultRolloverStrategy>
      		</RollingFile>		
      	</Appenders>
      	<Loggers>		
      		<Logger name="cpm.common.javascript" level="debug" additivity="false">
      			<AppenderRef ref="CPMCommonJavaScriptAppender" />
      		</Logger>		
      	</Loggers>
      </Configuration>
      

      But I have problem with file name in case that "CronTriggeringPolicy" is used. For example today is 2017/11/11:

      • 2017/11/11 23:52:15 Rollover for the file using "SizeBasedTriggeringPolicy" -> creates new file: l4j2_cpm-javascript-20171111-235215000.log.gz2.
      • 2017/11/12 00:00:00 Rollover for the file using "CronTriggeringPolicy" -> creates new file: l4j2_cpm-javascript-20171111-000000000.log.gz2.
        And this is the problem, because you have the file named "20171111-000000000" and it contains log records from the interval from 20171111-235215000 to 20171111-235959999. It looks like the first non-fixed value from "shedule" settings (for CronTriggeringPolicy) is decreased by 1. You can observe the same behaviour for rollover per minute -> schedule="0 0/1 * * * ?". In this case the log file generated for example at 2017/11/11 10:10:00 will be named ...20171111-100900..., etc.

      I have tried various combinations of filePattern values, policies and schedule parameter (for CronTriggeringPolicy) but have not been able to achieve correct file name. Please let me know where I am going wrong. Thanks for your help, I'm very appreciated it.

      Regards
      Petr

      Attachments

        1. FixedOnStartupTriggeringPolicy.java
          4 kB
          Wolff Bock von Wuelfingen
        2. FixedDefaultRolloverStrategy.java
          26 kB
          Wolff Bock von Wuelfingen
        3. FixedCronTriggeringPolicy.java
          6 kB
          Wolff Bock von Wuelfingen
        4. FileExtension.java
          4 kB
          Wolff Bock von Wuelfingen

        Activity

          People

            Unassigned Unassigned
            petr.valenta Petr Valenta
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated: