如何删除日志文件才能与spring-boot-starter-log4j2一起使用?

时间:2019-05-25 13:28:28

标签: spring-boot log4j2 slf4j

我的项目使用Slf4j版本1.7.25和spring-boot-starter-log4j2 v1.5.21.RELEASE。我已经用Delete操作配置了一个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>

正在使用适当的名称生成日志文件,该名称在运行时提供,并且滚动也可以正常进行。仅删除未发生。

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

更新解决方案:

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

1 个答案:

答案 0 :(得分:1)

我有一个小型应用程序,尝试使用Delete动作。到目前为止,它对我有用。

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

请在您的log4j配置(<Configuration status="TRACE" monitorInterval="30">)中启用TRACE,这将帮助您仅遵循在README.md文件中看到的日志,即可检测出删除操作对您不起作用的原因。

它可能来自您设置错误的路径(也许我在这里不正确),但是使用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**

祝你好运