动态控制Log4J日志级别,只需要一个JSP页面即可。log4jCtl.jsp如下
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<%@
page
import
=
"org.apache.log4j.*"
%>
<%@
taglib
prefix
=
"c"
uri
=
"http://java.sun.com/jsp/jstl/core"
%>
<
c:set
var
=
"basePath"
value
=
"
${pageContext.request.contextPath}
"
/>
DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>
Log4J级别控制
title
>
<
style
type
=
"text/css"
>
/* 超级链接样式 */
a
{
text-decoration
:
none
;
}
a:link
{
text-decoration
:
none
;}
a:visited
{
text-decoration
:
none
;}
a:hover
{
color
:
#F60
;
text-decoration
:
underline
;}
a:active
{
text-decoration
:
underline
;}
body
{
font
:
normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif
;
/*color: #4f6b72;
background: #E6EAE9; */
}
#log-table
{
padding
:
0
;
margin
:
5px 0
;
}
tr
{
background
:
#F5FAFA
;
}
tr:nth-child(2n)
{
background
:
#fff
;
}
tr
{
background-color
:
expression(
(
this.sectionRowIndex
%
2
==
0)
?
"#F5FAFA"
:
"#fff" )
;
}
td
{
border-right
:
1px solid #C1DAD7
;
border-bottom
:
1px solid #C1DAD7
;
font-size
:
12px
;
padding
:
6px 6px 6px 12px
;
/*color: #4f6b72; */
}
.left-border
{
border-left
:
1px solid #C1DAD7
;
}
th
{
font
:
bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif
;
/*color: #4f6b72; */
border-right
:
1px solid #C1DAD7
;
border-bottom
:
1px solid #C1DAD7
;
border-top
:
1px solid #C1DAD7
;
letter-spacing
:
2px
;
/*text-transform: uppercase; */
text-align
:
center
;
padding
:
6px 6px 6px 12px
;
background
:
#CAE8EA
;
}
/*---------for IE 5.x bug*/
html> body td
{
font-size
:
12px
;
}
style
>
head
>
<
body
>
<
h1
>
Log4J日志级别控制中心
h1
>
<
div
>
显示
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=true"
>
所有 Logger
a
>
| 显示
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=false"
>
当前运行的Logger
a
>
div
>
<%
String logName = request.getParameter(
"log"
);
if
(
null
!= logName) {
Logger log = (
""
.equals(logName) ? Logger.getRootLogger() : Logger.getLogger(logName));
log.setLevel(Level.toLevel(request.getParameter(
"level"
), Level.DEBUG));
}
%>
<
c:set
var
=
"rootLogger"
value
=
"
<%=
Logger.getRootLogger()
%>
"
/>
<
form
>
<
table
id
=
"log-table"
cellspacing
=
"0"
>
<
tr
>
<
th
class
=
"left-border"
>
Level
th
>
<
th
>
Logger
th
>
<
th
>
Set New Level
th
>
tr
>
<
tr
>
<
td
class
=
"left-border"
>
${rootLogger.level}
td
>
<
td
>
${rootLogger.name}
td
>
<
td
>
<
c:forTokens
var
=
"level"
delims
=
","
items
=
"ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF"
>
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=
${param.showAll}
&log=&level=
${level}
"
>
${level}
a
>
|
c:forTokens
>
td
>
tr
>
<
c:forEach
var
=
"logger"
items
=
"
${rootLogger.loggerRepository.currentLoggers}
"
>
<
c:if
test
=
"
${!
empty
logger.level.syslogEquivalent || param.showAll}
"
>
<
tr
>
<
c:if
test
=
"
${!
empty
logger.level.syslogEquivalent}
"
>
<
td
class
=
"left-border"
>
${logger.level}
td
>
<
td
>
${logger.name}
td
>
<
td
>
<
c:forTokens
var
=
"level"
delims
=
","
items
=
"ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF"
>
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=
${param.showAll}
&log=
${logger.name}
&level=
${level}
"
>
${level}
a
>
|
c:forTokens
>
td
>
c:if
>
<
c:if
test
=
"
${
empty
logger.level.syslogEquivalent}
"
>
<
td
class
=
"left-border"
>
----
td
>
<
td
>
${logger.name}
td
>
<
td
>
此Logger还未实例化运行
td
>
c:if
>
tr
>
c:if
>
c:forEach
>
table
>
form
>
<
div
>
显示
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=true"
>
所有 Logger
a
>
| 显示
<
a
href
=
"
${basePath}
/log4j.jsp?showAll=false"
>
当前运行的Logger
a
>
div
>
body
>
html
>