1 16 package info.magnolia.logging; 17 18 import java.io.IOException ; 19 import java.io.PrintWriter ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.Comparator ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.log4j.Level; 35 import org.apache.log4j.LogManager; 36 import org.apache.log4j.Logger; 37 38 39 78 public class Log4jConfigurationServlet extends HttpServlet { 79 80 83 private static final long serialVersionUID = 64182; 84 85 88 private static final String CONTENT_TYPE = "text/html"; 90 93 private static final String CONFIG_FRAGMENT = "fragment"; 95 98 private static final String ROOT = "Root"; 100 103 private static final String PARAM_CLASS = "class"; 105 108 private static final String PARAM_LEVEL = "level"; 110 113 private static final String PARAM_SORTBYLEVEL = "sortbylevel"; 115 118 private static final String [] LEVELS = new String []{ 119 Level.OFF.toString(), 120 Level.FATAL.toString(), 121 Level.ERROR.toString(), 122 Level.WARN.toString(), 123 Level.INFO.toString(), 124 Level.DEBUG.toString(), 125 Level.ALL.toString()}; 126 127 130 private boolean isFragment; 131 132 139 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 140 141 String sortByLevelParam = request.getParameter(PARAM_SORTBYLEVEL); 142 boolean sortByLevel = ("true".equalsIgnoreCase(sortByLevelParam) || "yes".equalsIgnoreCase(sortByLevelParam)); 144 List loggers = getSortedLoggers(sortByLevel); 145 int loggerNum = 0; 146 147 PrintWriter out = response.getWriter(); 148 if (!isFragment) { 149 response.setContentType(CONTENT_TYPE); 150 151 printHeader(out); 153 } 154 155 out.println("<a HREF=\"" + request.getRequestURI() + "\">Refresh</a>"); 158 out.println("<table class=\"log4jtable\">"); out.println("<thead><tr>"); 161 out.println("<th title=\"Logger name\">"); out.println("<a HREF=\"?" + PARAM_SORTBYLEVEL + "=false\">Class</a>"); out.println("</th>"); 165 out.println("<th title=\"Is logging level inherited from parent?\" style=\"text-align:right\" >*</th>"); out.println("<th title=\"Logger level\">"); out.println("<a HREF=\"?" + PARAM_SORTBYLEVEL + "=true\">Level</a>"); out.println("</th>"); 170 out.println("</tr></thead>"); out.println("<tbody>"); 173 displayLogger(out, Logger.getRootLogger(), loggerNum++); 175 176 Iterator iterator = loggers.iterator(); 178 179 while (iterator.hasNext()) { 180 displayLogger(out, (Logger) iterator.next(), loggerNum++); 181 } 182 183 out.println("</tbody>"); out.println("</table>"); out.println("<a HREF=\"\">Refresh</a>"); 187 if (!isFragment) { 188 out.println("</body></html>"); out.flush(); 190 out.close(); 191 } 192 } 193 194 201 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 202 String className = request.getParameter(PARAM_CLASS); 203 String level = request.getParameter(PARAM_LEVEL); 204 205 if (className != null) { 206 setClass(className, level); 207 } 208 209 doGet(request, response); 210 } 211 212 219 private void displayLogger(PrintWriter out, Logger logger, int row) { 220 String color = null; 221 String loggerName = (StringUtils.isEmpty(logger.getName()) ? ROOT : logger.getName()); 222 223 color = ((row % 2) == 1) ? "even" : "odd"; 225 out.println("<tr class=\"" + color + "\">"); 227 out.println("<td>"); out.println(loggerName); 230 out.println("</td>"); 232 out.println("<td style=\"text-align:right\">"); if ((logger.getLevel() == null)) { 235 out.println("*"); } 237 out.println("</td>"); 239 out.println("<td>"); out.println("<form action=\"\" method=\"post\">"); printLevelSelector(out, logger.getEffectiveLevel().toString()); 243 out.println("<input type=\"hidden\" name=\"" + PARAM_CLASS + "\" value=\"" + loggerName + "\">"); out.print("<input type=\"submit\" name=\"Set\" value=\"Set \">"); out.println("</form>"); out.println("</td>"); 248 out.println("</tr>"); } 250 251 257 private synchronized String setClass(String className, String level) { 258 Logger logger = null; 259 260 try { 261 logger = (ROOT.equalsIgnoreCase(className) ? Logger.getRootLogger() : Logger.getLogger(className)); 262 logger.setLevel(Level.toLevel(level)); 263 } 264 catch (Throwable e) { 265 System .out.println("ERROR Setting LOG4J Logger:" + e); } 268 269 return "Message Set For " + (StringUtils.isEmpty(logger.getName()) ? ROOT : logger.getName()); } 271 272 277 private List getSortedLoggers(boolean sortByLevel) { 278 Enumeration enm = LogManager.getCurrentLoggers(); 279 Comparator comp = new LoggerComparator(sortByLevel); 280 List list = new ArrayList (); 281 282 while (enm.hasMoreElements()) { 284 list.add(enm.nextElement()); 285 } 286 287 Collections.sort(list, comp); 289 290 return list; 291 } 292 293 298 private void printHeader(PrintWriter out) { 299 out.println("<html><head><title>Log4J Control Console</title>"); 301 out.println("<style type=\"text/css\">"); out.println("body{ background-color:#fff; }"); out.println("body, td, th, select, input{ font-family:Verdana, Geneva, Arial, sans-serif; font-size: 8pt;}"); out.println("select, input{ border: 1px solid #ccc;}"); out.println("table.log4jtable, table.log4jtable td { border-collapse:collapse; border: 1px solid #ccc; "); out.println("white-space: nowrap; text-align: left; }"); out.println("form { margin:0; padding:0; }"); out.println("table.log4jtable thead tr th{ background-color: #5991A6; padding: 2px; }"); out.println("table.log4jtable tr.even { background-color: #eee; }"); out.println("table.log4jtable tr.odd { background-color: #fff; }"); out.println("</style>"); 313 out.println("</head>"); out.println("<body>"); out.println("<h3>Log4J Control Console</h3>"); } 317 318 323 private void printLevelSelector(PrintWriter out, String currentLevel) { 324 out.println("<select id=\"" + PARAM_LEVEL + "\" name=\"" + PARAM_LEVEL + "\">"); 326 for (int j = 0; j < LEVELS.length; j++) { 327 out.print("<option"); if (LEVELS[j].equals(currentLevel)) { 329 out.print(" selected=\"selected\""); } 331 out.print(">"); out.print(LEVELS[j]); 333 out.println("</option>"); } 335 out.println("</select>"); } 337 338 341 private static class LoggerComparator implements Comparator { 342 343 346 private boolean sortByLevel; 347 348 352 public LoggerComparator(boolean sortByLevel) { 353 this.sortByLevel = sortByLevel; 354 } 355 356 362 public int compare(Object object1, Object object2) { 363 Logger logger1 = (Logger) object1; 364 Logger logger2 = (Logger) object2; 365 366 if (!sortByLevel) { 367 return logger1.getName().compareTo(logger2.getName()); 368 } 369 return logger1.getEffectiveLevel().toInt() - logger2.getEffectiveLevel().toInt(); 370 } 371 372 377 public boolean equals(Object object) { 378 if (!(object instanceof LoggerComparator)) { 379 return false; 380 } 381 return this.sortByLevel == ((LoggerComparator) object).sortByLevel; 382 } 383 384 387 public int hashCode() { 388 return super.hashCode(); 389 } 390 } 391 392 395 public void init(ServletConfig config) throws ServletException { 396 String fragmentParam = config.getInitParameter(CONFIG_FRAGMENT); 397 isFragment = ("true".equalsIgnoreCase(fragmentParam) || "yes".equalsIgnoreCase(fragmentParam)); super.init(config); 399 } 400 401 } | Popular Tags |