Last active
August 29, 2015 14:01
-
-
Save iambigd/f10ad403eaa8a06c2c64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package some.package; | |
import java.io.File; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
import org.apache.log4j.PropertyConfigurator; | |
import org.apache.log4j.LogManager; | |
import org.apache.log4j.xml.DOMConfigurator; | |
/** | |
* ServletContextListener to initialize Logging for the application | |
*/ | |
public class Log4jConfigListener implements ServletContextListener | |
{ | |
private static long DEFAULT_INTERVAL = 300000; | |
public void contextDestroyed(ServletContextEvent event) | |
{ | |
//Turn off logging when the Servlet Context is destroyed | |
LogManager.shutdown(); | |
} | |
public void contextInitialized(ServletContextEvent event) | |
{ | |
//Read configuration properties from web.xml | |
String file = event.getServletContext().getInitParameter("LOG4J_CONFIG_LOCATION"); | |
String interval = event.getServletContext().getInitParameter("LOG4J_REFRESH_INTERVAL"); | |
long delay = DEFAULT_INTERVAL; //Default delay is 5 minutes | |
//Check for interval parameter | |
if (interval != null && !"".equals(interval)) | |
{ | |
try | |
{ | |
delay = Long.parseLong(interval); | |
} | |
catch(NumberFormatException e) | |
{ | |
//Can't really log the error since we haven't initialized Log4J | |
//Will use the default value | |
delay = DEFAULT_INTERVAL; | |
} | |
} | |
//Check for file parameter | |
if(file != null && !"".equals(file)) | |
{ | |
if(!(new File(file).exists())) | |
{ | |
throw new IllegalArgumentException("Invalid 'LOG4J_CONFIG_LOCATION' parameter value '" | |
+ file + "'."); | |
} | |
if(file.toLowerCase().endsWith(".xml")) | |
{ | |
DOMConfigurator.configureAndWatch(file, delay); | |
} | |
else | |
{ | |
PropertyConfigurator.configureAndWatch(file, delay); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Log4j Initialization Parameters --> | |
<context-param> | |
<!-- absolute path to log4j config file (*.properties or *.xml) --> | |
<param-name>LOG4J_CONFIG_LOCATION</param-name> | |
<param-value> | |
ABSOLUTE_PATH_TO_YOUR_LOG4J_CONFIGURATION_FILE | |
</param-value> | |
</context-param> | |
<context-param> | |
<!-- number of milliseconds to wait before checking LOG4J_CONFIG_LOCATION for updates --> | |
<param-name>LOG4J_REFRESH_INTERVAL</param-name> | |
<param-value>(e.g. 300000)</param-value> | |
</context-param> | |
<listener> | |
<listener-class> | |
some.package.Log4jConfigListener | |
</listener-class> | |
</listener> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment