Log4j with spring configuration loading mechanism

Source: Internet
Author: User
Tags log4j


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.