What determines when log4j will flip TimeBasedRollingPolicy?

I am setting up TimeBaseRollingPolicy from additional Log4J applications, and I donโ€™t understand what the transition policy says. The API is not explicit, so I'm just drawing conclusions. It seems that the last element in FileNamePattern determines the frequency.

Take this example from the log4j Wiki :

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender"> <!-- The active file to log to --> <param name="file" value="/applogs/myportal/portal.log" /> <param name="append" value="true" /> <param name="encoding" value="UTF-8" /> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <!-- The file to roll to, this is a fairly intelligent parameter, if the file ends in .gz, it gzips it, based on the date stamp it rolls at that time, default is yyyy-MM-dd, (rolls at midnight) --> <param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" /> </rollingPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" /> </layout> </appender> 

Can we assume that since the pattern ends with dd, the policy should roll when it changes? The same with the example in the API, the "yyyy-MM" template means that the file should roll when the MM changes?

Thanks!

Floor

+4
source share
2 answers

Well, I would have to double check, but I would say that whenever a String created by formatting the current date with a change in the format string jumps. This means: if you format the date using "yyyy-MM-dd", the result will change every day. This will also happen only with "dd", BUT you will get the same file name every month, so the files will either be overwritten or added, or it will fail because the file already exists (not sure if this is true, depends on what the appender is doing, I think in this case the logs will be added, except maybe for gzip).

Edit:

Example: if you have mylog.%d{dd}.log , the final log file for today (2011-03-27) is named mylog.25.log (due to the formatting of the new date () when logging) and will add messages to this file. Tomorrow, the file in use will now be named mylog.26.log . On April 25th you will again get the file name `mylog.25.log, therefore all logs from this day will be added to the file that already contains logs from March 25th.

+3
source

I got this job. Below is the code for log4j. You only need to add the log4j-extras dependencies to pom.xml and use the code below: The code below loads every minute and creates a ZIP file.

 <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="/opt/app/srdotcom/logs/portal.%d{yyyy-MM-dd-HH-mm}.log.zip" /> </rollingPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss:SSS z}| %c{2}| %m%n" /> </layout> 

Maven Dependencies:

  <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.2.17</version> </dependency> 
0
source

Source: https://habr.com/ru/post/1345240/


All Articles