An OSGi Activator can allow simple way for reconfiguration of log4j.properties
without restarting the JVM.
----------------------------------------------------------------------------------------------------------
Key: DIRSERVER-611
URL: http://issues.apache.org/jira/browse/DIRSERVER-611
Project: Directory ApacheDS
Type: Improvement
Reporter: John Conlon
Runtime changes to the log4j.properties file require reconfiguration of the
logging infrastructure.
Adding an the attached Activator to an OSGi bundle that exports LOG4J and SLF4J
packages a bundle can offer LOG4J and SLF4J packages to other bundles and offer
a solution for reconfiguration of the logging infrastructure without restarting
the JVM.
--------------------------------------------------
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator implements BundleActivator
{
private static final String LOG_PROPERTIES_LOCATION =
"log4j.configuration";
private Logger log = null;
public void start(BundleContext bundleContext) throws Exception
{
try
{
resetLog4j(bundleContext);
}
catch (Exception e)
{
//e.printStackTrace();
}
log = LoggerFactory.getLogger(Activator.class);
log.debug("Reset log configuration.");
}
public void stop(BundleContext arg0) throws Exception {}
/**
* @return url of the log4j.properties configuration file
*
* @throws MalformedURLException
*
*/
private URL getLoggingProperty(BundleContext bundleContext)
throws MalformedURLException
{
final String logPropertiesLocation = bundleContext
.getProperty(LOG_PROPERTIES_LOCATION);
return new URL(logPropertiesLocation);
}
/**
* Reset the log4j configuration.
* @param bundleContext
* @throws MalformedURLException
* @throws FileNotFoundException
*/
private void resetLog4j(BundleContext bundleContext)
throws MalformedURLException,
FileNotFoundException
{
LogManager.resetConfiguration();
URL log4jprops = getLoggingProperty(bundleContext);
if (log4jprops != null)
{
PropertyConfigurator.configure(log4jprops);
}
else
{
throw new FileNotFoundException(bundleContext
.getProperty(LOG_PROPERTIES_LOCATION)
+ " could not be found. "
+ "Please specify add the file and restart the "
+ bundleContext.getBundle().getLocation() + " bundle.");
}
}
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira