動態設定log4j的記錄層級

來源:互聯網
上載者:User

 

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.*,org.apache.log4j.*,org.dom4j.*,org.dom4j.io.*"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<html>
<head>
<title>重新設定log4j</title>
<link href="${pageContext.request.contextPath}/css/styleqxgl.css" rel="stylesheet" type="text/css">
</head>
<body>
<font class="titleHead">
動態修改記錄層級,可以動態起作用!
</font>
<%
String renovateParam = request.getParameter("renovateParam");

%>
<form name="myform" method="post" action="<s:url value="/log4jConfigNew.jsp" />">
<input type="hidden" name="renovateParam" value="true">
<table border="1" bordercolor="#7595ea" cellpadding="2" style="border-collapse: collapse " >
<tr  bgcolor="eaf1ff"><td>包</td><td>記錄層級</td></tr>
<%
 try{
  String location = config.getServletContext().getInitParameter("log4jConfigLocation");
  out.println("location="+location+"<br><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 url = org.springframework.util.ResourceUtils.getURL(location);

  SAXReader reader = new SAXReader();

  Document doc = reader.read(url);

  Element root = doc.getRootElement();

  Iterator<Element> itr = root.elements("logger").iterator();

  while (itr.hasNext()) {
   Element category = itr.next();

   String name = category.attributeValue("name");
   Logger logger = Logger.getLogger(name);
   String level = logger.getLevel().toString();
%>

<tr><td>
 <input type="hidden" name="logger" value="<%=name %>"><%=name %>
 </td>
 <td>
 <select name="level">
 <%
 if (level.equals("OFF")){
 %>
  <option name="OFF" selected >OFF</option> 
 <%
 }else{
 %>
  <option name="OFF" >OFF</option> 
 <%  
 }
 %>
 <%
 if (level.equals("FATAL")){
 %>
  <option name="FATAL" selected >FATAL</option> 
 <%
 }else{
 %>
  <option name="FATAL">FATAL</option>
 <%  
 }
 %>
 <%
 if (level.equals("ERROR")){
 %>
  <option name="ERROR" selected >ERROR</option> 
 <%
 }else{
 %>
  <option name="ERROR">ERROR</option>
 <%  
 }
 %>
 <%
 if (level.equals("WARN")){
 %>
  <option name="WARN" selected >WARN</option> 
 <%
 }else{
 %>
  <option name="WARN">WARN</option>
 <%  
 }
 %>
 <%
 if (level.equals("INFO")){
 %>
  <option name="INFO" selected >INFO</option> 
 <%
 }else{
 %>
  <option name="INFO">INFO</option>
 <%  
 }
 %>
 <%
 if (level.equals("DEBUG")){
 %>
  <option name="DEBUG" selected >DEBUG</option> 
 <%
 }else{
 %>
  <option name="DEBUG">DEBUG</option>
 <%  
 }
 %> 
 <%
 if (level.equals("ALL")){
 %>
  <option name="ALL" selected >ALL</option> 
 <%
 }else{
 %>
  <option name="ALL">ALL</option>
 <%  
 }
 %>  
 </select>
 </td>
</tr>

<%
  }
  
 }catch(Exception e){
  out.println("擷取log4j配置失敗!"+e.getMessage());
  
 }
%>
</table>
<input type="submit" class="button11" value="重新設定日誌" >
</form>
<%
if("true".equals(renovateParam)){
 try{
  String[] loggers = request.getParameterValues("logger");
  String[] levels  = request.getParameterValues("level");
  int LEN = loggers.length;
  for(int i=0;i<LEN;i++){
   Logger logger = Logger.getLogger(loggers[i]);
   logger.setLevel(Level.toLevel(levels[i]));
  }
  out.println("log4j重新設定成功!<br>");
  out.println("時間:"+com.work.util.DateUtil.getCurrentDateTime());
 }catch(Exception e){
  out.println("log4j配置失敗!"+e.getMessage());
  
 }
}
%>

<hr>

</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.