我一直在使用默認的 spring 日志配置,我只在 application.properties 檔案中指定檔案名。
logging.file.name=app.log
但是當我從 cmd 行啟動應用程式時,默認情況下會附加日志 "java -jar abc.jar"
我試圖在每次啟動應用程式之前搜索清除檔案的屬性,但找不到它。在啟動應用程式之前,我應該如何清除日志檔案?
uj5u.com熱心網友回復:
Spring Boot 使用 Logback 框架作為默認的 Logger。您可以通過 application.properties 檔案使用環境變數來設定一些日志記錄屬性,但 logback xml 配置提供了更強大的功能。
當類路徑中的檔案具有以下名稱之一時,Spring Boot 將自動通過默認配置加載它:
- logback-spring.xml
- 登錄檔案
- logback-spring.groovy
- logback.groovy
所以你可以把下面的代碼片段放在 src/main/resources/logback-spring.xml 檔案中
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
>
<layout >
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="FILE" >
<file>app.log</file>
<append>false</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="Console" />
</root>
</configuration>
生產線<append>false</append>完成這項作業。如果您想記錄比編碼器模式可用的更多資訊,<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>請查看 logback 檔案https://logback.qos.ch/manual/layouts.html#logback-access
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402210.html
