首页 > 代码库 > 剖析项目多个logback配置(下)
剖析项目多个logback配置(下)
来源:http://www.cnblogs.com/guozp/p/5973038.html
上篇大概描述了logback的加载顺序以及加载的源码,本篇将分析如果在你的Maven或者其他多模块的项目中,每个模块都存在logback.xml的情况,项目会加载哪个为准。
这里简单的测试下,我的service模块下有个logback.xml,其他的模块下也有,但是输出目录不同,以此来观察。
service模块:
<?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:
<?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,仅仅是适用配置文件中的值输出测试而已)
service模块的值:
log4j.rootLogger=INFO,A1,FF
dao模块的值:
log4j.rootLogger=INFO,A1,FF,dddddddd
common模块的值:
log4j.rootLogger=INFO,A1,FF,ccccccccc
用例:
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>
剖析项目多个logback配置(下)