Issue Details (XML | Word | Printable)

Key: DIRSERVER-611
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Enrique Rodriguez
Reporter: John Conlon
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Directory ApacheDS

An OSGi Activator can allow simple way for reconfiguration of log4j.properties without restarting the JVM.

Created: 28/Apr/06 02:58 AM   Updated: 13/Dec/06 09:12 PM
Return to search
Component/s: None
Affects Version/s: None
Fix Version/s: 1.5.0

Time Tracking:
Not Specified

Resolution Date: 14/May/06 06:55 AM


 Description  « Hide
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.");
     }
  }
}

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order