zoukankan      html  css  js  c++  java
  • log4j.properties log4j.xml 路径问题

    自动加载配置文件:

    (1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解。log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找log4j.properties文件。然后加载配置。配置文件放置位置正确,不用在程序中手动加载log4j配置文件。如果将配置文件放到了config文件夹下,在build Path中设置下就好了。

    若要手动加载配置文件如下:

    (1)PropertyConfigurator.configure("log4j.properties") 默认读取的是项目根目录的路径。此时的log4j.properties要放在项目目录下。

    如图,log4j.properties和src是同级目录,同在根目录下

    (2)一般,一个Java项目会有很多的配置文件,建议把所有的配置文件放到一个文件夹下,

    例如,放到config文件夹。那么在读取这些配置文件的时候要加上子目录名称。

    如图在项目目录下创建config文件夹(注意:不是在src文件下),此时,config和src是同级目录

    这时,读取路径改为:

    PropertyConfigurator.configure("config/log4j.properties");

    (3)项目打成jar包时,一般不会把配置文件也打进jar包。

    如果是第一种方式,直接将log4j.properties文件和生成的HelloWorld.jar放在同一目录下,项目就能顺利读取配置文件。

    如果是第二种方式,要建立config文件夹,把配置文件放入其中,再将config文件和生成的HelloWorld.jar放在同一目录下,项目就能顺利读取配置文件。

     思考:log4j.properties配置文件,配置简单,但不支持复杂过滤器filter,log4j.xml虽然配置文件看似复杂,但支持复杂过滤器和Log4j的新特性。推荐使用log4j.xml

    ===================================================================================================================

    三、加载log4j.properties文件

    1、spring方式加载,配置与web.xml中:

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

    Log4jConfigListener必须要在Spring的Listener之前。

    web.xml

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

    <context-param>

    <param-name>log4jConfigLocation</param-name>

    <param-value>WEB-INF/classes/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>

    2、可以通过资源类对资源文件进行加载,与使用为一体

    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 " );

    }

    四、在程序中的使用 
    在程序中使用Log4j之前,首先要将commons-logging.jar和logging-log4j-1.2.9.jar导入到classpath中,并将log4j.properties放于src根目录中。接下来就可以使用了。 

    1.得到记录器 
    使用Log4j,第一步就是获取日志记录器,这个记录器将负责控制日志信息。其语法为: 
    public static Logger getLogger( String name), 
    通过指定的名字获得记录器,如果必要的话,则为这个名字创建一个新的记录器。Name一般取本类的名字,比如: 
    static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () ) ; 
    注:推荐使用commons-logging结合log4j进行日志记录 
    private static Log logger = LogFactory.getLog(Yourclass.class); 

    2.插入记录信息(格式化日志信息) 
    当上两个必要步骤执行完毕,您就可以轻松地使用不同优先级别的日志记录语句插入到您想记录日志的任何地方,其语法如下: 
    Logger.debug ( Object message ) ; 
    Logger.info ( Object message ) ; 
    Logger.warn ( Object message ) ; 
    Logger.error ( Object message ) ; 

    例如:

     import org.apache.log4j.*;
      
     public class LogTest ...{    
         static Logger logger = Logger.getLogger(LogTest.class.getName());
      
         public static void main(String[] args) ...{
    	//通过PropertyConfigurator加载log4j.properties文件,如果不添加这句话,则有spring加载  
             PropertyConfigurator.configure ( “.srclog4j.properties”);
              logger.debug("Debug ...");
      
             logger.info("Info ...");
      
             logger.warn("Warn ...");
      
             logger.error("Error ...");
     
         }
     }
  • 相关阅读:
    Python 集合
    Python sorted()
    CodeForces 508C Anya and Ghosts
    CodeForces 496B Secret Combination
    CodeForces 483B Friends and Presents
    CodeForces 490C Hacking Cypher
    CodeForces 483C Diverse Permutation
    CodeForces 478C Table Decorations
    CodeForces 454C Little Pony and Expected Maximum
    CodeForces 313C Ilya and Matrix
  • 原文地址:https://www.cnblogs.com/maoyizhimi/p/7250913.html
Copyright © 2011-2022 走看看