Log4j2 Rolling appender - fileName "rolling" as shown

I am looking for a rollover strategy when the current log (the active output target in manual terminology) does not fix the file name, but the template is specified or, more precisely, the same template as in the attribute filePattern.

I want to get a daily survey where today log, say log-2015-05-05.log,, and in the midnight structure just stops writing it and starts writing in log-2015-05-06.log. However AFAIK, the current configuration only allows

<RollingFile name="ROLFILE"
    fileName="log.log"
    filePattern="log-%d{yyyy-MM-dd}.log"
>

Setting the same value in the attribute fileNamedoes not work (leads to the fact that the file with sensitive letters is interpreted literally). I have not noticed a single example or SO question with such a dynamic value fileName. Please note that this fileName="log-${date:yyyy-MM-dd}.log"does not solve the problem, because the expression is evaluated only at startup, and events are still sent to the file, even if their timestamp does not match the expression.

I am moving from Log4j 1.2 to Log4j 2.2. In the old version, the required behavior is possible with

<appender name="ROLFILE" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="log-%d{yyyy-MM-dd}.log" />
    </rollingPolicy>
    ...

I prefer to keep the current method as some log analysis tools rely on it. Is this possible in Log4j2? Thank.

+4
source share
4 answers

, , $$ . .

<RollingFile name="ROLFILE"
    fileName="log-$${date:yyyy-MM-dd}.log"
    filePattern="oldlog-%d{yyyy-MM-dd}.log"
>

, , , . ( "oldlog".)

0

, , . RollingFileManager. , , appender fileName, , .

<RollingFile name="ROLFILE"
    fileName="log-${date:yyyy-MM-dd}.log"
    filePattern="log-%d{yyyy-MM-dd}.log"
>
    <SlidingFilenameRolloverStrategy />
    ...

, . ( fileName AbstractManager MAP, - ​​, , , , .)

, , API RollingFileManager . javadoc, AFAIK , RollingFileAppender.

0

, , :

 fileName="log-${date:yyyy-MM-dd}.log"
 filePattern="log-%d{yyyy-MM-dd}.log"

log4j2 2.5

0

Log4j 2.8 (. issue LOG4J2-1101).

RollingFile, filename DirectWriteRolloverStrategy.

In addition, this function seems to have some problems with TimeBasedTriggeringPolicy(the first rollover does not occur, so each log file is shifted by one interval); CronTriggeringPolicyworks correctly.

Configuration Example:

<RollingRandomAccessFile name="MyLogger"
    filePattern="logs/application.%d{yyyy-MM-dd}.log">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    <Policies>
        <CronTriggeringPolicy schedule="0 * * * * ?" evaluateOnStartup="true"/>
    </Policies>
    <DirectWriteRolloverStrategy/>
</RollingRandomAccessFile>

RollingRandomAccessFileAppender support is requested in issue LOG4J2-1878 .

Edit: Changed in the CronTriggeringPolicy policy after detecting TimeBasedTriggeringPolicy. I had some problems.

-1
source

All Articles