一般咱們在不一樣的環境使用不一樣的日誌配置文件,本章講指定logback的配置文件,如何使用logback參照【Log】logback的配置和使用(一)html
寫一個配置加載類,注意JoranConfigurator這個導入的是ch.qos.logback.classic.joran.JoranConfigurator包下面的java
1 package com.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.Date; 6 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 10 import ch.qos.logback.classic.LoggerContext; 11 import ch.qos.logback.classic.joran.JoranConfigurator; 12 import ch.qos.logback.core.joran.spi.JoranException; 13 import ch.qos.logback.core.util.StatusPrinter; 14 15 /** 16 * 17 * @author H__D 18 * 19 */ 20 public class LogBackConfigLoader { 21 22 public static void main(String[] args) throws IOException, JoranException { 23 load("D:/logback.xml"); 24 25 Logger logger = LoggerFactory.getLogger(LogBackConfigLoader.class); 26 27 logger.debug("如今的時間是 {}", new Date().toString()); 28 29 logger.info(" This time is {}", new Date().toString()); 30 31 logger.warn(" This time is {}", new Date().toString()); 32 33 logger.error(" This time is {}", new Date().toString()); 34 35 @SuppressWarnings("unused") 36 int n = 1 / 0; 37 } 38 39 /** 40 * 加載外部的logback配置文件 41 * 42 * @param externalConfigFileLocation 43 * 配置文件路徑 44 * @throws IOException 45 * @throws JoranException 46 */ 47 public static void load(String externalConfigFileLocation) throws IOException, JoranException { 48 49 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 50 51 File externalConfigFile = new File(externalConfigFileLocation); 52 53 if (!externalConfigFile.exists()) { 54 55 throw new IOException("Logback External Config File Parameter does not reference a file that exists"); 56 57 } else { 58 59 if (!externalConfigFile.isFile()) { 60 throw new IOException("Logback External Config File Parameter exists, but does not reference a file"); 61 62 } else { 63 64 if (!externalConfigFile.canRead()) { 65 throw new IOException("Logback External Config File exists and is a file, but cannot be read."); 66 67 } else { 68 69 JoranConfigurator configurator = new JoranConfigurator(); 70 configurator.setContext(lc); 71 lc.reset(); 72 configurator.doConfigure(externalConfigFileLocation); 73 74 StatusPrinter.printInCaseOfErrorsOrWarnings(lc); 75 } 76 77 } 78 79 } 80 81 } 82 }
D盤logback.xml文件以下:app
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!-- scan="true" 當此屬性設置爲true時,配置文件若是發生改變,將會被從新加載,默認值爲true。 --> 4 <!-- scanPeriod="30 seconds" 設置每30秒自動掃描,若沒有指定具體單位則以milliseconds爲標準(單位:milliseconds, seconds, minutes or hours) --> 5 <!-- debug="false"當此屬性設置爲true時,將打印出logback內部日誌信息,實時查看logback運行狀態。默認值爲false。--> 6 <configuration scan="true" scanPeriod="30 seconds"> 7 <!-- 上下文名稱 --> 8 <contextName>test</contextName> 9 10 <!-- 存放日誌文件路徑 --> 11 <property name="Log_Home" value="D:/logs/test" /> 12 13 14 <!-- ch.qos.logback.core.ConsoleAppender 控制檯輸出 --> 15 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 16 <encoder> 17 <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 --> 18 <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</pattern> 19 </encoder> 20 </appender> 21 22 <!-- DEBUG級別 --> 23 <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> 24 25 <!-- 級別過濾器 --> 26 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 27 <!-- 設置過濾級別 --> 28 <level>DEBUG</level> 29 <!-- 用於配置符合過濾條件的操做 --> 30 <onMatch>ACCEPT</onMatch> 31 <!-- 用於配置不符合過濾條件的操做 --> 32 <onMismatch>DENY</onMismatch> 33 </filter> 34 <Encoding>UTF-8</Encoding> 35 <File>${Log_Home}/debug/debug.log</File> 36 <!-- 根據時間來制定滾動策略 --> 37 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 38 <FileNamePattern> 39 ${Log_Home}/debug/debug.%d{yyyy-MM-dd}.%i.log 40 </FileNamePattern> 41 <!-- 多久後自動清楚舊的日誌文件,單位:月 --> 42 <MaxHistory>1</MaxHistory> 43 <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 44 <!-- 默認值是 10MB,文檔最大值 --> 45 <MaxFileSize>2MB</MaxFileSize> 46 </TimeBasedFileNamingAndTriggeringPolicy> 47 </rollingPolicy> 48 49 <encoder> 50 <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</Pattern> 51 </encoder> 52 </appender> 53 54 <!-- INFO級別 --> 55 <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> 56 57 <!-- 級別過濾器 --> 58 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 59 <!-- 設置過濾級別 --> 60 <level>INFO</level> 61 <!-- 用於配置符合過濾條件的操做 --> 62 <onMatch>ACCEPT</onMatch> 63 <!-- 用於配置不符合過濾條件的操做 --> 64 <onMismatch>DENY</onMismatch> 65 </filter> 66 <Encoding>UTF-8</Encoding> 67 <File>${Log_Home}/info/info.log</File> 68 <!-- 根據時間來制定滾動策略 --> 69 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 70 <FileNamePattern> 71 ${Log_Home}/info/info.%d{yyyy-MM-dd}.%i.log 72 </FileNamePattern> 73 <!-- 多久後自動清楚舊的日誌文件,單位:月 --> 74 <MaxHistory>1</MaxHistory> 75 <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 76 <!-- 默認值是 10MB,文檔最大值 --> 77 <MaxFileSize>2MB</MaxFileSize> 78 </TimeBasedFileNamingAndTriggeringPolicy> 79 </rollingPolicy> 80 81 <encoder> 82 <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</Pattern> 83 </encoder> 84 </appender> 85 86 87 <!-- WARN級別 --> 88 <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> 89 90 <!-- 級別過濾器 --> 91 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 92 <!-- 設置過濾級別 --> 93 <level>WARN</level> 94 <!-- 用於配置符合過濾條件的操做 --> 95 <onMatch>ACCEPT</onMatch> 96 <!-- 用於配置不符合過濾條件的操做 --> 97 <onMismatch>DENY</onMismatch> 98 </filter> 99 <Encoding>UTF-8</Encoding> 100 <File>${Log_Home}/warn/warn.log</File> 101 <!-- 根據時間來制定滾動策略 --> 102 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 103 <FileNamePattern> 104 ${Log_Home}/warn/warn.%d{yyyy-MM-dd}.%i.log 105 </FileNamePattern> 106 <!-- 多久後自動清楚舊的日誌文件,單位:月 --> 107 <MaxHistory>1</MaxHistory> 108 <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 109 <!-- 默認值是 10MB,文檔最大值 --> 110 <MaxFileSize>2MB</MaxFileSize> 111 </TimeBasedFileNamingAndTriggeringPolicy> 112 </rollingPolicy> 113 114 <encoder> 115 <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</Pattern> 116 </encoder> 117 </appender> 118 119 <!-- ERROR級別 --> 120 <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> 121 122 <!-- 級別過濾器 --> 123 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 124 <!-- 設置過濾級別 --> 125 <level>ERROR</level> 126 <!-- 用於配置符合過濾條件的操做 --> 127 <onMatch>ACCEPT</onMatch> 128 <!-- 用於配置不符合過濾條件的操做 --> 129 <onMismatch>DENY</onMismatch> 130 </filter> 131 <Encoding>UTF-8</Encoding> 132 <File>${Log_Home}/error/error.log</File> 133 <!-- 根據時間來制定滾動策略 --> 134 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 135 <FileNamePattern> 136 ${Log_Home}/error/error.%d{yyyy-MM-dd}.%i.log 137 </FileNamePattern> 138 <!-- 多久後自動清楚舊的日誌文件,單位:月 --> 139 <MaxHistory>1</MaxHistory> 140 <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 141 <!-- 默認值是 10MB,文檔最大值 --> 142 <MaxFileSize>2MB</MaxFileSize> 143 </TimeBasedFileNamingAndTriggeringPolicy> 144 </rollingPolicy> 145 146 <encoder> 147 <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n</Pattern> 148 </encoder> 149 </appender> 150 151 152 153 <!-- 控制java下面包的打印,沒設置等級,將繼承上級root的等級,能夠指定包和類的打印級別,屬性additivity="false",能夠阻止打印日誌信息傳遞,默認爲true。--> 154 <logger name="com.test"/> 155 156 <!-- 當前日誌總級別爲TRACE、DEBUG、INFO、 WARN、ERROR、ALL和 OF --> 157 <!-- the level of the root level is set to DEBUG by default. --> 158 <root level="DEBUG"> 159 <appender-ref ref="STDOUT" /> 160 <appender-ref ref="FILE_DEBUG" /> 161 <appender-ref ref="FILE_INFO" /> 162 <appender-ref ref="FILE_WARN" /> 163 <appender-ref ref="FILE_ERROR" /> 164 </root> 165 </configuration>
注:appender至關於輸出口,注意父子級的疊加輸出post
運行LogBackConfigLoader中的main方法,控制檯輸出以下:url
1 2016-07-14 19:15:08 [main] DEBUG com.test.LogBackConfigLoader - 如今的時間是 Thu Jul 14 19:15:08 CST 2016 2 2016-07-14 19:15:08 [main] INFO com.test.LogBackConfigLoader - This time is Thu Jul 14 19:15:08 CST 2016 3 2016-07-14 19:15:08 [main] WARN com.test.LogBackConfigLoader - This time is Thu Jul 14 19:15:08 CST 2016 4 2016-07-14 19:15:08 [main] ERROR com.test.LogBackConfigLoader - This time is Thu Jul 14 19:15:08 CST 2016 5 Exception in thread "main" java.lang.ArithmeticException: / by zero 6 at com.test.LogBackConfigLoader.main(LogBackConfigLoader.java:36)
外部輸入日誌以下:spa