log4j log output to Web project specified folder

Source: Internet
Author: User

Thank you eric2500 for this article: http://www.cxyclub.cn/n/27860/

Summary: Try to output the log4j file log to the Web engineering directory, encountered a lot of problems, and finally under the guidance of eric2500, the following is the record. The principle is that the log4j configuration file supports the environment variables of the server's VM, such as ${oss.log4j.path}, and the log file path is set with System.setproperty ("" ") before log4j load the configuration file. This is done through an initial servlet.

Steps:

1. Configure Log4j.properties

Note: 1. The output log as console is commented out; 2.log4j's Layout.conversionpattern property is not written as HTML output is useless (HTML has its own format).

log4j.rootcategory=debug,stdout,rollfile,r# Output as console. Now comment it out because I Don' t want to use it. #log4j. appender.stdout=Org.apache.log4j.consoleappender#log4j.appender.stdout.layout=Org.apache.log4j.patternlayout#log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%5p (%f:%l)-%m%n# output as FILELOG4J.APPENDER.R=Org.apache.log4j.rollingfileappenderlog4j.appender.r.layout=Org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%5p (%c:%l)-%m%Nlog4j.appender.r.file=${log4j.root.path}logs/log.loglog4j.appender.r.maxfilesize=100kblog4j.appender.r.maxbackupindex=1# Output as html# Conversionpattern to no effect as output HTML Log4j.appender.ROLLFILE=Org.apache.log4j.DailyRollingFileAppenderlog4j.appender.ROLLFILE.layout=Org.apache.log4j.HTMLLayoutlog4j.appender.ROLLFILE.File= ${log4j.root.path}logs/Log.htmllog4j.appender.ROLLFILE.Append=trueLog4j.appender.ROLLFILE.Threshold= DEBUG

2. Configure Web. xml

Note:<init-param> The <param-value> property for the specified log4j storage address, which is not set here, is specified in Systemservlet that handles LOG4J's storage address.

<!--set Configuration path for log4j (under Class directory) -     <Context-param>          <Param-name>Log4jconfiglocation</Param-name>          <Param-value>Classpath:log4j.properties</Param-value>     </Context-param>    <!--load the log4j configuration file -    <Listener>      <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class>    </Listener>        <!--set the storage address of the log4j in the custom servlet. You do not need to configure the Servlet-mapping,load-on-startup setting to 0 to mount the servlet when the Web container is started.  -    <servlet>        <Servlet-name>Systemservlet</Servlet-name>        <Servlet-class>Com.wql.util.SystemServlet</Servlet-class>        <Init-param>            <Param-name>Log4j.root.path</Param-name>            <Param-value></Param-value>        </Init-param>        <Load-on-startup>0</Load-on-startup>    </servlet>

3. Write systemservlet to handle the storage address of the log4j

Note: The storage address of the log4j under the physical path published by this project: E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\OA2_02\logs

 PackageCom.wql.util;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;ImportOrg.apache.log4j.Logger; Public classSystemservletextendshttpservlet{Private Static FinalLogger Logger = Logger.getlogger (systemservlet.class); Private Static Final LongSerialversionuid = -833322220864312415l; @Override Public voidInit ()throwsservletexception {//Get the root path (Web publishing address)String RootPath = This. Getservletcontext (). Getrealpath ("/"); //Print root Path at info levelLogger.info (RootPath); //gets the value param-name to Log4j.root.path from Web. XML-that is, the storage address of the specified log4jString Log4jpath = This. Getservletconfig (). Getinitparameter ("Log4j.root.path"); //If you do not specify a log4j.root.path initial parameter, use the Web's project directory-that is, the web's publishing addressLog4jpath = (log4jpath==NULL||"". Equals (Log4jpath))?Rootpath:log4jpath; //set the storage address of the log4j to System PropertiesSystem.setproperty ("Log4j.root.path", Log4jpath); //Initialize        Super. Init (); }}

Done! After you start the server, you can view the logs in: E:\Spring2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\OA2_02\logs.

Note:

1. Configure log4j in this manner the Logs\log.log deny IO stream Access exception information appears when you start the server, as follows: (Do not control it, there is no impact.) )

Attached 1:log4j log file output to the specified disk: http://blog.csdn.net/mxlxiao7/article/details/7340480

Details of the attached 2:lo4j are: http://www.blogjava.net/hwpok/archive/2008/01/16/175711.html

log4j log output to Web project specified folder

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.