SpringBoot之LogBack常用配置
阿新 • • 發佈:2019-02-19
前言
若我們使用的是Starters,則SpringBoot預設日誌框架是logback,所以我們在SpringBoot中使用它還是比較簡單的。
需求明確
- 需要一個總日誌,記錄系統、環境的資訊
- 根據包名,分別在對應目錄下輸出日誌
- 輸出的日誌要有必須的資訊,方便除錯
- 定時刪除日誌,日誌限定大小
編寫測試類
需求明確後,我這裡編寫兩個測試類
Controller
@RestController public class UserController { private final static Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired private UserService mUserService; @GetMapping("/log") public String logController() { logger.info("controller"); return mUserService.logService(); } }
Service
@Service
public class UserService {
private final static Logger logger = LoggerFactory.getLogger(UserService.class);
public String logService() {
logger.info("service");
return "hello logback";
}
}
Application
@RestController @SpringBootApplication public class ZsdkApplication { private final static Logger logger = LoggerFactory.getLogger(ZsdkApplication.class); @RequestMapping("/") public String showIndex() { logger.info("hello world for log"); return "hello world for log"; } public static void main(String[] args) { SpringApplication.run(ZsdkApplication.class, args); } }
在resources資料夾下放置logback的配置檔案
注意:配置檔案最好以logback-spring.xml命名,這也是SpringBoot推薦的,不然有可能日誌初始化有問題
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <!--定義日誌檔案的儲存地址 勿在 LogBack 的配置中使用相對路徑--> <property name="LOG_HOME" value="/logs" /> <!-- 控制檯輸出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字元寬度,%line:行號,%msg:日誌訊息,%n是換行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} Line:%line ----------------> %msg%n</pattern> </encoder> </appender> <!-- 按照每天生成日誌檔案 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日誌檔案輸出的檔名--> <FileNamePattern>${LOG_HOME}/system/SDKWeb-%d{yyyy-MM-dd}.log</FileNamePattern> <!--日誌檔案保留天數--> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} Line:%line ----------------> %msg%n</pattern> </encoder> <!--日誌檔案最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- show parameters for hibernate sql 專為 Hibernate 定製 --> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> <!--myibatis log configure--> <logger name="com.apache.ibatis" level="TRACE"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.Statement" level="DEBUG"/> <logger name="java.sql.PreparedStatement" level="DEBUG"/> <!-- 日誌輸出級別 --> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> <!--分包輸出日誌,分包配置--> <appender name="serviceAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/service/SdkService-%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} Line:%line ----------------> %msg%n</pattern> </encoder> <!--日誌檔案最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <appender name="controllerAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/controller/SdkController-%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} Line:%line ----------------> %msg%n</pattern> </encoder> <!--日誌檔案最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!--分包輸出日誌,分包指向--> <logger name="com.zsdk.service" level="INFO" additivity="false"> <appender-ref ref="serviceAppender" /> </logger> <logger name="com.zsdk.controller" level="INFO" additivity="false"> <appender-ref ref="controllerAppender" /> </logger> </configuration>