Log4j learning (below)

xiaoxiao2021-03-06  46

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

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:

Hello loginit < / servlet-name> com.log4j.test.test.initservlet log4j_file_url /web-inf/classess/log4j.xml Reflesh_time 5 1 5) OK, after 4 steps above, log4j is basically configured. When the web is published, the background will print as follows:

[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!

转载请注明原文地址:https://www.9cbs.com/read-115880.html

New Post(0)