#20 log4j动态控制页面

This commit is contained in:
Calvin
2012-04-13 23:56:24 +08:00
parent bd7e5a2c40
commit 990dbb0201
5 changed files with 164 additions and 6 deletions
@@ -0,0 +1,108 @@
package org.springside.examples.showcase.log;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springside.modules.log.Log4jManager;
import com.google.common.collect.Lists;
@Controller
@RequestMapping("/log")
public class LogController {
private List levels = Lists.newArrayList("", "ERROR", "WARN", "INFO");
@Autowired
private Log4jManager log4jManager;
@RequestMapping(value = "/console")
public String console(Model model) {
createFormObject(model);
return "log/console";
}
@RequestMapping(value = "/defaultsetting")
public String updateDefaultLogger(FormObject command, Model model) {
log4jManager.setRootLoggerLevel(command.getRootLoggerLevel());
log4jManager.setProjectLoggerLevel(command.getProjectLoggerLevel());
createFormObject(model);
return "log/console";
}
@RequestMapping(value = "/loggersetting")
public String updateLogger(FormObject command, Model model) {
String loggerName = command.loggerName;
String loggerLevel = null;
if (StringUtils.isNotBlank(command.loggerLevel)) {
loggerLevel = command.loggerLevel;
log4jManager.setLoggerLevel(command.loggerName, command.loggerLevel);
} else {
loggerLevel = log4jManager.getLoggerLevel(command.loggerName);
}
FormObject newCommand = createFormObject(model);
newCommand.loggerName = loggerName;
newCommand.loggerLevel = loggerLevel;
return "log/console";
}
private FormObject createFormObject(Model model) {
FormObject command = new FormObject();
command.rootLoggerLevel = log4jManager.getRootLoggerLevel();
command.projectLoggerLevel = log4jManager.getProjectLoggerLevel();
model.addAttribute("command", command);
model.addAttribute("levels", levels);
return command;
}
public static class FormObject {
private String rootLoggerLevel;
private String projectLoggerLevel;
private String loggerName;
private String loggerLevel;
public String getRootLoggerLevel() {
return rootLoggerLevel;
}
public String getProjectLoggerLevel() {
return projectLoggerLevel;
}
public String getLoggerName() {
return loggerName;
}
public String getLoggerLevel() {
return loggerLevel;
}
public void setRootLoggerLevel(String rootLoggerLevel) {
this.rootLoggerLevel = rootLoggerLevel;
}
public void setProjectLoggerLevel(String projectLoggerLevel) {
this.projectLoggerLevel = projectLoggerLevel;
}
public void setLoggerName(String loggerName) {
this.loggerName = loggerName;
}
public void setLoggerLevel(String loggerLevel) {
this.loggerLevel = loggerLevel;
}
}
}
@@ -11,9 +11,10 @@
<a href="${ctx}/story/security/">安全演示</a>
<a href="${ctx}/story/web/">Web演示</a>
<a href="${ctx}/story/webservice/">WebService演示</a>
<a href="${ctx}/log/console">日志控制演示</a>
<a href="${ctx}/story/jmx/">JMX演示</a>
<a href="${ctx}/story/executablewar/">可运行war包演示</a>
<a href="${ctx}/story/utilizes/">Utilizes演示</a>
<a href="${ctx}/story/utilizes/">工具类演示</a>
<shiro:guest>
<a href="${ctx}/login">登录</a>
</shiro:guest>
@@ -0,0 +1,41 @@
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
<title>邮件演示</title>
</head>
<body>
<h2>Log4j控制頁面</h2>
<form:form id="defaultLoggerForm" action="${ctx}/log/defaultsetting" method="post">
<div>
<label for="rootLoggerLevel">Root Logger Level:</label> <form:select path="rootLoggerLevel" items="${levels}"/>
</div>
<div>
<label for="rootLoggerLevel">Project Logger Level:</label> <form:select path="projectLoggerLevel" items="${levels}"/>
</div>
<div>
<input type="submit" value="save"/>
</div>
</form:form>
<form:form id="anyLoggerForm" action="${ctx}/log/loggersetting" method="post">
<div>
<label for="loggerName">Logger Name:</label><input name="loggerName" value="${command.loggerName}"/> <form:select path="loggerLevel" items="${levels}"/>
</div>
<div>
<c:if test="${command.loggerName ==null}">
<input type="submit" value="query"/>
</c:if>
<c:if test="${command.loggerName !=null}">
<input type="submit" value="save"/>
</c:if>
</div>
</form:form>
</body>
</html>
@@ -9,13 +9,15 @@
<h3>技术说明:</h3>
<ul>
<li>可运行war使用嵌入式的Jetty,在简便的容器外运行web应用</li>
<li>可运行war使用嵌入式的Jetty,简便地在容器外运行web应用.</li>
</ul>
<h3>用户故事:</h3>
<ul>
<li>在showcase中打包可运行war包的命令:mvn clean package -Pstandalone</li>
<li>运行war包的命令: java -jar showcase-4.x.x.standalone.war</li>
<li>在showcase中打包可运行war包的命令:<br/>
mvn clean package -Pstandalone</li>
<li>运行war包的命令: <br/>java -jar showcase-4.x.x.standalone.war</li>
</ul>
</body>
</html>
@@ -51,7 +51,11 @@ public class Log4jManager {
*/
@ManagedAttribute(description = "Project default logging level of the logger")
public String getProjectLoggerLevel() {
return getLoggerLevel(projectLoggerName);
if (projectLoggerName != null) {
return getLoggerLevel(projectLoggerName);
}
return null;
}
/**
@@ -60,7 +64,9 @@ public class Log4jManager {
*/
@ManagedAttribute(description = "Project default logging level of the logger")
public void setProjectLoggerLevel(String newLevel) {
setLoggerLevel(projectLoggerName, newLevel);
if (projectLoggerName != null) {
setLoggerLevel(projectLoggerName, newLevel);
}
}
/**