效果图:
<%@ 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>