Recently used Org.slf4j.Logger, and suddenly found log also have a lot of wins to consider. This is a simple introduction to the configuration bar.
WEB Project under the words, first located in Web-app/web-inf/web.xml this everyone is very familiar with the bar ~ inside the configuration as follows
<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>
See above, must be through this loaded, first set the environment variable log4jconfiglocation=/web-inf/conf/log4j.xml then log4jconfiglistener to read load this configuration.
The Log4jconfiglistener code is as follows:
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 ());
}
Then it must be log4jwebconfigurer.initlogging (Event.getservletcontext ()); This one reads.
After following this method, we found that the method passed through a series of read configuration file addresses and finally executed the following method
Log4jconfigurer.initlogging (location);
To understand the following context, you must first get the environment variable log4jconfiglocation=/web-inf/conf/log4j.xml and then read the configuration file. So the most core method is log4jconfigurer.initlogging (location); So even if it's not a Web project (for example, thrift) directly use the following spring configuration to directly add log4j configuration
<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, the above is a simple configuration file.