[ 
https://issues.apache.org/jira/browse/LOG4J2-1204?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17314703#comment-17314703
 ] 

Sergei Gorbunov edited comment on LOG4J2-1204 at 4/5/21, 7:55 AM:
------------------------------------------------------------------

I mean an index used in filePattern attirbute of RollingFile element (%i):
{code}
filePattern - The pattern of the file name of the archived log file. The format 
of the pattern should is dependent on the RolloverStrategu that is used. The 
DefaultRolloverStrategy will accept both a date/time pattern compatible with 
SimpleDateFormat and/or a %i which represents an integer counter. 
{code}
I am also used a DirectWrite Rollover Strategy.
{code}
DirectWrite Rollover Strategy
The DirectWriteRolloverStrategy causes log events to be written directly to 
files represented by the file pattern. With this strategy file renames are not 
performed. If the size-based triggering policy causes multiple files to be 
written during the specified time period they will be numbered starting at one 
and continually incremented until a time-based rollover occurs.
{code}
It's all about "will be numbered starting at one and continually incremented 
until a time-based rollover occurs".
When i use a TimeBasedTriggeringPolicy, index is resetting to 1 when a 
time-based rollover occurs (as written in manual). But when i use a 
CronTriggeringPolicy, index is not resetting back to 1 when a time-based 
rollover occurs and continue incrementing. This prevents using 
CronTriggeringPolicy with "index" in filePattern.

There is sample config that causes appending the entries of current day log in 
previous log file:
{code:xml}
        <RollingRandomAccessFile name="File" 
filePattern="logs/ggg-${hostName}-%d{yyyy-MM-dd}-%i.log.zip" append="true" 
immediateFlush="false">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [#%T:%t MDC:%X] 
%-5level %notEmpty{[%marker]} %logger{36} - %msg%n" charset="UTF-8" 
header="${bundle:version:name} ${bundle:version:version}${sys:line.separator}"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="200 MB"/>
            </Policies>
            <DirectWriteRolloverStrategy>
                <Delete basePath="logs">
                    <IfFileName glob="ggg-${hostName}-*" />
                    <IfAccumulatedFileSize exceeds="700 MB" />
                </Delete>
            </DirectWriteRolloverStrategy>
        </RollingRandomAccessFile>
{code}
There is folder contents:
||File||Modified||
|ggg-host1-2021-03-31-1.log.zip|01.04.2021 0:00|
|ggg-host1-2021-03-31-2.log.zip|02.04.2021 0:00|
|ggg-host1-2021-04-01-1.log|02.04.2021 11:00|

UPDATE: I have corrected modified dates according to timezone of component 
which is produced logs.


was (Author: sgorb):
I mean an index used in filePattern attirbute of RollingFile element (%i):
{code}
filePattern - The pattern of the file name of the archived log file. The format 
of the pattern should is dependent on the RolloverStrategu that is used. The 
DefaultRolloverStrategy will accept both a date/time pattern compatible with 
SimpleDateFormat and/or a %i which represents an integer counter. 
{code}
I am also used a DirectWrite Rollover Strategy.
{code}
DirectWrite Rollover Strategy
The DirectWriteRolloverStrategy causes log events to be written directly to 
files represented by the file pattern. With this strategy file renames are not 
performed. If the size-based triggering policy causes multiple files to be 
written during the specified time period they will be numbered starting at one 
and continually incremented until a time-based rollover occurs.
{code}
It's all about "will be numbered starting at one and continually incremented 
until a time-based rollover occurs".
When i use a TimeBasedTriggeringPolicy, index is resetting to 1 when a 
time-based rollover occurs (as written in manual). But when i use a 
CronTriggeringPolicy, index is not resetting back to 1 when a time-based 
rollover occurs and continue incrementing. This prevents using 
CronTriggeringPolicy with "index" in filePattern.

There is sample config that causes appending the entries of current day log in 
previous log file:
{code:xml}
        <RollingRandomAccessFile name="File" 
filePattern="logs/ggg-${hostName}-%d{yyyy-MM-dd}-%i.log.zip" append="true" 
immediateFlush="false">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [#%T:%t MDC:%X] 
%-5level %notEmpty{[%marker]} %logger{36} - %msg%n" charset="UTF-8" 
header="${bundle:version:name} ${bundle:version:version}${sys:line.separator}"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="200 MB"/>
            </Policies>
            <DirectWriteRolloverStrategy>
                <Delete basePath="logs">
                    <IfFileName glob="ggg-${hostName}-*" />
                    <IfAccumulatedFileSize exceeds="700 MB" />
                </Delete>
            </DirectWriteRolloverStrategy>
        </RollingRandomAccessFile>
{code}
There is folder contents:
||File||Modified||
|ggg-host1-2021-03-31-1.log.zip|01.04.2021 4:00|
|ggg-host1-2021-03-31-2.log.zip|02.04.2021 4:00|
|ggg-host1-2021-04-01-1.log|02.04.2021 15:00|



> log4j2 routing file appender appending the entries of current day log in 
> previous log file
> ------------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-1204
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1204
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.4.1
>         Environment: Windows 7 Enterprise , Core i5 vPro, 4Gb RAM
>            Reporter: Rajakrishnan
>            Priority: Major
>              Labels: newbie, patch
>
> This is my scenario: For my website I have used log4j2 for rolling log files. 
> currently I am generating 3 log file which consist of 2 rolling file (trace & 
> error) and 1 routing appender.
> 1) Trace log of entire date (rolling file appender)
> 2) Error log of entire date (rolling file appender)
> 3) logged users activity log for entire day (routing file appender)
> Below is my log4j2.xml using for the above scenario, which works exactly as 
> per the requirement.
> {code:xml}
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE xml>
> <Configuration status="WARN" name="mywebsite" monitorInterval="30">
>     <Appenders>
>         <Console name="Console" target="SYSTEM_OUT">
>             <PatternLayout
>                 pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] 
> [%logger{1}] - %msg%n" />
>         </Console>
>         <RollingFile name="error-log" append="true"
>             fileName="D:/client/error [${date:yyyy-MM-dd}].log" 
> filePattern="D:/lient/error-%d{yyyy-MM-dd}.log">
>             <PatternLayout
>                 pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] 
> [%logger{1}] - %msg%n" />
>             <Policies>
>                 <TimeBasedTriggeringPolicy interval="1" modulate="true" />
>                 <SizeBasedTriggeringPolicy size="250 MB" />
>             </Policies>
>         </RollingFile>
>         <RollingFile name="trace-log" append="true"
>             fileName="D:/client/trace [${date:yyyy-MM-dd}].log" 
> filePattern="D:/client/trace-%d{yyyy-MM-dd}.log">
>             <PatternLayout
>                 pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] 
> [%logger{1}] - %msg%n" />
>             <Policies>
>                 <TimeBasedTriggeringPolicy interval="1" modulate="true" />
>                 <SizeBasedTriggeringPolicy size="250 MB" />
>             </Policies>
>         </RollingFile>
>         <Routing name="RoutingAppender">
>             <Routes pattern="$${ctx:logFileName}">
>                 <Route>
>                     <RollingFile name="Rolling-${ctx:logFileName}" 
> append="true"
>                         
> fileName="D:/userlog/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
>                         
> filePattern="D:/userlog/${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.log.gz">
>                         <PatternLayout
>                             pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] 
> [%logger{1}] - %msg%n" />
>                         <Policies>
>                             <TimeBasedTriggeringPolicy interval="1" 
> modulate="true" />
>                             <SizeBasedTriggeringPolicy size="512 MB" />
>                         </Policies>
>                     </RollingFile>
>                 </Route>
>                 <Route ref="Console" key="${ctx:logFileName}" />
>             </Routes>
>         </Routing>
>     </Appenders>
>     <Loggers>
>         <Root level="trace" additivity="false">
>             <Appender-Ref ref="Console"/> 
>             <Appender-Ref ref="trace-log" level="trace" />
>             <Appender-Ref ref="error-log" level="error" />
>             <Appender-Ref ref="RoutingAppender" />
>         </Root>
>     </Loggers>
> </Configuration>
> {code}
> I am new to log4j2, I somehow managed to configure the log4j xml from the 
> sources in internet. Rolling file were created for every day and file 
> append-er created for each user and catches the respective events in the 
> respective files for the entire day, .
> Issue is :
> When date changes, events of current day were added in the previous day log 
> file.
> For example the trace.log file of 1st November has the log events entries of 
> 2nd November.
> This happens for file append-er too .
> For example file append-er will generate log file based on user log's in say 
> log file named john-01-112015.log was created yesterday. and same user log's 
> in 2nd November a new file should be rolled out as per the configuration. but 
> the  logs of 2nd November is added in the john-01-112015.log as said in the 
> previous scenario.
> new log file should be triggerd everyday, but it fails and logs were appended 
> in the existing log of the logged user.
> When the tomcat is stop started then the new file genereted, but the issue 
> exist until the next stop start. we can't do stop start in  production env..
> Guys in stackoverflow suggested to raise a ticket ,since it might be a 
> possible bug.
> Is there anything wrong in the log4j2.xml ? guys help me to solve this issue. 
> Guide me if i did anything wrong 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to