web开发过程中,我们经常使用log4j来实现日志输出。日志级别在软件试运行的过程中经常调整。
如果修改一次log4j.xml就重新启动一下web应用,实在是非常麻烦。
下面的程序可以实现log4j.xml动态配置。
(修改了log4j.xml之后,然后运行一下下面的jsp程序就可以了。
本程序不提供可视化配置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>重新配置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><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);
org.apache.log4j.xml.DOMConfigurator.configure(url);
out.println("log4j重新配置成功!<br>");
out.println("时间:"+com.work.util.DateUtil.getCurrentDateTime());
}catch(Exception e){
out.println("log4j配置失败!"+e.getMessage());
}
%>
<hr>
</body>
</html>