Skip to content

Instantly share code, notes, and snippets.

@fbaligand
Created March 16, 2013 11:14
Show Gist options
  • Save fbaligand/5175976 to your computer and use it in GitHub Desktop.
Save fbaligand/5175976 to your computer and use it in GitHub Desktop.
Page JSP permettant de modifier à chaud, sa configuration LOG4J
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="org.springframework.util.FileCopyUtils"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.Properties"%>
<%@page import="java.io.StringReader"%>
<%@page import="org.apache.log4j.PropertyConfigurator"%>
<%@page import="org.apache.log4j.LogManager"%>
<%!
/**
* variable statique contenant la configuration LOG4J actuellement chargée en mémoire
*/
private static String log4jConfiguration = null;
%>
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
// cas du premier appel de la page ou du reset : on récupère la configuration log4j par défaut
if (request.getParameter("log4jConfiguration") == null) {
if (log4jConfiguration == null || request.getParameter("resetConfiguration") != null) {
// initialisation du cache de la configuration LOG4J
InputStream log4jConfigurationIS = this.getClass().getResourceAsStream("/log4j.properties");
if (log4jConfigurationIS != null) {
log4jConfiguration = new String(FileCopyUtils.copyToByteArray(log4jConfigurationIS));
log4jConfigurationIS.close();
}
}
}
// cas du post de la nouvelle configuration LOG4J : on récupère la configuration log4j envoyée
else {
// récupération de la nouvelle configuration log4j
log4jConfiguration = request.getParameter("log4jConfiguration");
}
// cas où le rechargement de la configuration est demandé (configuration postée ou reset)
String message = "";
if (request.getParameter("log4jConfiguration") != null || request.getParameter("resetConfiguration") != null) {
// chargement de la nouvelle configuration log4j en tant qu'objet Properties
Properties log4jProps = new Properties();
StringReader log4jConfigurationReader = new StringReader(log4jConfiguration);
log4jProps.load(log4jConfigurationReader);
// rechargement de la nouvelle configuration dans log4j
LogManager.resetConfiguration();
PropertyConfigurator.configure(log4jProps);
// affichage du rechargement réussi
message = "<font color='green'><b>log4j configuration reloaded !</b></font><br/><br/>";
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>log4j reload</title>
</head>
<body>
<div align="center">
<form action="log4j-reload.jsp" method="post">
<%=message %>
<input name="submit" type="submit" value="reload log4j"/>&nbsp;&nbsp;
<input name="resetConfiguration" type="button" value="reset log4j configuration" onclick="window.location='log4j-reload.jsp?resetConfiguration'"/><br/>
<textarea name="log4jConfiguration" rows="40" cols="120"><%=log4jConfiguration %></textarea><br/>
<input name="submit" type="submit" value="reload log4j"/>&nbsp;&nbsp;
<input name="resetConfiguration" type="button" value="reset log4j configuration" onclick="window.location='log4j-reload.jsp?resetConfiguration'"/><br/>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment