Hi,
I guess more people than I had the problem to use log4j inside a
"non log4j enviroment" e.g. Caucho Resin.
I wrote a small Struts log4j PlugIn which might be useful to those
who do not want to reinvent the wheel ;-)
The Plug-In is also able to reload configuration after a given sleep time,
because restarting the server on every property file change anoyed me.
Use:
----
1) put your log4j.properties in your /WEB-INF/ directory.
2) Add the following to your struts-config.xml
<plug-in className="Log4jPlugIn" >
<!--
sleep time, default to 2 minutes
sleep time of 0 means no background thread
-->
<set-property property="sleepTime" value="1200000" />
<!-- where to find the log4j.properties file -->
<set-property property="logProperties"
value="/WEB-INF/log4j.properties" />
</plug-in>
3) Compile the following and add to your /WEB-INF/classes
- code ---------------------------------------------------------
import java.io.File;
import javax.servlet.ServletException;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ApplicationConfig;
import org.apache.struts.action.ActionServlet;
/**
* Initializes the <a href="http://jakarta.apache.org/log4j">log4j</a>
* system with a given log4j.properties file for non - Log4j enviroments,
* e.g. Caucho Resin.
* <p>
* This class also starts a background thread which checks the modification
* time of the property file periodically (sleepTime) and updates the
* system if necessary.
* <p>
* Default sleep time is 2 minutes, a sleep time of 0 simply means
* NO background thread.
*
* @author <a href="mailto:thomas.nagel@;ptv.de">Thomas Nagel</a>
* @version $Revision: 1.1 $ $Date: 2002/11/12 09:23:27 $
*/
public final class Log4jPlugIn implements PlugIn {
// ------------------------------------------------------- members
/** Logger instance to use with this Plugin. */
private Logger log;
/** Path to find the log properties. */
private String _logConfiguration;
/** Which configuration file to use. */
private File _logProperties;
/** When was last time the logging properties where modified. */
private long _lastChange;
/**
* The background thread which updates the log4j if the configuration
* file has changed.
*/
private ModificationWatcher _modificationWatcher;
/**
* How long should the modification thread sleep until
* it reloads the configuration? Default to 2 minutes
* (120000 milliseconds).
*/
private long _sleepTime = 120000;
// ------------------------------------------------------- constructor
/** Creates a new instance of DatabasePlugIn */
public Log4jPlugIn() {}
// ------------------------------------------------------- methods
/**
* Initializes the Log4jPlugIn by configuring the log4j system
* with the given property file.
*
* @throws ServletException if initialization failes.
*/
public void init(ActionServlet servlet,
ApplicationConfig applicationConfig) throws
ServletException {
try{
_logProperties =
new
File(servlet.getServletContext().getRealPath(getLogProperties()));
configureLog4j();
log = Logger.getLogger(getClass().getName());
if(_sleepTime > 0 ){
_modificationWatcher = new
Log4jPlugIn.ModificationWatcher();
_modificationWatcher.start();
}
}catch(Exception e){
throw new ServletException("Could not initialize log4j
subsystem", e);
}
log.info("Log4j successfully initialized.");
}
/**
* Not used but required by interface.
*/
public void destroy() {
log.info("Shutting down log4j system.");
_modificationWatcher.shutdown();
}
/**
* Configures the log4j system.
*/
private void configureLog4j() throws Exception {
PropertyConfigurator.configure(_logProperties.toURL());
_lastChange = _logProperties.lastModified();
}
// ------------------------------------------------------------
properties
public String getLogProperties() {
return _logConfiguration;
}
public void setLogProperties(String logProperties) {
_logConfiguration = logProperties;
}
public void setSleepTime(String sleepTime) {
_sleepTime = Long.parseLong(sleepTime);
}
// ---------------------------------------------------------- inner
classes
/**
* Background check if the log4j configuration file
* has changed.
*/
private final class ModificationWatcher extends Thread {
protected boolean _done = false;
public void run(){
while(!_done){
if(_logProperties.lastModified() != _lastChange){
try{
configureLog4j();
}catch(Exception e){
log.error("Problems configuring log4j system",
e);
}
log.info("Configuring log4j system");
}
try{
sleep(_sleepTime);
}catch(InterruptedException ex){}
}
}
public void shutdown(){
_done = true;
}
}
}
- code ---------------------------------------------------------
Bye
Thomas
--
To unsubscribe, e-mail: <mailto:struts-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@;jakarta.apache.org>