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

标签 spring-boot log4j2 slf4j

我的项目使用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>

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

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

更新解决方案:

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

最佳答案

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

请参阅 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**

祝你好运

关于spring-boot - 如何让日志文件删除与 spring-boot-starter-log4j2 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305269/

相关文章:

xml - log4j2 xml配置 - 记录到文件和控制台(不同级别)

java - log4j2结合Kafka性能不佳

maven - 通过将其添加为测试范围来排除 Maven 依赖项?

java - Logback 占位符不适用于测试

spring-boot - Gradle Spring Boot项目无法从IDEA运行

java - 没有 hibernate-validator 依赖的 Spring Boot 应用程序

spring-boot - 在Spring Boot Gradle项目中手动添加第3方jar

java - 未创建日志文件

java - 使用 log4j2 和 slf4j : java. lang.StackOverflowError

java - 当自定义 validator 使用 @Component 注释时,JSR-303 验证将被忽略