一個簡單通用的logback配置檔案
阿新 • • 發佈:2018-11-12
首先pom依賴於ch.qos.logback基於slf4j
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.6</version> </dependency>
基礎配置logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configurationxmlns="http://ch.qos.logback/xml/ns/logback" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd"> <!-- 輸出到控制檯 --> <appender name="STDOUT"class="ch.qos.logback.core.ConsoleAppender" > <!-- 輸出的格式 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n</pattern> </encoder> </appender><appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>${catalina.base}/logs/logback-info.log.%d{yyyy-MM-dd}</fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>60</maxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印錯誤日誌 --> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <!-- log.dir 在maven profile裡配置 --> <fileNamePattern>${catalina.base}/logs/logback-error.log.%d{yyyy-MM-dd}</fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>60</maxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印錯誤日誌 --> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 相當於logger元素,只是name值已經確定為root了 --> <!--確定輸出級別為info以上--> <root level="info"> <appender-ref ref="STDOUT" /> <appender-ref ref="INFO" /> <appender-ref ref="ERROR" /> </root> </configuration>