在项目中直接在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>
其配置参数说明如下图: