LOG4J PATH environment variable configuration and log4j load configuration

Source: Internet
Author: User

The 1.lo4j log path is read from the environment variable, and the Log4j.xml configuration is as follows:

The specific configuration is as follows: log4j.appender.r.encoding=utf-8log4j.appender.r= Org.apache.log4j.dailyrollingfileappenderlog4j.appender.r.append=true#log4j.appender.r.threshold= Infolog4j.appender.r.file=${log4j.home}/logs/log.loglog4j.appender.r.datepattern= '. ' yyyy-mm-ddlog4j.appender.r.layout=org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpattern=%-d{ Yyyy-mm-dd HH\:MM\:SS} [%c]-[%p]%m%n

1.1 Monitoring configuration Web. xml

  < Listener >      < Listener-class >com.jumper.logview.servlet.Log4jlistener</listener-class>    </listener>  

1.2 Monitoring Code

 Public classLog4jlistenerImplementsServletcontextlistener {Final StaticString log4j_home = "Log4j.home"; Final StaticString env_home = "Log4j_home";  Public voidcontextdestroyed (servletcontextevent servletcontextevent) {system.getproperties (). Remove (Log4j_home); }         Public voidcontextinitialized (servletcontextevent servletcontextevent) {String logshome=system.getenv (env_home); if(Logshome = =NULL) {Logshome= System.getproperty ("catalina.home") + "/log4j"; }system.out.println ("*********log4j dir:" +logshome);    System.setproperty (Log4j_home, logshome); }}

Two ways to load 2.log4j

2.1 With Spring load configuration

     <Context-param>        <Param-name>Log4jconfiglocation</Param-name>        <Param-value>/web-inf/classes/config/log4j.properties</Param-value>    </Context-param>    <!--Spring Refreshes the interval of log4j configuration file changes in milliseconds -    <Context-param>        <Param-name>Log4jrefreshinterval</Param-name>        <Param-value>1000000</Param-value>    </Context-param>    <Listener>        <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class>    </Listener>

2.2 Loaded with Serlvet

Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;ImportOrg.apache.log4j.PropertyConfigurator; Public classLog4jinitextendsHttpServlet {Private Static Final LongSerialversionuid = 1L;  Public voiddestroy () {Super. Destroy (); }     PublicLog4jinit () {Super(); }    /*** Initialization of the servlet. <br> * *@throwsservletexception * If an error occurs*/     Public voidInit ()throwsservletexception {String file= This. Getinitparameter ("log4j");//read from the Web. XML configuration, the name must be consistent with the Web. XML Configuration        if(File! =NULL) {propertyconfigurator.configure ( This. Getservletcontext (). Getrealpath (file)); }}}web.xml Configuration:<servlet> <servlet-name>Log4jInit</servlet-name> <servlet-class>com.jumper.log4j.log4jinit</servlet-class> <init-param> <param-name>log4j</param-name>//the name is the identity of the bottom path configuration (like key)<param-value>/WEB-INF/classes/config/log4j.properties</param-value>//This is the path to load the log4j configuration file when the container initializes (this is like a value);</init-param> <load-on-startup>1</load-on-startup> </servlet>

LOG4J PATH environment variable configuration and log4j load configuration

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.