[Initialization configuration of log4j in original]web application __web

Source: Internet
Author: User
Tags log4j

In a normal Web project, you can write the following XML configuration file:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE log4j:configuration SYSTEM "LOG4J.DTD" >
<log4j:configuration xmlns:log4j= "http://jakarta.apache.org/log4j/" >

<appender name= "File.log" class= "Org.apache.log4j.RollingFileAppender" >
<!--you must use an absolute path (or relative to Tomcat's path)-->
<param name= "File" value= "C:/logs/myweb.log"/>
<param name= "Append" value= "false"/>
<layout class= "Org.apache.log4j.PatternLayout" >
<param name= "Conversionpattern" value= "%-d{yyyy-mm-dd HH:mm:ss} [%c:%m ()]-[%p]%m%n"/>
</layout>
</appender>

<appender name= "Console.log" class= "Org.apache.log4j.ConsoleAppender" >
<layout class= "Org.apache.log4j.PatternLayout" >
<param name= "Conversionpattern" value= "%5p (%f:%l)-%m%n"/>
</layout>
</appender>

<!--name Here is the name of the package to be printed, which means that all the intermediate information in the Org.woden module (including the child modules) is printed-->
<logger name= "Org.woden" additivity= "false" >
<level value= "DEBUG"/>
<appender-ref ref= "Console.log"/>
<appender-ref ref= "File.log"/>
</logger>

<!--here root is configured to fatal level to prevent printing of intermediate information in the frame (solve the problem of printing Struts intermediate information)-->
<root>
<level value= "FATAL"/>
<appender-ref ref= "Console.log"/>
<appender-ref ref= "File.log"/>
</root>
</log4j:configuration>

Then write a servlet yourself, named Log4jinit, with the following code:

Package org.woden.controller.servlet;

Import java.io.IOException;

Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.log4j.xml.DOMConfigurator;

public class Log4jinit extends HttpServlet {
public void init (ServletConfig config) throws servletexception {
Super.init (config);
String prefix = Getservletcontext (). Getrealpath ("/");
String file = Getinitparameter ("log4j");
System.out.println ("--------log4j Start---------");
if (file!= null) {
Domconfigurator.configure (prefix + file);
}
}

protected void doget (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
}
}

Then configure the servlet boot in Web.xml:

<servlet>
<servlet-name>MyLog4jInit</servlet-name>
<servlet-class>org.woden.controller.servlet.Log4jInit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/log4j.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Note: The startup level is best set to a smaller point, indicating faster startup. Some Ides typically set the struts (Action) startup level to 3 or 2, preferably by putting the log4j boot before struts.

In addition, the log4j is enhanced in the Spring framework:
Instead of writing a servlet to start log4j, you can use spring's log4jconfiglistener listener directly. There are several benefits to doing this, and the biggest benefit is that the log4j level (and policy) is changed without restarting the Web application. And spring allows log files to be placed in the project's relative directory.

The configuration statements in Web.xml in spring are as follows:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value><!--60 Second scan once-->
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

In the log4j configuration file, make the appropriate modifications:
<param name= "File" value= "${webapp.root}/web-inf/logs/test.log"/><!--put the log file in the project relative directory-->

(The end)

This article refers to the CSDN article: http://dev.csdn.net/develop/article/81/81782.shtm

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.