Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change log4j log levels runtime from a jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ page import="org.apache.log4j.Level" %>
<%@ page import="org.apache.log4j.LogManager" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.Arrays" %>
<% long beginPageLoadTime = System.currentTimeMillis();%>
<html>
<head>
<title>Log4J Administration</title>
<style type="text/css">
<!--
#content {
padding: 0 10px;
text-align: center;
background-color: #eee;
border: 2px solid #666;
border-radius: 8px;
}
#innerContent {
border-radius: 6px;
background: black;
border: 3px solid black;
}
body {
position: relative;
margin: 16px;
padding: 0px;
color: #333;
font-family: Verdana, Arial, Helvetica sans-serif;
}
h1 {
font-size: 1.5em;
font-weight: normal;
margin-top: 20px;
}
h2 {
margin-top: 10px;
font: 0.75em Verdana, Arial, Helvetica sans-serif;
text-align: left;
}
a, a:link, a:visited, a:active {
font-weight: bold;
text-decoration: none;
color: black;
}
#loggersTable {
font-family: Consolas, Lucida Console, Courier New, Monaco5, monospace;
font-size: 14px;
background-color: #000;
border: 0px;
width: 100%;
font-size: 0.70em;
}
td {
white-space: nowrap;
padding: 2px 10px;
}
tr:nth-child(even) {background: #efefef}
tr:nth-child(odd) {background: #FFF}
td.levels {
text-align: center;
white-space: nowrap;
width: 250px;
}
.filterForm {
font-size: 0.75em;
background-color: #000;
color: #fff;
text-align: center;
white-space: nowrap;
margin-bottom: 0;
padding: 5px 0 10px;
}
.filterText {
background-color: #FFFFFF;
border: 1px solid #CCCCCC;
border-radius: 3px;
color: #000000;
text-align: left;
white-space: nowrap;
width: 35%;
}
.filterButton {
background-color: #666666;
border: 1px solid #CCCCCC;
border-radius: 5px;
color: #FFFFFF;
}
.selectedLevel {
color: gray;
}
-->
</style>
</head>
<body onLoad="javascript:document.logFilterForm.logNameFilter.focus();">
<%
String containsFilter = "contains";
String beginsWithFilter = "begins with";
String[] logLevels = {"trace", "debug", "info", "warn", "error", "fatal", "off"};
String targetOperation = (String) request.getParameter("operation");
String targetLogger = (String) request.getParameter("logger");
String targetLogLevel = (String) request.getParameter("newLogLevel");
String logNameFilter = (String) request.getParameter("logNameFilter");
String logNameFilterType = (String) request.getParameter("logNameFilterType");
%>
<div id="content">
<h1>Log4J Administration</h1>
<div id="innerContent">
<form action="log4jAdmin.jsp" name="logFilterForm" class="filterForm">filter:&nbsp;&nbsp;
<input name="logNameFilter" type="text" size="50" value="<%=(logNameFilter == null ? "":logNameFilter)%>"
class="filterText"/>
<input name="logNameFilterType" type="submit" value="<%=beginsWithFilter%>" class="filterButton"/>&nbsp;
<input name="logNameFilterType" type="submit" value="<%=containsFilter%>" class="filterButton"/>&nbsp;
<input name="logNameClear" type="button" value="clear" class="filterButton"
onmousedown='javascript:document.logFilterForm.logNameFilter.value="";'/>
<input name="logNameReset" type="reset" value="reset" class="filterButton"/>
<param name="operation" value="changeLogLevel"/>
</form>
<table id="loggersTable" cellspacing="1">
<%
Enumeration loggers = LogManager.getCurrentLoggers();
HashMap loggersMap = new HashMap(128);
Logger rootLogger = LogManager.getRootLogger();
if (!loggersMap.containsKey(rootLogger.getName())) {
loggersMap.put(rootLogger.getName(), rootLogger);
}
while (loggers.hasMoreElements()) {
Logger logger = (Logger) loggers.nextElement();
if (logNameFilter == null || logNameFilter.trim().length() == 0) {
loggersMap.put(logger.getName(), logger);
} else if (containsFilter.equals(logNameFilterType)) {
if (logger.getName().toUpperCase().indexOf(logNameFilter.toUpperCase()) >= 0) {
loggersMap.put(logger.getName(), logger);
}
} else {
// Either was no filter in IF, contains filter in ELSE IF, or begins with in ELSE
if (logger.getName().startsWith(logNameFilter)) {
loggersMap.put(logger.getName(), logger);
}
}
}
Set loggerKeys = loggersMap.keySet();
String[] keys = new String[loggerKeys.size()];
keys = (String[]) loggerKeys.toArray(keys);
Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < keys.length; i++) {
Logger logger = (Logger) loggersMap.get(keys[i]);
// MUST CHANGE THE LOG LEVEL ON LOGGER BEFORE GENERATING THE LINKS AND THE
// CURRENT LOG LEVEL OR DISABLED LINK WON'T MATCH THE NEWLY CHANGED VALUES
if ("changeLogLevel".equals(targetOperation) && targetLogger.equals(logger.getName())) {
Logger selectedLogger = (Logger) loggersMap.get(targetLogger);
selectedLogger.setLevel(Level.toLevel(targetLogLevel));
}
String loggerName = null;
String loggerEffectiveLevel = null;
String loggerParent = null;
if (logger != null) {
loggerName = logger.getName();
loggerEffectiveLevel = String.valueOf(logger.getEffectiveLevel());
loggerParent = (logger.getParent() == null ? null : logger.getParent().getName());
}
%>
<tr>
<td><%=loggerName%>
</td>
<td class="levels"><%
for (int cnt = 0; cnt < logLevels.length; cnt++) {
String url = "log4jAdmin.jsp?operation=changeLogLevel&logger=" + loggerName + "&newLogLevel=" + logLevels[cnt] + "&logNameFilter=" + (logNameFilter != null ? logNameFilter : "") + "&logNameFilterType=" + (logNameFilterType != null ? logNameFilterType : "");
if (logger.getLevel() == Level.toLevel(logLevels[cnt]) || logger.getEffectiveLevel() == Level.toLevel(logLevels[cnt])) {
%><span class="selectedLevel"><%=logLevels[cnt]%></span><%
} else {
%><a href='<%=url%>'><%=logLevels[cnt]%></a><%
}
%>&nbsp;<%
}
%>
</td>
</tr>
<%
}
%>
</table>
</div>
<h2>
Revision: 1.0.1<br/>
Page Load Time: <%=(System.currentTimeMillis() - beginPageLoadTime)%>ms
</h2>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment