One: Introduction
LOG4J is a Jakarta project is mainly for debugging use, in order not to let is in the program to write a lot of System.out.println ();
I personally think is a good tool, the program after debugging, you can turn off the log4j.
Two: Download
You can download from the address below to ant, current version: 1.13
http://jakarta.apache.org/builds/jakarta-log4j/release/v1.1.3/
Three: Installation
log4j do not need to install, you only need to put Log4j.jar,log4j-core.jar in the classpath you can use.
IV: Using LOG4J
1: Copy Log4j.jar,log4j-core.jar to Classpath, in tomcat4.03 you can put it in
The Tomcat_home\common\lib directory.
2: Set the properties file
Log4j need to use a log4j.properties file with the following contents:
Log4j.properties
=====================================
log4j.rootcategory=, A1
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
The simplest configuration, if you want to be complex, look at the document yourself.
3: Write Startup file
There are several ways to initialize log4j, and I only provide one way below.
Initservlet.java (a servlet)
==================================
Import Java.io.InputStream;
Import java.io.IOException;
Import java.util.Hashtable;
Import Java.util.Locale;
Import java.util.MissingResourceException;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import javax.servlet.UnavailableException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import javax.servlet.*;
Import java.io.*;
4: Set Web.xml
The servlet that is written above is appserver to run, so you need to configure the Web.xml
Add the following content:
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>com. Initservlet</servlet-class>
<init-param>
<param-name>log4j</param-name>
<param-value>/log4j.properties</param-value>
<!--the relative aplication path is above root/log4j.properties reference-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
5: Use log4j in the program;
private static Org.apache.log4j.Category log =
Org.apache.log4j.Category.getInstance (classname);
ClassName Java filename \ r
Log.debug ("...");
Log.error ("...");
.....
You can look at log4j's documents in concrete use.
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.