Hi all,

I am writing a line command tool and I'd like to add an option like
--log-file path that allow to specify where the command execution log is
stored. I want to keep all the other log parameters in a .properties
file, which is loaded from the URL passed via log4j.configuration, as
per Default Initialization Procedure.

This means that I'd like to be able to override one of the properties
loaded from the config file (i.e.: something like
log4j.appender.FileApp.File) with the value passed via the command line
option.

I wonder if something like that is already available in Log4J, or if I
have to implement it on my own. If that is the case, I've written the
attached class, but I am not sure it is the best way to do it.

Thanks in advance for any help.

Marco.




import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggerRepository;

/**
 * An extension of [EMAIL PROTECTED] PropertyConfigurator} that allows to programmatically override the properties that are 
 * loaded from a file or a URL. 
 * 
 *
 */
public class OverridablePropertyConfigurator extends PropertyConfigurator
{
	private static Properties properties = new Properties ();
	
	public static void setProperty ( String key, String value ) 
	{
		properties.setProperty ( key, value );
	}
	
	public static void setProperties ( Properties properties ) {
		properties.putAll ( properties );
	}
	

	@Override
	/** Reads the config properties from the file and overrides them with the ones overridden in this configurator */
	public void doConfigure ( String configFileName, LoggerRepository hierarchy ) 
	{
		// Code copied from the original log4j configurator
    Properties props = new Properties();
    try {
      FileInputStream istream = new FileInputStream(configFileName);
      props.load(istream);
      istream.close();
    }
    catch (IOException e) {
      LogLog.error("Could not read configuration file ["+configFileName+"].", e);
      LogLog.error("Ignoring configuration file [" + configFileName+"].");
      return;
    }
    // If we reach here, then the config file is alright.
    
    // Adds up our properties
    props.putAll ( properties );
    
    doConfigure(props, hierarchy);
    
	}

	@Override
	/** Reads the config properties from the file and overrides them with the ones overridden in this configurator */
	public void doConfigure ( URL configURL, LoggerRepository hierarchy ) 
	{
		// Code copied from the original log4j configurator
    Properties props = new Properties();
    LogLog.debug("Reading configuration from URL " + configURL);
    try {
      props.load(configURL.openStream());
    }
    catch (java.io.IOException e) {
      LogLog.error("Could not read configuration file from URL [" + configURL + "].", e);
      LogLog.error("Ignoring configuration file [" + configURL +"].");
      return;
    }

    // Adds up our properties
    props.putAll ( properties );
    
    doConfigure (props, hierarchy);
	}
	
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to