logback.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>
<configuration>
<!-- properties -->
<property name="logDir" value="logs" />
<property name="maxHistory" value="30" />
<!-- 控制臺(tái)輸出 ConsoleAppender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
<!-- %replace: 字符串替換; %caller: 方法調(diào)用棧. -->
<!-- (\bCaller.+?(?=\()): 正則, 匹配字符串"Caller...(",不含(號(hào) -->
%d{HH:mm:ss.SSS} %-5level %replace(%caller{1}){'(\bCaller.+?(?=\())|\r|\n|\s*|\t', ''} - %msg [%thread] %n
</Pattern>
</layout>
</appender>
<!-- 滾動(dòng)文件輸出, RollingFileAppender -->
<appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 日志過(guò)濾器 LevelFilter -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
<!-- 只寫(xiě)入DEBUG級(jí)別的日志信息 -->
</filter>
<!-- 滾動(dòng)策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 以天為單位記錄日志, 只保留30天 -->
<fileNamePattern>${logDir}/%d{yyyy-MM-dd}/debug-log.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 日志過(guò)濾器 ThresholdFilter -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- 只記錄WARN及以上級(jí)別的日志. 即WARN, ERROR. -->
<level>WARN</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logDir}/%d{yyyy-MM-dd}/warn-log.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 郵件日志
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.host.com</smtpHost>
<smtpPort></smtpPort>
<SSL>true</SSL>
<username>your@mail.com</username>
<password>yourPassword</password>
<to>someone@mail.com</to>
<to>another@mail.com</to>
<from>your@mail.com/from>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger{36} - %message%n</pattern>
</layout>
</appender>
-->
<!-- root, 只有在level及以上級(jí)別的日志會(huì)被輸出 -->
<!-- 例如: 當(dāng)root level設(shè)置為INFO時(shí), appender DEBUG中無(wú)法獲取到DEBUG級(jí)別的日志事件, 則DEBUG日志信息也不會(huì)寫(xiě)入debug.log中. -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG" />
<appender-ref ref="WARN" />
</root>
</configuration>