來源:http://www.cnblogs.com/guozp/p/5973038.html html
上篇大概描述了logback的加載順序以及加載的源碼,本篇將分析若是在你的Maven或者其餘多模塊的項目中,每一個模塊都存在logback.xml的狀況,項目會加載哪一個爲準。java
這裏簡單的測試下,個人service模塊下有個logback.xml,其餘的模塊下也有,可是輸出目錄不一樣,以此來觀察。git
service模塊:spring
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="false" scanPeriod="60 seconds" debug="false"> <property name="LOG_HOME" value="D:/log" /> <property name="appName" value="index"></property> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <Encoding>UTF-8</Encoding> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </layout> </appender> <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Encoding>UTF-8</Encoding> <file>${LOG_HOME}/${appName}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log</fileNamePattern> <MaxHistory>10</MaxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>500MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </layout> </appender> <!-- Spring framework logger --> <logger name="org.springframework" level="error" additivity="false"></logger> <logger name="com.XX.XX" level="info" additivity="false"> <appender-ref ref="stdout" /> <appender-ref ref="appLogAppender" /> </logger> <root level="info"> <appender-ref ref="stdout" /> </root> </configuration>
其餘模塊配置,例如Dao:api
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="false" scanPeriod="60 seconds" debug="false"> <property name="LOG_HOME" value="D:/log" /> <property name="appName" value="index_dao"></property> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <Encoding>UTF-8</Encoding> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </layout> </appender> <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Encoding>UTF-8</Encoding> <file>${LOG_HOME}/${appName}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log</fileNamePattern> <MaxHistory>10</MaxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>500MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </layout> </appender> <!-- Spring framework logger --> <logger name="org.springframework" level="error" additivity="false"></logger> <logger name="com.XX.XX" level="info" additivity="false"> <appender-ref ref="stdout" /> <appender-ref ref="appLogAppender" /> </logger> <root level="info"> <appender-ref ref="stdout" /> </root> </configuration>
在不一樣的模塊有不一樣的用例,可是service模塊依賴dao和common模塊,觀察service模塊會加載哪一個文件,dao模塊又會加載哪一個文件,在用例中取出配置文件的某個值能夠直觀的看到你到底加載的那個配置文件(我本地使用的是log4j的配置文件,可是我項目中沒有log4j的jar,僅僅是適用配置文件中的值輸出測試而已)app
service模塊的值:測試
log4j.rootLogger=INFO,A1,FF
dao模塊的值:ui
log4j.rootLogger=INFO,A1,FF,dddddddd
common模塊的值:this
log4j.rootLogger=INFO,A1,FF,ccccccccc
用例:url
public class LogTest { private static Logger logger = LoggerFactory.getLogger(LogTest.class); public static void main(String[] args) throws Exception { logger.info("111111"); ClassLoader classLoader = LogTest.class.getClassLoader(); HashSet urlSet = new HashSet(); //看當前類路徑下存在的配置文件 Enumeration urlEnum = classLoader.getResources("log4j.properties"); while(urlEnum.hasMoreElements()) { URL url = (URL)urlEnum.nextElement(); urlSet.add(url); System.out.println(url); } //查找具備給定名稱的資源 URL url = classLoader.getResource("log4j.properties"); File file = new File(url.toURI()); BufferedReader bf = new BufferedReader(new FileReader(file)); String s = bf.readLine(); System.out.println(s); } }
輸出結果:
2016-10-20 16:26:08.439 [main] INFO com.jd.index.storm.LogTest - 111111
file:/D:/IdeaCode/gitCode/index_file/index_file_service/target/classes/log4j.properties
file:/D:/IdeaCode/gitCode/index_file/index_file_common/target/classes/log4j.properties
file:/D:/IdeaCode/gitCode/index_file/index_file_dao/target/classes/log4j.properties
log4j.rootLogger=INFO,A1,FF
輸出文件:
從結果能夠看出我在service模塊的用例加載的是當前模塊下的log4j配置文件,並且日誌使用的logback配置也是當前模塊下的,並無使用其餘模塊中的。
具體緣由能夠參考如下源碼:
public void autoConfig() throws JoranException { StatusListenerConfigHelper.installIfAsked(this.loggerContext); //在這裏加載真正的配置文件 URL url = this.findURLOfDefaultConfigurationFile(true); if(url != null) { this.configureByResource(url); } else { BasicConfigurator.configure(this.loggerContext); } } public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = this.findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if(url != null) { return url; } else { //經過getResource返回目標文件的URL url = this.getResource("logback.groovy", myClassLoader, updateStatus); if(url != null) { return url; } else { url = this.getResource("logback-test.xml", myClassLoader, updateStatus); return url != null?url:this.getResource("logback.xml", myClassLoader, updateStatus); } } } private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { //經過調用類加載器加載 URL url = Loader.getResource(filename, myClassLoader); if(updateStatus) { //輸出警告 this.statusOnResourceSearch(filename, myClassLoader, url); } return url; } public static URL getResource(String resource, ClassLoader classLoader) { try { //經過調用類加載器加載 return classLoader.getResource(resource); } catch (Throwable var3) { return null; } }
ClassLoader中的方法:
此方法首先搜索資源的父類加載器;若是父類加載器爲 null,則搜索的路徑就是虛擬機的內置類加載器的路徑。若是搜索失敗,則此方法將調用 來查找資源。findResource(String)
public URL getResource(String name) { URL url; if (parent != null) { url = parent.getResource(name); } else { url = getBootstrapResource(name); } if (url == null) { url = findResource(name); } return url; }
注意我用例中使用的另外一個方法:
public Enumeration<URL> getResources(String name)
資源的 URL
對象的枚舉。若是找不到資源,則該枚舉將爲空。類加載器無權訪問的資源不在此枚舉中。
public Enumeration<URL> getResources(String name) throws IOException { Enumeration[] tmp = new Enumeration[2]; if (parent != null) { tmp[0] = parent.getResources(name); } else { tmp[0] = getBootstrapResources(name); } tmp[1] = findResources(name); return new CompoundEnumeration<>(tmp); }
至此能夠看出,logback加載配置文件順序是調用的類加載器原生的方法,因此加載的順序以及要加載哪一個配置天然和原生的類加載器同樣。
簡單來講,看你的程序運行在哪一個模塊,運行時會加載相應的模塊的日誌配置,並不使用其餘模塊下的日誌配置,雖然同在類路徑下。
本項目包含Storm和其餘的模塊,可是Storm輸出日誌的時候並不會使用其餘模塊下的logback的配置,緣由就在這裏。
在這裏順道嘮叨幾句Storm日誌問題,不少人對此仍是有仍是有些模糊的,例如使用的是那個篇日誌文件,文件名稱又是哪裏定義的。
storm使用logback做爲日誌服務插件,配置文件在$STORM_HOME/logback/cluster.xml 。
對於storm,咱們關心的主要是worker、nimbus、supervisor等日誌(worker-xxxx.log,nimbus.log,supervisor.log),
<appender name="A1" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${storm.home}/logs/${logfile.name}</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${storm.home}/logs/${logfile.name}.%i</fileNamePattern> <minIndex>1</minIndex> <maxIndex>9</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>1024MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n</pattern> </encoder> </appender> <appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${storm.home}/logs/access.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${storm.home}/logs/access.log.%i</fileNamePattern> <minIndex>1</minIndex> <maxIndex>9</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>100MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n</pattern> </encoder> </appender> <appender name="METRICS" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${storm.home}/logs/metrics.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>metrics.log.%i</fileNamePattern> <minIndex>1</minIndex> <maxIndex>9</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>2MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d %-8r %m%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="A1"/> </root>