Log4j in Spring uses

Source: Internet
Author: User
Tags log4j

A dedicated servlet is typically used in Web applications to complete the log4j configuration and ensures that the servlet is in the Web.xml configuration before other servlet to invoke in the servlet and JSP. Here's the servlet, the code is as follows:
Import org.apache.log4j.*;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Log4jinit extends HttpServlet {
public void init () {
String prefix = Getservletcontext (). Getrealpath ("/");
String file = Getinitparameter ("log4j");//configuration file location
if (file!= null) {
Propertyconfigurator.configure (Prefix+file);
}
}

}

This servlet is configured in Web.xml:
</web-app>
...........
Servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>example. Log4jinit</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...........
</web-app>

The property file used to configure log4j:
Log4j.rootlogger=debug, A1, R
Log4j.appender.a1=org.apache.log4j.consoleappender
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
Log4j.appender.a1.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n
Log4j.appender.r=org.apache.log4j.rollingfileappender
Log4j.appender.r.file=log4j.log
log4j.appender.r.maxfilesize=100kb
Log4j.appender.r.maxbackupindex=1
Log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p%t%c-%m%n
This configuration file specifies two output sources A1 and R. The former output log information to the console, the latter is a rotary log file. The largest file is 100KB, when a log file reaches the maximum size, log4j will automatically rename Example.log to Example.log.1, and then rebuild a new Example.log file, turn the rotation.

Test file test.jsp:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "org.apache.log4j.*"%>
<%
Logger Logger = Logger.getlogger ("test.jsp");
Logger.debug ("befor test");
%>
<% logger.info ("after test");%>

Format of Patternlayout

%r the number of milliseconds consumed since the program started
%t represents the thread that the logging request generated
%p represents the priority level of a log statement
The class name where the%c log information resides
%m%n represents the contents of the log information
%r the number of milliseconds it takes to start from application to output this log information
%d log point-in-time date or time, the default format is ISO8601, or the format can be specified thereafter, such as:%d{yyy MMM dd hh:mm:ss,sss}, Output is similar: October 18, 2002 22:10:28,921
%l where the log event occurred, including the class name, the thread that occurred, and the number of lines in the code.


log4j Configuration in Spring
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>

<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

<listener>
<listener-class>
Org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>

Where the file is saved
Log4j.appender.r.file=${webapp.root}/logs/chengchuang.log

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.