Log4j中配置文件自动加载原理及非SRC下使用

本文详细解析了Log4j如何自动加载位于WEB-INF/classes下的log4j.properties或log4j.xml配置文件,并介绍了当这些文件位于其他路径时的加载方式,包括在Spring或web.xml中的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中直接在src(即,编译后的WEN-INF/classes)下放置log4j.properties or log4j.xml,项目会自动加载并打印日志。

【1】自动加载原理

JVM加载log4j的类(org.apache.log4j.LogManager)后,执行静态代码块,会尝试加载默认路径下(WEN-INF/classes)的log4j.properties or log4j.xml:

public class LogManager {

  /**
   * @deprecated This variable is for internal use only. It will
   * become package protected in future versions.
   * */
  static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
  
  static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";  
   
  /**
   * @deprecated This variable is for internal use only. It will
   * become private in future versions.
   * */
  static final public String DEFAULT_CONFIGURATION_KEY="log4j.configuration";

  /**
   * @deprecated This variable is for internal use only. It will
   * become private in future versions.
   * */
  static final public String CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";

  /**
  * @deprecated This variable is for internal use only. It will
  * become private in future versions.
  */
  public static final String DEFAULT_INIT_OVERRIDE_KEY = 
"log4j.defaultInitOverride";
...//

部分静态代码块:

 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); 
			}	
      }
      ...//
 }

如下图所示配置了加载的两种配置文件:

这里写图片描述


【2】非src路径下

如果log4j.properties or log4j.xml 放到了另外路径下,比如src/config ,那么可以在spring.xml 或者 web.xml加载该配置文件,如下:

spring方式加载,配置于web.xml中

Spring加载log4j.properties,它提供了一个Log4jConfigListener,本身就能通过web.xml配置从指定位置加载log4j配置文件和log4j的输出路径。

要注意的是Log4jConfigListener必须要在Spring的Listener之前。

<!-- 设置由Sprng载入的Log4j配置文件位置 -->

<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>WEB-INF/classes/config/log4j.properties</param-value>  
</context-param>  
  <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 -->
<context-param>  
    <param-name>log4jRefreshInterval</param-name>  
    <param-value>10000</param-value>  
</context-param>  
<listener>  
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>

其配置参数说明如下图:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值