By programming, log4j configuration takes effect dynamically. It is implemented through JSP.

Source: Internet
Author: User

Log4j is often used for log output during web development. The log level is frequently adjusted during software test run.

If you modify log4j. xml once and restart the web application, it is very troublesome.

The following program can implement dynamic configuration of log4j. xml.

(After modifying log4j. XML, run the JSP program under the supervisor.

This program does not provide the function of visually configuring log4j. xml.

)

 

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<%
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
%>
<HTML>
<Head>
<Title> reconfigure log4j </title>
<Link href = "$ {pagecontext. Request. contextpath}/CSS/styleqxgl.css" rel = "stylesheet" type = "text/CSS">
</Head>
<Body>

<%
Try {
String location = config. getservletcontext (). getinitparameter ("log4jconfiglocation ");
Out. println ("location =" + location + "<br> ");
 
If (! Org. springframework. util. resourceutils. isurl (location )){
// Resolve system property placeholders before resolving real path.
Location = org. springframework. util. systempropertyutils. resolveplaceholders (location );
Location = org. springframework. Web. util. webutils. getrealpath (config. getservletcontext (), location );
}
 
Java.net. url = org. springframework. util. resourceutils. geturl (location );
 
Org. Apache. log4j. xml. domconfigurator. Configure (URL );
 
Out. println ("log4j reconfiguration successful! <Br> ");
Out. println ("time:" + COM. Work. util. dateutil. getcurrentdatetime ());
} Catch (exception e ){
Out. println ("log4j configuration failed! "+ E. getmessage ());
 
}
%>

<HR>

</Body>
</Html>

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.