1. Basic knowledge:
Log4j's Chinese documentation (this is based on the latest log4j (jakarta-log4j-1.2.8) Development package comes with the manual translation of the document)
Http://dev.csdn.net/develop/article/29/29441.shtm
The corresponding English text:
Short Introduction to Log4j
Http://logging.apache.org/log4j/docs/manual.html
2. Steps
1) Log4j.jar placed under Jsp-examples\web-inf\lib;
2) Create a new properties\log4j.properties under Jsp-examples;
3) log4j.properties content:
#log4j. Properties
Log4j.rootlogger=info, A1
Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a1.file=${webapphome}/logs/tomcat_log_
Log4j.appender.a1.datepattern=yyyy-mm-dd '. html '
Log4j.appender.a1.layout=org.apache.log4j.htmllayout
4) "F:\UserWorkSpace\jiangcm\Tomcat 5.0\webapps\jsp-examples\" to create a new property file set by the storage
Logs folder for log files;
Comments:
Tomcat_log_ is a file name, you can not create a new file beforehand, and if it is pre-built, the log file is directly tomcat_log_2007-
03-05.html format, if no previous file was created, the log file name generated in the first time is called Tomcat_log_;
5) Enter the catalogue "F:\UserWorkSpace\jiangcm\Tomcat 5.0\webapps\jsp-examples\web-inf\classes", new
Build "Log4j\log4jinit.java";
6) Log4jinit.java content:
Package log4j;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import org.apache.log4j.*;
public class Log4jinit extends HttpServlet {
public void Init () throws Servletexception {
String prefix = Getservletcontext (). Getrealpath ("/");
String test = Getservletcontext (). Getrealpath ("");
System.out.println (prefix);
SYSTEM.OUT.PRINTLN (test);
System.setproperty ("Webapphome", test);
String file = Getservletconfig (). Getinitparameter ("Log4j-config-file");
System.out.println (Prefix+file);
Read the log4j configuration file from the servlet parameter
if (file! = null) {
Propertyconfigurator.configure (prefix + file);
}
}
public void doget (HttpServletRequest request,httpservletresponse response) throws
IOException, Servletexception {}
public void DoPost (HttpServletRequest request,httpservletresponse response) throws
IOException, Servletexception {}
}
Attention:
"System.setproperty (" Webapphome ", test); This line of code will appear in the "Propertyconfigurator.configure
(prefix + file); " This line of code before, because this is to set the "Webapphome" value, the Log4j.properties file "
Log4j.appender.a1.file=${webapphome}/logs/tomcat_log_ "${webapphome}" in this environment variable is only
Assignment, otherwise the log file cannot be output;
7) Set the "Jsp-examples\web-inf\web.xml" so that the Log4jinit.java this servlet automatically starts, in the Web. xml
Added in:
<servlet>
<servlet-name>log4jinit</servlet-name>
<servlet-class>log4j. Log4jinit</servlet-class>
<init-param>
<param-name> Log4j-config-file </param-name>
<param-value>/properties/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note: The above load-on-startup should be set to 1 so that the servlet is loaded when the Web container starts. Log4j.properties file
Put it in the properties subdirectory of the root, or you can put it in a different directory. The. properties file should be centrally stored so that the
Management.
8) Use log4j in WebApp, enter directory "F:\UserWorkSpace\jiangcm\Tomcat 5.0\webapps\jsp-
Examples\web-inf\classes\dates ", modify Jspcalendar.java, plus log information:
Jspcalendar.java
Package dates;
Import Java.text.DateFormat;
Import java.util.*;
Import Org.apache.log4j.Logger;
Import Org.apache.log4j.Level;
Import org.apache.log4j.*;
public class Jspcalendar {
Calendar calendar = null;
static Logger Logger = Logger.getlogger (Jspcalendar.class);
Public Jspcalendar () {
Logger.debug ("This is Debug.");
Logger.info ("This was an info.");
Logger.warn ("This is a warning.");
Logger.error ("This was an error.");
Logger.fatal ("This is a fatal error.");
Calendar = Calendar.getinstance ();
Date Trialtime = new Date ();
Calendar.settime (Trialtime);
}
public int getYear () {
Return Calendar.get (calendar.year);
}
......
Other content does not change;
}
9) Modify the system date so that the log generated by the day can be manually triggered;
10) Restart Tomcat;
11) Enter "http://localhost:8086/jsp-examples/dates/date.jsp" in IE;
12) View the log directory "F:\UserWorkSpace\jiangcm\Tomcat 5.0\webapps\jsp-examples\logs", yes! Generated
A date-named HTML log file Tomcat_log_2007-03-05.html! Haha, too happy!
Comments:
1.tomcat itself will automatically generate log files by day, under the directory "F:\UserWorkSpace\jiangcm\Tomcat 5.0\logs";
2.JDK If you cannot compile the servlet, you need to copy the Servlet-api.jar to Jdk\jre\lib under Tomcat\common\lib;
Problem:
1. Change the tomcat_log_2007-03-05.html file name to: jsp-examples_log_2007-03-05.html;
2.xml configuration method to be practiced and learning;
Resources:
1.http://www.3doing.net/forums/printpage.asp?boardid=11&id=184
2.http://wangluwww.spaces.live.com/blog/
3.LOG4J's Chinese documentation (this is based on the latest log4j (jakarta-log4j-1.2.8) Development package comes with the manual translation of the document)
Http://dev.csdn.net/develop/article/29/29441.shtm
4.http://www.javaeye.com/post/126014
In case of trouble, consider placing an application under one of the directories, the entire servlet, dedicated to setting environment variables and reading the log4j configuration
file, the directory of the application is fixed when the servlet is initialized
You can use System.setproperty ("Webapphome", This.getservletcontext () Getrealpath ("")) to set the current
The absolute path to the applied directory is the environment variable inside Java, which is used in the log4j configuration file ${webapphome}/web-
Inf/conf/log/application.log to configure the absolute path to the file.
log4j Configuring the WebApp log System