一个java项目是如何自动加载log4j.properties
来源:互联网 发布:mac重装系统无法启动 编辑:程序博客网 时间:2024/06/05 23:56
来源:http://www.cnblogs.com/alipayhutu/archive/2013/04/18/3028249.html
今天看代码,发现log4j.properties。没有相应的加载代码,但它却生效了,这多神奇!
看进去,org.apache.log4j, LogManager.java,其有一个static方法块:
static { // By default we use a DefaultRepositorySelector which always returns 'h'. Hierarchy h = new Hierarchy(new RootLogger((Level) Level.DEBUG)); repositorySelector = new DefaultRepositorySelector(h); /** Search for the properties file log4j.properties in the CLASSPATH. */ String override =OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY, null); // if there is no default init override, then get the resource // specified by the user or the default config file. if(override == null || "false".equalsIgnoreCase(override)) { String configurationOptionStr = OptionConverter.getSystemProperty( DEFAULT_CONFIGURATION_KEY, null); String configuratorClassName = OptionConverter.getSystemProperty( CONFIGURATOR_CLASS_KEY, null); URL url = null; // if the user has not specified the log4j.configuration // property, we search first for the file "log4j.xml" and then // "log4j.properties" if(configurationOptionStr == null) { url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE); if(url == null) { url = Loader.getResource(DEFAULT_CONFIGURATION_FILE); } } else { try { url = new URL(configurationOptionStr); } catch (MalformedURLException ex) { // so, resource is not a URL: // attempt to get the resource from the class path url = Loader.getResource(configurationOptionStr); } } // If we have a non-null url, then delegate the rest of the // configuration to the OptionConverter.selectAndConfigure // method. if(url != null) { LogLog.debug("Using URL ["+url+"] for automatic log4j configuration."); OptionConverter.selectAndConfigure(url, configuratorClassName, LogManager.getLoggerRepository()); } else { LogLog.debug("Could not find resource: ["+configurationOptionStr+"]."); } } }
换句话说,是什么呢?也就是说:
1. 获取系统属性,看是否用户设置了override。默认是不设置的。
2. 如果确实没有设置,那么尝试找一下,有没有log4j.xml,有则加载。
3. 如果还没有,那么尝试找一下,有没有log4j.properites,有则加载。
其中,2、3里提到的“尝试找一下”,可能是去哪个目录里面找呢?翻译了一下,效果不好,还是上原文清晰 :
- Search for
resource
using the thread context class loader under Java2. If that fails, search forresource
using the class loader that loaded this class (Loader
). Under JDK 1.1, only the the class loader that loaded this class (Loader
) is used.- Try one last time with
ClassLoader.getSystemResource(resource)
, that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
所以,你把log4j.xml或log4j.properties放在这些目录下,那么log4j会“自动去加载”到,不用程序里手工写加载代码了。
但我个人,还是倾向于自己写加载。因为这种“悄悄被人做掉”,一是代码很难理解,二是假如A同学放了一个log4j,B同学又写了一个放在其他目录,这种默认加载机制,不一定哪个生效及生效顺序。这种不确定性,还是自己写两行代码,消灭在摇篮里吧。
阅读全文
0 0
- 一个java项目是如何自动加载log4j.properties
- Log4j.properties文件自动加载
- log4j.properties文件自动加载
- log4j,如何“自动加载”?
- Java日志 - 如何在项目中指定log4j.properties路径
- java 配置文件properties自动加载类
- 如何优雅地加载一个properties配置文件
- log4j的使用与java中properties配置文件加载
- log4j的使用与java中properties配置文件加载
- Java Web项目加载properties资源文件
- Log4j.properties文件的加载
- log4j.properties配置与加载
- log4j.properties配置与加载
- java中如何加载Properties文件
- java项目使用log4j工具类(非web项目)以及log4j.properties文件详解
- web项目log4j.properties配置
- java应用(非web应用)中log4j.properties动态修改配置文件,无需重启,就能立即生效---自动加载
- log4j自动加载原理
- redis常用命令
- windows github教程
- Ubuntu16.04 自己创造环境变量
- 海航王健领导各航企打造卓越服务品牌
- [python] 时间序列分析之ARIMA
- 一个java项目是如何自动加载log4j.properties
- 多表查询的几种方法
- MVC模式(三层架构模式)-转载
- 技术路上,多个同伴...
- Region拆分策略
- go-kit 上手之example stringsvc3 通过代理实现分布式处理
- 梦里不觉秋已深
- IntelliJ IDEA Spring Boot(9)打成war包
- 宋宝华:LEP(Linux易用剖析器) 是什么,为什么以及怎么办(2)