如何在Spring Boot的application.yml中配置滚动文件appender

时间:2015-04-28 11:34:00

标签: spring logging spring-boot yaml

是否可以在Spring Boot应用程序的application.yml中配置每日文件追加器?

即。 filenamePattern:myfile。%d {yyyy-MM-dd-HH-mm-ss} .log

我的application.yml文件中有以下配置。

logging:

   file: /mypath/myfile.log

   level:
     mypackage: INFO

由于

7 个答案:

答案 0 :(得分:30)

default file appender基于尺寸(10MB)。

在您的logback.xml中,按照here

所述配置TimeBasedRollingPolicy

即。类似的东西:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>

  <appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

        <!-- daily rollover -->
        <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>

    </rollingPolicy>
  </appender>

  <root level="INFO">
    <appender-ref ref="ROLLIN" />
  </root>

  <logger name="org.springframework.web" level="INFO"/>
</configuration>

答案 1 :(得分:4)

要覆盖默认文件appender并将其更改为每日翻转,您可以使用如下所示的logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

    <appender name="ROLLING-FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING-FILE"/>
    </root>

</configuration>

答案 2 :(得分:1)

logging.file.name=MyApp.log
logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd-HH-mm-ss}.%i.log

使用Spring Boot 2.3.42.2.10
Not使用Spring Boot 2.1.17

答案 3 :(得分:0)

您还可以根据自己的文件大小配置滚动策略  logback-spring.xml。在下面,我们为SizeBasedTriggeringPolicy指定最大文件大小为10MB:

<?xml version="1.0" encoding="UTF-8"?>

    <configuration>

        <include resource="org/springframework/boot/logging/logback/defaults.xml" />
        <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
        <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

        <appender name="ACTUAL_LOG_FILE"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
            </encoder>
            <file>${LOG_FILE}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <!-- gz extension to enable file deletion by logrotator  -->
                <fileNamePattern>${LOG_FILE}.%i.gz</fileNamePattern>
                 <minIndex>1</minIndex>
                <maxIndex>10</maxIndex>
            </rollingPolicy>
            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <MaxFileSize>10MB</MaxFileSize>
            </triggeringPolicy>
        </appender>

        <root level="INFO">
            <appender-ref ref="ACTUAL_LOG_FILE" />
        </root>

    </configuration>

答案 4 :(得分:0)

参加聚会有点晚了...但是我可以使用application.yaml中的以下配置使log文件按大小滚动(按大小),而没有任何logback.xml配置:

logging:
    file: /var/log/webapps/app/app.log

    # Roll the log file when it reaches max size
    file.max-size: 1024KB

    # Limit the number of log files retained
    file.max-history: 50

    pattern:
        console: "%d %-5level %logger : %msg%n"
        file: "%d %-5level [%thread] %logger : %msg%n"

    level:
        root:                                           info
        my.package.of.app:                              debug
        org.springframework:                            error
        # etc. etc.

答案 5 :(得分:-1)

在application.properties文件中添加以下代码行,请根据您的要求使用其中任何

logging.file.name=myinfo.log
#daily rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd}.%i.log
#per hour rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH}.%i.log
#per minute rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm}.%i.log
#per secs rolling logs
 logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm-ss}.%i.log

答案 6 :(得分:-2)

从此link: -

logging:
  file: logs/application-debug.log
  pattern:
    console: "%d %-5level %logger : %msg%n"
    file: "%d %-5level [%thread] %logger : %msg%n"
  level:
    org.springframework.web: ERROR
    com.howtodoinjava: INFO
    org.hibernate: ERROR