删除使用 log4j2 滚动文件创建的文件时出现的问题

发布于 2025-01-17 11:45:49 字数 6167 浏览 0 评论 0原文

我已经配置了log4j2.xml文件,以删除2分钟以上(用于测试)的文件。这可以正常工作,但我无法删除生成的第一个文件。翻转后,所有文件都将被删除。

我附上了项目文件夹结构的图像。您可以在日志文件夹中看到2个日志文件。由于创建的时间仅为3-4毫秒左右,也没有以指定格式生成,因此第一个日志文件没有被删除。

“项目文件夹结构”

这是我的log4j2.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Properties>
        <Property name="csvLog.fileName">csvLog</Property>
        <Property name="file-header">sender,flow,message</Property>
        <Property name="baseDir">logs</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MM-dd} %-5p %c{1}:%L - %m%n" />
        </Console>
        <RollingFile name="csvFile" 
                     fileName="${baseDir}/csvLog.csv"
                     filePattern="${baseDir}/csvLog-%d{yyyy-MM-dd-HH-mm}.csv">
            <CsvParameterLayout delimiter="," header="${file-header}\n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
             <!--   <SizeBasedTriggeringPolicy size="1 KB" /> -->
            </Policies>
            <DefaultRolloverStrategy>
               <Delete basePath="${baseDir}" maxDepth="1">
                  <IfFileName glob="csvLog*.csv" /> 
                  <IfLastModified age="2m" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="CSVLOG" level="debug" additivity="false">
            <appender-ref ref="csvFile" level="debug"/>
        </Logger>
        <Root level="debug" additivity="false">
            <AppenderRef ref="Console" level="debug"/>
        </Root>
    </Loggers>
</Configuration>

这是我的跟踪:

    2022-03-28 23:31:17,343 main TRACE PatternProcessor.getNextTime returning 2022/03/28-23:32:00.000, nextFileTime=2022/03/28-23:31:00.000, prevFileTime=2022/03/28-23:30:00.000, current=2022/03/28-23:31:17.343, freq=EVERY_MINUTE
2022-03-28 23:31:17,345 main TRACE DefaultRolloverStrategy.purge() took 1.0 milliseconds
2022-03-28 23:31:17,345 main DEBUG RollingFileManager executing synchronous FileRenameAction[logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv to logs\csvLog-2022-03-28-23-30.csv, renameEmptyFiles=false]
2022-03-28 23:31:17,348 main TRACE Renamed file C:\Users\Pakalu\OneDrive\Desktop\CSV_Log\CSVlog\logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv to C:\Users\Pakalu\OneDrive\Desktop\CSV_Log\CSVlog\logs\csvLog-2022-03-28-23-30.csv with Files.move
2022-03-28 23:31:17,348 main DEBUG RollingFileManager executing async CompositeAction[DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:csvLog*.csv), IfLastModified(age=PT2M)]]]
2022-03-28 23:31:17,349 Log4j2-2 DEBUG Starting DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:csvLog*.csv), IfLastModified(age=PT2M)]]
2022-03-28 23:31:17,349 Log4j2-2 DEBUG DeleteAction complete in 7.6E-4 seconds
2022-03-28 23:31:17,350 Log4j2-2 TRACE Sorted paths:
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv (modified: 2022-03-28T18:01:17.3480474Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-2022-03-28-23-30.csv (modified: 2022-03-28T18:00:17.3403012Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-2022-03-28-23-29.csv (modified: 2022-03-28T17:59:17.3259106Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-%d{yyyy-MM-dd-HH-mm}.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified REJECTED: csvLog-%d{yyyy-MM-dd-HH-mm}.csv ageMillis '2' < 'PT2M'
2022-03-28 23:31:17,351 Log4j2-2 TRACE Not deleting base=logs, relative=csvLog-%d{yyyy-MM-dd-HH-mm}.csv
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-2022-03-28-23-30.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified REJECTED: csvLog-2022-03-28-23-30.csv ageMillis '60011' < 'PT2M'
2022-03-28 23:31:17,351 Log4j2-2 TRACE Not deleting base=logs, relative=csvLog-2022-03-28-23-30.csv
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-2022-03-28-23-29.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified ACCEPTED: csvLog-2022-03-28-23-29.csv ageMillis '120026' >= 'PT2M'
2022-03-28 23:31:17,352 Log4j2-2 TRACE Deleting logs\csvLog-2022-03-28-23-29.csv

这是我的主要文件:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class App {
 private static final Logger csvlogger = LogManager.getLogger("CSVLOG");
 private static final Logger logger = LogManager.getLogger();

 public static void main(String args[]){

     SpringApplication.run(App.class);
     
     csvlogger.info("Debug Message", "Namaste", "Whats up", "hmm..ok");
     logger.info("You have made an error");
     logger.debug("Debug Message");
     
     runMe("test");
 }
 
 private static void runMe(String parameterParam){
     String parameter;
     for(int i=0; i<100; ++i) {
         parameter = parameterParam + i;
         System.out.println("log iteration: "+i);
         if (csvlogger.isDebugEnabled()) {
             csvlogger.debug(new ObjectArrayMessage("JD", "Ri", parameter));
         }

         if (csvlogger.isInfoEnabled()) {
             csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         }

         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         try {
             Thread.sleep(60000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
 }
 
}

我已经转介了很多关于Stackoverflow的文章,但无法弄清楚这个问题。谁能帮助我了解第一个文件的生成以及如何删除?

任何帮助将不胜感激。谢谢 !

I have configured the log4j2.xml file to delete files older than 2 minutes(for testing) to be deleted. This works fine but i am not able to delete the first file that is generated. After rollover, all files are being deleted.

I have attached an image of the project folder structure. You can see 2 logs files in the log folder. The first log file is not being deleted as the time it was created in is just around 3-4 milliseconds and also it is not being generated in the specified format.

Project Folder Structure

This is my log4j2.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Properties>
        <Property name="csvLog.fileName">csvLog</Property>
        <Property name="file-header">sender,flow,message</Property>
        <Property name="baseDir">logs</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MM-dd} %-5p %c{1}:%L - %m%n" />
        </Console>
        <RollingFile name="csvFile" 
                     fileName="${baseDir}/csvLog.csv"
                     filePattern="${baseDir}/csvLog-%d{yyyy-MM-dd-HH-mm}.csv">
            <CsvParameterLayout delimiter="," header="${file-header}\n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
             <!--   <SizeBasedTriggeringPolicy size="1 KB" /> -->
            </Policies>
            <DefaultRolloverStrategy>
               <Delete basePath="${baseDir}" maxDepth="1">
                  <IfFileName glob="csvLog*.csv" /> 
                  <IfLastModified age="2m" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="CSVLOG" level="debug" additivity="false">
            <appender-ref ref="csvFile" level="debug"/>
        </Logger>
        <Root level="debug" additivity="false">
            <AppenderRef ref="Console" level="debug"/>
        </Root>
    </Loggers>
</Configuration>

This is my trace :

    2022-03-28 23:31:17,343 main TRACE PatternProcessor.getNextTime returning 2022/03/28-23:32:00.000, nextFileTime=2022/03/28-23:31:00.000, prevFileTime=2022/03/28-23:30:00.000, current=2022/03/28-23:31:17.343, freq=EVERY_MINUTE
2022-03-28 23:31:17,345 main TRACE DefaultRolloverStrategy.purge() took 1.0 milliseconds
2022-03-28 23:31:17,345 main DEBUG RollingFileManager executing synchronous FileRenameAction[logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv to logs\csvLog-2022-03-28-23-30.csv, renameEmptyFiles=false]
2022-03-28 23:31:17,348 main TRACE Renamed file C:\Users\Pakalu\OneDrive\Desktop\CSV_Log\CSVlog\logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv to C:\Users\Pakalu\OneDrive\Desktop\CSV_Log\CSVlog\logs\csvLog-2022-03-28-23-30.csv with Files.move
2022-03-28 23:31:17,348 main DEBUG RollingFileManager executing async CompositeAction[DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:csvLog*.csv), IfLastModified(age=PT2M)]]]
2022-03-28 23:31:17,349 Log4j2-2 DEBUG Starting DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:csvLog*.csv), IfLastModified(age=PT2M)]]
2022-03-28 23:31:17,349 Log4j2-2 DEBUG DeleteAction complete in 7.6E-4 seconds
2022-03-28 23:31:17,350 Log4j2-2 TRACE Sorted paths:
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-%d{yyyy-MM-dd-HH-mm}.csv (modified: 2022-03-28T18:01:17.3480474Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-2022-03-28-23-30.csv (modified: 2022-03-28T18:00:17.3403012Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE logs\csvLog-2022-03-28-23-29.csv (modified: 2022-03-28T17:59:17.3259106Z)
2022-03-28 23:31:17,350 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-%d{yyyy-MM-dd-HH-mm}.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified REJECTED: csvLog-%d{yyyy-MM-dd-HH-mm}.csv ageMillis '2' < 'PT2M'
2022-03-28 23:31:17,351 Log4j2-2 TRACE Not deleting base=logs, relative=csvLog-%d{yyyy-MM-dd-HH-mm}.csv
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-2022-03-28-23-30.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified REJECTED: csvLog-2022-03-28-23-30.csv ageMillis '60011' < 'PT2M'
2022-03-28 23:31:17,351 Log4j2-2 TRACE Not deleting base=logs, relative=csvLog-2022-03-28-23-30.csv
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfFileName ACCEPTED: 'glob:csvLog*.csv' matches relative path 'csvLog-2022-03-28-23-29.csv'
2022-03-28 23:31:17,351 Log4j2-2 TRACE IfLastModified ACCEPTED: csvLog-2022-03-28-23-29.csv ageMillis '120026' >= 'PT2M'
2022-03-28 23:31:17,352 Log4j2-2 TRACE Deleting logs\csvLog-2022-03-28-23-29.csv

This is my main file:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class App {
 private static final Logger csvlogger = LogManager.getLogger("CSVLOG");
 private static final Logger logger = LogManager.getLogger();

 public static void main(String args[]){

     SpringApplication.run(App.class);
     
     csvlogger.info("Debug Message", "Namaste", "Whats up", "hmm..ok");
     logger.info("You have made an error");
     logger.debug("Debug Message");
     
     runMe("test");
 }
 
 private static void runMe(String parameterParam){
     String parameter;
     for(int i=0; i<100; ++i) {
         parameter = parameterParam + i;
         System.out.println("log iteration: "+i);
         if (csvlogger.isDebugEnabled()) {
             csvlogger.debug(new ObjectArrayMessage("JD", "Ri", parameter));
         }

         if (csvlogger.isInfoEnabled()) {
             csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         }

         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         csvlogger.info(new ObjectArrayMessage("JD", "Ri", parameter));
         try {
             Thread.sleep(60000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
 }
 
}

I have referred many articles on stackoverflow but not able to figure out the problem. Can anyone please help me as to how the first file is generated and how to delete it ?

Any Help would be greatly appreciated. Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-01-24 11:45:49

我认为这是因为filename =“ $ {laseir}/csvlog.csv” - &gt;是日志输出的当前文件。它是碧昂删除的历史。和文件fiLepattern =“ $ {lasedir}/csvlog-%d {yyyy-mm-dd-hh-mm} .csv-是用于滚动的文件(重命名) - “存档日志文件”。每次将信息写入日志中CSVLOG.CSV将被创建(如果不是EXSITS),我想第一步是滚动和删除,然后将创建CSVLOG.CSV。 org/log4j/2.x/手动/附录器.html#rollingFileAppender“ rel =” nofollow noreferrer“> https://logging.apache.org/log4j/2.x/manual/manual/appenders.html #html #html #html#rollingfilingfileappender

I think this is because fileName="${baseDir}/csvLog.csv" -> is the current file for log output. It is beyong delete history. And the files filePattern="${baseDir}/csvLog-%d{yyyy-MM-dd-HH-mm}.csv - are files for rolling (renaming) - "archived log files". Each time you write info into log the csvLog.csv will be created (if not exsits). And I suppose the first step is rolling and deleting, and only then csvLog.csv will be created. See here: https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文