在项目中指定log4j.xml的位置

一、默认位置

1、项目会默认在classpath目录下读取log4j.xml或log4j.properties。

 

二、在代码中指定

1、 try {

Log4jConfigurer.initLogging("file:/opt/app/config/log4j.xml");

                        Log4jConfigurer.initLogging

 

} catch (FileNotFoundException e) {
e.printStackTrace();

 

}

 

三、在spring配置文件中指定

1、web项目---在web.xml中指定

a、在pom.xml中添加依赖


    org.slf4j
    slf4j-log4j12
    1.7.13

b、在web.xml中添加log4j监听及log4j.xml配置文件的位置


    org.springframework.web.util.Log4jConfigListener


    log4jConfigLocation
    file:opt/app/config/log4j.xml

 

2、普通项目---在applicationContext.xml中指定

class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">




file:/opt/app/config/log4j.xml


 

 

 

 


    log4jConfigLocation
    WEB-INF/log4j.xml





    org.springframework.web.util.Log4jConfigListener

pom.xml

    org.slf4j
    slf4j-log4j12
    1.7.13

你可能感兴趣的:(JAVA)