Spring 引导 - 如何为轮换的 tomcat 日志文件指定不同的位置

Spring boot - How to specify different location for rotated tomcat log file

我有一个 spring 启动应用程序。嵌入式 tomcat 的访问日志配置为每天轮换,时间戳后缀在 application.properties 中。我的要求是将旋转的日志文件生成到存档文件夹中。但我找不到任何配置来指定 application.properties 中旋转文件的位置。有谁知道如何做到这一点?

所有最新的日志将在 /logs 文件夹中并且 所有旋转的日志文件都应该在 /logs/archive 文件夹中。 - 如何做到这一点?

您可以创建 DailySchedular class 来完成这项工作。使用@EnableScheduling和一个@Scheduled每天唤醒一次的方法,根据日期扫描想要的目录,将想要的文件移除到存档目录。

OK, finally i figured out a way to do this using logback access.

    Include the following dependency

<dependency>
                <groupId>net.rakugakibox.spring.boot</groupId>
                <artifactId>logback-access-spring-boot-starter</artifactId>
                <version>2.7.0</version>
    </dependency>




Also create a logback-access.xml in resources folder with following configuration.  

<configuration>
    <property resource="application.properties" />
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/dev_access.log</file>
        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/Archive/dev_access_%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%h %l %u %t "%r" %s %b  %D</pattern>
        </encoder>
    </appender>
    <appender-ref ref="FILE" />
</configuration>