log4j介绍

log4j.properties文件的三种加载方式

1.默认自动加载

  满足以下条件时:

    1).配置文件名为log4j.properties

    2).在classpath根目录下(即resources根目录下)

  spring会自动加载log4j.properties文件,无需显式加载.

  参考文章 : https://blog.csdn.net/javanotes/article/details/78389727

2.spring手动加载

  使用spring提供的Log4jConfigListener,在web.xml中加载配置文件

<!-- 设置Log4j配置文件位置 -->
<context-param> 
  <param-name>log4jConfigLocation</param-name> 
  <param-value>classpath:properties/log4j.properties</param-value> 
</context-param> 
 <!-- 刷新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> 
View Code

3.在类中加载配置文件

public  class TestLog4j  {
  public  static  void main(String[] args)  {
    PropertyConfigurator.configure( " D:/Code/conf/log4j.properties " );
    Logger logger = Logger.getLogger(TestLog4j. class );
    logger.debug( " debug " );
    logger.error( " error " );
  } 
} 
View Code

 

posted @ 2019-03-31 16:42  Kaneha  阅读(320)  评论(0编辑  收藏  举报