【问题标题】:How to get log file deletion to work with spring-boot-starter-log4j2?如何让日志文件删除与 spring-boot-starter-log4j2 一起使用?
【发布时间】:2019-05-25 13:28:27
【问题描述】:

我的项目使用 Slf4j 版本 1.7.25 和 spring-boot-starter-log4j2 v1.5.21.RELEASE。我已经配置了一个带有删除操作的 log4j2.xml 文件。删除似乎根本不起作用。我已经尝试添加

<configuration status="trace">

但它没有显示任何与路径有关的错误日志。

这是日志输出:

2019-05-25 18:41:20,124 main DEBUG Building Plugin[name=Delete, class=org.apache.logging.log4j.core.appender.rolling.action.DeleteAction].
2019-05-25 18:41:20,124 main DEBUG createDeleteAction(basePath="/logs/hadoop/archive/", followLinks="false", maxDepth="2", testMode="false", PathSorter=null, ={IfAll[IfFileName(glob:okie-*.log.gz), IfLastModified(age=P1D)]}, ScriptCondition=null, Configuration(okieLog4j2Config))
2019-05-25 18:41:20,124 main DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2019-05-25 18:41:20,125 main DEBUG createStrategy(max="1", min="null", fileIndex="null", compressionLevel="null", ={DeleteAction[basePath=/logs/hadoop/archive, options=[], maxDepth=2, conditions=[IfAll[IfFileName(glob:okie-*.log.gz), IfLastModified(age=P1D)]]]}, stopCustomActionsOnError="true", Configuration(okieLog4j2Config))

另外请看看我下面的配置 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="okieLog4j2Config" status="trace"
  strict="true" monitorInterval="5">
  <properties>
    <property name="patternlayout">%d [%t] %-5level %logger{36} -
      %msg%n%throwable{full}
    </property>
  </properties>
  <appenders>
    <appender name="Console" type="Console" target="SYSTEM_OUT">
      <layout type="PatternLayout" pattern="${patternlayout}"/>
    </appender>
    <Routing name="File">
      <Routes pattern="$${ctx:ROUTING_KEY}">
        <Route>
          <RollingFile name="okie.log" bufferedIO="true" immediateFlush="true" append="true"
            fileName="logs/${ctx:ROUTING_KEY}/okie.log"
            filePattern="logs/${ctx:ROUTING_KEY}/archive/$${date:yyyy-MM}/okie-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
              <pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
            </PatternLayout>
            <Policies>
              <TimeBasedTriggeringPolicy/>
              <!--<SizeBasedTriggeringPolicy size="10 MB"/>-->
            </Policies>
            <DefaultRolloverStrategy max="1">
              <Delete basePath="/logs/hadoop/archive/" maxDepth="2">
                <IfAll>
                  <IfFileName glob="okie-*.log.gz"/>
                  <IfLastModified age="1d"/>
                </IfAll>
              </Delete>
            </DefaultRolloverStrategy>
          </RollingFile>
        </Route>
      </Routes>
    </Routing>
  </appenders>
  <loggers>
    <Logger name="org.springframework" level="INFO">
      <AppenderRef ref="Console"/>
    </Logger>
    <root level="INFO">
      <appender-ref ref="Console" level="INFO"/>
      <appender-ref ref="File" level="WARN"/>
    </root>
  </loggers>
</configuration>

日志文件正在按预期生成,并使用在运行时提供的适当名称,并且滚动也正常工作。只有删除没有发生。

另外,如果我需要添加更多信息,请告诉我。

更新解决方案:

“/”正斜杠是我提到要删除的路径中的问题。删除它修复它。但我仍然想知道为什么它没有出现在日志中。

【问题讨论】:

    标签: spring-boot log4j2 slf4j


    【解决方案1】:

    我有一个小应用程序可以尝试使用删除操作。到目前为止,它对我有用。

    请参阅https://github.com/bigzidane/spring-boot-delete-log 的示例。检查 READMe.md 以了解以前的日志是如何被删除的。

    请在您的 log4j 配置 (&lt;Configuration status="TRACE" monitorInterval="30"&gt;) 中启用 TRACE,它可以帮助您检测为什么删除操作对您不起作用,只需遵循您在我的 README.md 文件中看到的日志。

    它可能来自你设置错误的路径(也许我在这里不正确)但是使用TRACE选项我相信你可以自己找到信息。

    带有 Trace 选项的日志示例

    2019-05-25 09:38:33.756 INFO WINDOWS-ESDA5FC --- [ main] c.e.SpringBootDeleteLogApp : Starting SpringBootDeleteLogApp on WINDOWS-ESDA5FC with PID 17236 (C:\Users\dotha\IdeaProjects\spring-boot-delete-log\target\classes started by dotha in C:\Users\dotha\IdeaProjects\spring-boot-delete-log)
    
    2019-05-25 09:38:33.760 INFO WINDOWS-ESDA5FC --- [ main] c.e.SpringBootDeleteLogApp : No active profile set, falling back to default profiles: default
    
    2019-05-25 09:38:33.798 INFO WINDOWS-ESDA5FC --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@338fc1d8: startup date [Sat May 25 09:38:33 CDT 2019]; root of context hierarchy
    
    2019-05-25 09:38:33,802 main TRACE DefaultRolloverStrategy.purge() took 3.0 milliseconds
    
    2019-05-25 09:38:33,804 main DEBUG RollingFileManager executing synchronous FileRenameAction[logs\log4j2-demo.log to logs\log4j2-demo-2019-05-25-2.log, renameEmptyFiles=false]
    
    2019-05-25 09:38:33,805 main TRACE Renamed file C:\Users\dotha\IdeaProjects\spring-boot-delete-log\logs\log4j2-demo.log to C:\Users\dotha\IdeaProjects\spring-boot-delete-log\logs\log4j2-demo-2019-05-25-2.log with Files.move
    
    2019-05-25 09:38:33,806 main DEBUG RollingFileManager executing async CompositeAction[DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:log4j2-demo-*.log), IfLastModified(age=PT1M)]]]
    
    2019-05-25 09:38:33,806 main DEBUG Now writing to logs/log4j2-demo.log at 2019-05-25T09:38:33.806-0500
    
    2019-05-25 09:38:33,807 Log4j2-TF-2-RollingFileManager-3 DEBUG Starting DeleteAction[basePath=logs, options=[], maxDepth=1, conditions=[IfFileName(glob:log4j2-demo-*.log), IfLastModified(age=PT1M)]]
    
    2019-05-25 09:38:33,809 Log4j2-TF-2-RollingFileManager-3 DEBUG DeleteAction complete in 0.001881032 seconds
    
    2019-05-25 09:38:33,810 Log4j2-TF-2-RollingFileManager-3 TRACE Sorted paths:
    
    2019-05-25 09:38:33,810 Log4j2-TF-2-RollingFileManager-3 TRACE logs\log4j2-demo.log (modified: 2019-05-25T14:38:33.807885Z)
    
    2019-05-25 09:38:33,812 Log4j2-TF-2-RollingFileManager-3 TRACE logs\log4j2-demo-2019-05-25-2.log (modified: 2019-05-25T14:38:33.803895Z)
    
    2019-05-25 09:38:33,812 Log4j2-TF-2-RollingFileManager-3 TRACE logs\log4j2-demo-2019-05-25-1.log (modified: 2019-05-25T14:36:13.862034Z)
    
    2019-05-25 09:38:33,812 Log4j2-TF-2-RollingFileManager-3 TRACE IfFileName REJECTED: 'glob:log4j2-demo-*.log' does not match relative path 'log4j2-demo.log'
    
    2019-05-25 09:38:33,812 Log4j2-TF-2-RollingFileManager-3 TRACE Not deleting base=logs, relative=log4j2-demo.log
    
    2019-05-25 09:38:33,812 Log4j2-TF-2-RollingFileManager-3 TRACE IfFileName ACCEPTED: 'glob:log4j2-demo-*.log' matches relative path 'log4j2-demo-2019-05-25-2.log'
    
    2019-05-25 09:38:33,813 Log4j2-TF-2-RollingFileManager-3 TRACE IfLastModified REJECTED: log4j2-demo-2019-05-25-2.log ageMillis '9' < 'PT1M'
    
    2019-05-25 09:38:33,813 Log4j2-TF-2-RollingFileManager-3 TRACE Not deleting base=logs, relative=log4j2-demo-2019-05-25-2.log
    
    2019-05-25 09:38:33,813 Log4j2-TF-2-RollingFileManager-3 TRACE IfFileName ACCEPTED: 'glob:log4j2-demo-*.log' matches relative path 'log4j2-demo-2019-05-25-1.log'
    
    2019-05-25 09:38:33,813 Log4j2-TF-2-RollingFileManager-3 TRACE IfLastModified ACCEPTED: log4j2-demo-2019-05-25-1.log ageMillis '139951' >= 'PT1M'
    
    **2019-05-25 09:38:33,813 Log4j2-TF-2-RollingFileManager-3 TRACE Deleting logs\log4j2-demo-2019-05-25-1.log**
    

    祝你好运,

    【讨论】:

    • 感谢您的所有努力和建议。这很有帮助,我能够解决我的问题。
    • 很高兴知道它对您有所帮助。请更新您的发现,以便人们从中学习。
    猜你喜欢
    • 2020-11-05
    • 2019-11-30
    • 2019-10-28
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多