Log4j learning (below)
In the last section, how to use the Commons-Logging and Log4j packages in a normal Java application, this section will summarize how two packages are used in the web application
1) Copy commongs-logging.jar and log4j-1.2.jar package into the lib directory of the web application, as shown below:
Hello
--Module Directory
--Web-INF
--Classes
--com
--LIB
--commons-logging.jar
--Log4j-1.2.8.jar
--testlog4j.jsp
2) Configure the log4j.xml file, place it in the class directory (of course, it can be placed in any directory), log4j.xml content is as follows:
Log4j.xml
XML Version = "1.0" encoding = "UTF-8">
layout>
appender>
logger>
root>
log4j: Configuration>
Note: Log4j.xml configuration parameters Meaning, see log4j learning (on)
3) Write a servlet class that reads the log4j.xml file, called INITSERVLET:
Package com.log4j.test.test;
Import javax.servlet.servletException;
Import javax.servlet.http.httpservlet;
Import org.apache.log4j.xml.domconfigurator;
Import org.apache.log4j.logmanager;
Public class initservlet
Extends httpservlet {
Public void init () throws servletexception {
String logfileurl = "classes / config / log4j.xml"; int RefreshTime = 60; // by secondes
String prefix = GetServletContext (). GetRealPath ("/");
IF (GetInitParameter ("log4j_file_url)! = null &&
GetinitParameter ("log4j_file_url). Length ()> 0) {
LogfileURL = GetInitParameter ("log4j_file_url);
}
IF ("reflesh_time")! = null&&
GetinitParameter ("reflesh_time"). Length ()> 0) {
Try
{
Refreshtime = INTEGER.PARSEINT (GetInitParameter ("Reflesh_time");
} catch (NumberFormatexception EX)
{
EX.PrintStackTrace ();
}
}
// Log initialization
Print ("Log4j Config Url: Prefix Logfileurl " Reflesh Time IS " RefreshTime);
Try {
Domconfigurator.configureAndWatch (Prefix logfileurl, refreshtime * 1000);
}
Catch (Exception EX) {
EX.PrintStackTrace ();
Print ("Log4j Config Failure!");
}
Print ("Log4j Config Success!");
}
Public Boolean Init (httpservlet hs) {
Return True;
}
Public void destory () {
LogManager.shutdown ();
Print ("log4j shutdown");
}
Private Void Print (String MSG)
{
System.out.println ("[in com.log4j.test.test.initservlet.java]" msg);
}
}
4) Declaring the servlet in Web.xml, so that the Web is published automatically to automatically call the above servlet to configure log4j's environment variables.
Web.xml:
XML Version = "1.0" Encoding = "UTF-8"?>
[in com.log4j.test.test.initservlet.java] log4j config URL: D: /program/log/hello/web-inf/classes/log4j.xml reflesh time is 5 [in com.log4j.test.test. INITSERVLET.JAVA] Log4j Config SUCCESS!
6) Write a Test JSP page:
Testlog4j.jsp:
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Page Import = "org.apache.commons.logging. *"%>
<% @ Page Import = "com.log4j.test.testlog4j2"%>
<%!
Private log log = logfactory.getlog ("testlog4j.jsp");
%>
<%
Log.Error ("JSP log4j error");
Log.Warn ("JSP log4j warn");
LOG.INFO ("JSP log4j info");
Log.debug ("JSP log4j debug";
%>
This is log4j test!
<% = ("Helllo")%> After running the background print information:
TestLog4j.jsp jsp_servlet .__ testlog4j._jspservice (__ testlog4j.java: 139) 2004-09-
20 17: 09: 23,014 [ExecuteThread: '14' for queue: 'WebLogic.kernel.default'] Error
- JSP log4j error
Testlog4j.jsp jsp_servlet .__ testlog4j._jspservice (__ testlog4j.java:140) 2004-09-
20 17: 09: 23,014 [ExecuteThread: '14' for Queue: 'WebLogic.kernel.default'] WARN
- JSP log4j Warn
Testlog4j.jsp jsp_servlet .__ testlog4j._jspservice (__ testlog4j.java:141) 2004-09-
20 17: 09: 23,029 [ExecuteThread: '14' for queue: 'WebLogic.kernel.default'] info
- JSP log4j info
Oh, enjoy!