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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29918323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:32:22  来源:igfitidea点击:

How to configure rolling file appender within Spring Boot's application.yml

springloggingspring-bootyaml

提问by ele

Is is possible to configure a daily file appender within the application.yml of a Spring Boot application?

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

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

即文件名模式:myfile.%d{yyyy-MM-dd-HH-mm-ss}.log

I have configuration such as the following in my application.yml file.

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

logging:

   file: /mypath/myfile.log

   level:
     mypackage: INFO

Thanks

谢谢

回答by Donovan Muller

The default file appenderis size based (10MB).

默认的文件附加器是基于大小(10MB)。

In your logback.xmljust configure a TimeBasedRollingPolicyas described here

在你logback.xml只是一个配置TimeBasedRollingPolicy描述这里

I.e. something like:

即类似:

<?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>

回答by xmatjar

To override the default file appender and change it to daily rollover, you could use a logback-spring.xml looking like this:

要覆盖默认文件 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>

回答by anayagam

You can also configure rolling policy based on file size in your logback-spring.xml. In the below, we are specifying max file size as 10MB for SizeBasedTriggeringPolicy:

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

<?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>

回答by Kevin Klassen

A little late to the party... but I was able to get my log file to roll (by size) with the following configuration in application.yaml and no logback.xml configuration whatsoever:

聚会有点晚了……但是我能够使用 application.yaml 中的以下配置(按大小)滚动我的日志文件,并且没有任何 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.

回答by indranil32

From this 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