How to configure rolling file appender within Spring Boot's application.yml
是否可以在Spring Boot应用程序的application.yml中配置每日文件追加程序?
即文件名模式:myfile。%d {yyyy-MM-dd-HH-mm-ss} .log
我的application.yml文件中具有以下配置。
1 2 3 4 5 6 | logging: file: /mypath/myfile.log level: mypackage: INFO |
谢谢
默认文件附加器基于大小(10MB)。
在您的
即 就像是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <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"> </root> <logger name="org.springframework.web" level="INFO"/> </configuration> |
要覆盖默认文件附加器并将其更改为每日翻转,可以使用如下所示的logback-spring.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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"> </root> </configuration> |
聚会晚了一点……但是我可以使用application.yaml中的以下配置(没有任何logback.xml配置)使日志文件按大小滚动(按大小):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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. |
您还可以根据自己的文件大小配置滚动策略
logback-spring.xml。 在下面,我们为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?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"> </root> </configuration> |
从这个链接:-
1 2 3 4 5 6 7 8 9 | 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 |