Jakarta-use log4j (java debugging tools)
Jakarta-use log4j (Java debugging tools)
1:
Jakarta log4j is a project mainly to the use of the debugger, in order to prevent in the process of writing a lot of System.out.println ();
I personally think that it is a good tool for program debugging good after you can switch off the log4j.
2: you can download from the following address downloaded to the ant, the current version: 1.13
Http://jakarta.apache.org/builds/jakarta-log4j/release/v1.1.3/
3: Installation
Log4j do not need to install you need only to log4j.jar, log4j-core.jar on the classpath of you can use.
4: use log4j
1:) copy log4j.jar, log4j-core.jar to classpath, in the tomcat4.03 you can put it in
Tomcat_home \ common \ lib directory.
2: Setting properties file
Log4j need to use a log4j.properties content of the document are as follows:
Log4j.properties
=====================================
Log4j.rootCategory =, A1
Log4j.appender.A1.layout = org.apache.log4j.PatternLayout
The simplest configuration, if that is very complicated and see their own files.
3: Write startup files
Log4j initialization can take several forms, I only provide a means 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. *;
Public class InitServlet extends HttpServlet (
Protected String config = "/ log4j.properties";
Public void init () (throws ServletException
ServletContext sct = getServletContext ();
System.out.println ( "[Log4j: The Root Path:" + sct.getRealPath ("/"));
System.out.println ( "[Log4j: InitServlet init start …");
Org.apache.log4j.PropertyConfigurator.configure (sct.getRealPath ("/") + getServletConfig (). GetInitParameter ( "log4j"));
System.out.println ( "[Log4j: InitServlet init over.");
)
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws IOException, ServletException (
;
)
Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws IOException, ServletException (
;
)
)
4: Setting Web. XML
Write in the servlet AppServer is to start running, so you need to configure web.xml
With the following:
<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>
<! - Relative aplication ROOT/log4j.properties path in the light of the above ->
</ Init-param>
<load-on-startup> 1 </ load-on-startup>
</ Servlet>
5: In the process of using log4j;
Private static org.apache.log4j.Category log =
Org.apache.log4j.Category.getInstance (classname);
/ / Classname java file name
Log.debug ("………");
Log.error ("………");
…..
Specific use log4j you can see the documents.
Tags: developement tools, Jakarta, java tools, log4j






