You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Ralph Goers (Jira)" <ji...@apache.org> on 2022/05/31 06:25:00 UTC

[jira] [Resolved] (LOG4J2-3490) Inconsistent counting in DirectWriteRolloverStrategy

     [ https://issues.apache.org/jira/browse/LOG4J2-3490?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ralph Goers resolved LOG4J2-3490.
---------------------------------
    Fix Version/s: 2.18.0
       Resolution: Fixed

Patch applied. Please verify and close.

> Inconsistent counting in DirectWriteRolloverStrategy
> ----------------------------------------------------
>
>                 Key: LOG4J2-3490
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-3490
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.17.2
>            Reporter: Hans
>            Assignee: Ralph Goers
>            Priority: Major
>             Fix For: 2.18.0
>
>
> When several log files already exist, DirectWriteRolloverStrategy has an inconsistent counting after restart. E.g. if maxFiles = 2 and in the log folder there are two files app-21.log  app-22.log, then the first created file is app-2.log.
> After a rollover, the next created file name is OK.
> Example configuration 
> {code:java}
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="warn" name="MyApp" packages="">
>     <Appenders>
>         <RollingFile name="RollingFile"
>             filePattern="logs/app-%i.log">
>             <PatternLayout>
>                 <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
>             </PatternLayout>
>             <Policies>
>                 <SizeBasedTriggeringPolicy size="1 kB" />
>             </Policies>
>             <DirectWriteRolloverStrategy maxFiles="2" />
>         </RollingFile>
>     </Appenders>
>     <Loggers>
>         <Root level="INFO">
>             <AppenderRef ref="RollingFile" />
>         </Root>
>     </Loggers>
> </Configuration> {code}
> Just run following Java application several times and you will encounter the effect after building up a log file history.
> {code:java}
> package log4jtest;
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> public class LogTest
> {
>     private static final Logger logger = LogManager.getLogger("HelloWorld");
>     public static void main(String[] args) {
>         for (int i = 0; i < 15; i++) {
>             logger.info("Hello, World!");
>         }
>     }
> }
> {code}
> May be caused by line 317 of DirectWriteRolloverStrategy 
> {code:java}
> final int fileIndex = eligibleFiles.size() > 0 ? (nextIndex > 0 ? nextIndex : eligibleFiles.size()) : 1;
> {code}
> which defaults to the number of files when nextIndex is not initialized.
>  
>  
> {panel:title=Running test application 5 times}
> C:\misc\pt1\ws\log4jtest\logs>ls 
> app-1.log
> C:\misc\pt1\ws\log4jtest\logs>ls 
> app-1.log  app-2.log
> C:\misc\pt1\ws\log4jtest\logs>ls 
> app-2.log  app-3.log
> C:\misc\pt1\ws\log4jtest\logs>ls 
> app-3.log  app-4.log
> C:\misc\pt1\ws\log4jtest\logs>ls 
> _*app-2.log*_  app-3.log  app-4.log 
> {panel}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.7#820007)