最近经常使用org.slf4j.Logger , 突然间发现log也有很多细活去考虑。这算是一篇浅显的入门配置吧。
web项目下的话,首先位于web-app/WEB-INF/web.xml 这个大家都很熟悉了吧~ 里面的配置如下
<context-param>
<param-name><span style="color:#3366ff;background-color: rgb(204, 204, 204);">log4jConfigLocation</span></param-name>
<param-value>/WEB-INF/conf/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
一看到上面 , 肯定就是通过这个加载的啦,首先设置环境变量log4jConfigLocation=/WEB-INF/conf/log4j.xml 然后后面的Log4jConfigListener去读取加载这个配置。
Log4jConfigListener代码如下:
public class Log4jConfigListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
Log4jWebConfigurer.initLogging(event.getServletContext());
}
@Override
public void contextDestroyed(ServletContextEvent event) {
Log4jWebConfigurer.shutdownLogging(event.getServletContext());
}
}
那么肯定是 Log4jWebConfigurer.initLogging(event.getServletContext()); 这个读取的喽
跟进这个方法之后我们发现该方法中经过一系列的读取配置文件地址最后执行下面的方法
Log4jConfigurer.initLogging(location);
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:conf/log4j.xml</value>
</list>
</property>
</bean>
OK ,以上就是简单的配置文件喽.