想使用Spring Boot中日誌輸出,這樣之後遇到問題,就能夠翻看日誌數據,看看程序運行的過程,便於排查錯誤。html
<?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" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </configuration>
logging.path=logs
上面兩步,主要就是對spring-boot對最基礎日誌配置。java
<?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="DAY_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"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd_HH-mm}.log</fileNamePattern> <maxHistory>365</maxHistory> </rollingPolicy> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="DAY_FILE" /> </root> </configuration>
**Note:**這裏的DAY_FILE,其實主要就是參考org/springframework/boot/logging/logback/file-appender.xml
進行配置。spring
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DemoController { private final static Logger logger = LoggerFactory.getLogger(DemoController.class); public Demo greeting(@RequestParam(value="serviceId", defaultValue="World") String serviceId) { logger.info("serviceId-->" + serviceId); ... } }
沒有必要去弄顏色輸出,由於個人命令行工具已經弄好了主題,我使用的是德古拉主題;使用Logback理由,是由於Spring推薦使用。app
參考: 77. Logging Spring boot——logback 基礎使用篇(一) 設置spring-boot的logging Spring Boot 日誌記錄 SLF4J USING LOGBACK WITH SPRING BOOTspring-boot