logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From psm...@apache.org
Subject cvs commit: logging-log4j/tests build.xml
Date Sun, 17 Oct 2004 23:24:13 GMT
psmith      2004/10/17 16:24:13

  Modified:    tests    build.xml
  Added:       tests/input/multiplex configTest-1.xml
               tests/src/java/org/apache/log4j/multiplex
                        MultiplexAppenderTest.java
               src/java/org/apache/log4j/multiplex MultiplexSelector.java
                        AppenderFactory.java MDCKeySelector.java
                        MultiplexAppender.java AppenderFactoryUtils.java
                        MultiplexSelectorSkeleton.java
               tests/witness/multiplex multiplex-test1_bob.txt
                        multiplex-test1_jane.txt
  Log:
  Committing very _very_ early version of the MultiplexAppender and related
  paraphernalia.
  
  This change adds the core MultiplexAppender, MultiplexSelector, and AppenderFactory plus
  some standard utility classes and methods, and some test cases.
  
  JavaDoc and Unit tests are of poor quality at this stage, and anyone that uses
  these classes at this stage should consider seeking some mental health advice.
  
  Committing so that the community can be involved in some discussions which relate
  to this and how Joran can configure things.
  
  Revision  Changes    Path
  1.1                  logging-log4j/tests/input/multiplex/configTest-1.xml
  
  Index: configTest-1.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE log4j:configuration>
  	  
  <log4j:configuration debug="true">
  	  
  	    
    <appender name="Multiplex" class="org.apache.log4j.multiplex.MultiplexAppender">
      
       <selector class="org.apache.log4j.multiplex.MDCKeySelector" >
        <param name="mdcKey" value="User" />
        <appenderFactory class="org.apache.log4j.multiplex.factories.TimeBasedRollingAppenderFactory">
         <param name="fileNamePattern" value="output/MultiplexAppenderTest-%i.log.%d{yyyy.MM.dd}"/>
        </appenderFactory>
       </selector>
       
    </appender>
  
    
    <root>
      <level value ="debug"/>
      <appender-ref ref="Multiplex" />
    </root>  
  </log4j:configuration>
  
  
  1.1                  logging-log4j/tests/src/java/org/apache/log4j/multiplex/MultiplexAppenderTest.java
  
  Index: MultiplexAppenderTest.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import junit.framework.TestCase;
  
  import org.apache.log4j.ConsoleAppender;
  import org.apache.log4j.LogManager;
  import org.apache.log4j.Logger;
  import org.apache.log4j.MDC;
  import org.apache.log4j.PatternLayout;
  import org.apache.log4j.multiplex.AppenderFactory;
  import org.apache.log4j.multiplex.AppenderFactoryUtils;
  import org.apache.log4j.multiplex.MDCKeySelector;
  import org.apache.log4j.multiplex.MultiplexAppender;
  import org.apache.log4j.rolling.RollingFileAppender;
  import org.apache.log4j.rolling.TimeBasedRollingPolicy;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.util.Compare;
  
  /**
   * @author psmith
   *  
   */
  public class MultiplexAppenderTest extends TestCase {
  	private Logger root;
  
  	private Logger logger = Logger
  			.getLogger(MultiplexAppenderTest.class);
  
  	public void setUp() {
  		root = Logger.getRootLogger();
  		root.addAppender(new ConsoleAppender(new PatternLayout(
  				"%d{ABSOLUTE} %c{1} [%properties{User}] %m%n") ));
  	}
  
  	public void testIllegalStates() {
  		MultiplexAppender appender = null;
  
  		appender = new MultiplexAppender();
  		try {
  			appender.activateOptions();
  			fail("Should have thrown an IllegalStateException because it should not be configured
correctly");
  		} catch (Exception e) {
  			// expected
  		}
  
  		appender = null;
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see junit.framework.TestCase#tearDown()
  	 */
  	protected void tearDown() throws Exception {
  		super.tearDown();
  		LogManager.shutdown();
  	}
  
  	public void testMDCSelector() throws Exception {
  
          
  		MultiplexAppender appender = new MultiplexAppender();
  
  		final String mdcKey = "User";
          MDCKeySelector selector = new MDCKeySelector(mdcKey);
  		appender.setSelector(selector);
          selector.setAppenderFactory(AppenderFactoryUtils.createSimpleMDCbasedFileAppender("output/MultiplexRollingFileAppenderTestMDC",
mdcKey, new PatternLayout("%m%n")));
  		selector.activateOptions();
          
          
          logger.addAppender(appender);
  
  		MDC.put(mdcKey, "Bob");
  
  		logger.debug("Hello Bob");
  
  		MDC.put(mdcKey, "Jane");
  		logger.debug("Hello Jane");
          
          MDC.put(mdcKey, "Bob");
          logger.debug("I wonder what Jane's file looks like");
          
  
          MDC.put(mdcKey, "Jane");
          logger.debug("Bob.  Be quiet.");
  
          
          assertTrue("Bob's file does not match expected", Compare.compare("witness/multiplex/multiplex-test1_bob.txt",
"output/MultiplexRollingFileAppenderTestMDC_Bob.log"));
          assertTrue("Jane's file does not match expected", Compare.compare("witness/multiplex/multiplex-test1_jane.txt",
"output/MultiplexRollingFileAppenderTestMDC_Jane.log"));
  
  	}
      
      // TODO we need a test that tests when the MDC value is null/not found 
      
      public void testcreateMDCAndDailyRollingAppenderFactory() {
       AppenderFactory factory = AppenderFactoryUtils.createMDCAndDailyRollingAppenderFactory("output/standardMDC",
"User", new PatternLayout("%m%n"));
       MDC.put("User", "Bob");
       LoggingEvent e = new LoggingEvent();
       
       RollingFileAppender appender = (RollingFileAppender) factory.create(e);
       
       assertEquals(appender.getTriggeringPolicy().getClass(), TimeBasedRollingPolicy.class);
       //TimeBasedRollingPolicy policy =(TimeBasedRollingPolicy) appender.getTriggeringPolicy();
      }
  }
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/MultiplexSelector.java
  
  Index: MultiplexSelector.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.spi.OptionHandler;
  
  /**
   * @author psmith
   *
   */
  public interface MultiplexSelector extends OptionHandler{
  
      public void setAppenderFactory(AppenderFactory factory);
      
      public Appender select(LoggingEvent e);
  
  	/**
  	 * 
  	 */
  	public void close();
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/AppenderFactory.java
  
  Index: AppenderFactory.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.spi.OptionHandler;
  
  /**
   * @author psmith
   *
   */
  
  public interface AppenderFactory extends OptionHandler{
  
      public Appender create(LoggingEvent e);
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/MDCKeySelector.java
  
  Index: MDCKeySelector.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * @author psmith
   *  
   */
  public class MDCKeySelector extends MultiplexSelectorSkeleton {
  
  	private String MDCKey;
  
  	/**
  	 * @param key
  	 */
  	public MDCKeySelector(String key) {
  		super();
  		MDCKey = key;
  	}
  
  	/**
  	 * @return Returns the mDCKey.
  	 */
  	public final String getMDCKey() {
  		return MDCKey;
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see org.apache.log4j.multiplex.MultiplexSelector#select(org.apache.log4j.spi.LoggingEvent)
  	 */
  	public Appender select(LoggingEvent e) {
          return lookupOrCreateAsNeeded(e.getProperty(getMDCKey()), e);
  	}
  
  	/**
  	 * @param key
  	 *            The mDCKey to set.
  	 */
  	public final void setMDCKey(String key) {
  		MDCKey = key;
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.log4j.spi.OptionHandler#activateOptions()
  	 */
  	public void activateOptions() {
          
          // TODO ?
  	}
  
  }
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/MultiplexAppender.java
  
  Index: MultiplexAppender.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.AppenderSkeleton;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * 
   * TODO discuss how the Selector and AppenderFactory works
   * 
   * 
   * @author psmith
   *  
   */
  public class MultiplexAppender extends AppenderSkeleton {
  
  	private MultiplexSelector selector;
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see org.apache.log4j.spi.OptionHandler#activateOptions()
  	 */
  	public void activateOptions() {
  
  
  		// check that we have a selector of something at this point
  		if (getSelector() == null) {
  			throw new IllegalStateException(
  					"Should have had a Selector defined at this point");
  		}
  
          // TODO work out how the Selector has it's AppenderFactory configured by Joran
        
  
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see org.apache.log4j.Appender#close()
  	 */
  	public void close() {
  		getSelector().close();
  
  	}
  
  	/**
  	 * @return Returns the selector.
  	 */
  	public final MultiplexSelector getSelector() {
  		return selector;
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see org.apache.log4j.Appender#requiresLayout()
  	 */
  	public boolean requiresLayout() {
  		//        TODO check this is correct
  		return true;
  	}
  
  	/**
  	 * @param selector
  	 *            The selector to set.
  	 */
  	public final void setSelector(MultiplexSelector selector) {
  		this.selector = selector;
  	}
  
  	/*
  	 * (non-Javadoc)
  	 * 
  	 * @see org.apache.log4j.WriterAppender#subAppend(org.apache.log4j.spi.LoggingEvent)
  	 */
  	protected void append(LoggingEvent event) {
  
  		// we assume appropriate syncronization has occured on this appender
  
  		// determinge the key to lookup the Appender to use
  		Appender appender = getSelector().select(event);
  		appender.doAppend(event);
  
  	}
  }
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/AppenderFactoryUtils.java
  
  Index: AppenderFactoryUtils.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import java.text.MessageFormat;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.FileAppender;
  import org.apache.log4j.Layout;
  import org.apache.log4j.PatternLayout;
  import org.apache.log4j.rolling.RollingFileAppender;
  import org.apache.log4j.rolling.TimeBasedRollingPolicy;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * @author psmith
   *  
   */
  public class AppenderFactoryUtils {
  
  	public static AppenderFactory createMDCAndDailyRollingAppenderFactory(
  			final String fullFilePathAndPrefix, final String mdcKey,
  			final Layout layout) {
  		return new AppenderFactory() {
  
  			public void activateOptions() {
  
  			}
  
  			public Appender create(LoggingEvent e) {
  				String value = e.getProperty(mdcKey);
  
  				String datePattern = "yyyy-MM-dd";
  
  				TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
  				String pattern = fullFilePathAndPrefix + "-" + value + "-%d{"
  						+ datePattern + "}";
  
  				tbrp.setFileNamePattern(pattern);
  				tbrp.activateOptions();
  				RollingFileAppender rfa = new RollingFileAppender();
  				rfa.setRollingPolicy(tbrp);
  				rfa.activateOptions();
  				return rfa;
  
  			}
  
  		};
  
  	}
  
  	private AppenderFactoryUtils() {
  	}
  
  	/**
  	 * @param mdcKey
  	 * @param layout
  	 * @return
  	 */
  	public static AppenderFactory createSimpleMDCbasedFileAppender(
  			final String fullFilePathAndPrefix, final String mdcKey,
  			final PatternLayout layout) {
  		return new AppenderFactory() {
  
  			public Appender create(LoggingEvent e) {
  				String value = e.getProperty(mdcKey);
  				String pattern = fullFilePathAndPrefix + "_{0}.log";
  				try {
  					FileAppender fileAppender = new FileAppender(layout,
  							MessageFormat.format(pattern,
  									new Object[] { value }));
  					fileAppender.activateOptions();
  					return fileAppender;
  				} catch (Exception ex) {
  					throw new RuntimeException(ex);
  				}
  			}
  
  			public void activateOptions() {
  
  			}
  		};
  	}
  }
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/multiplex/MultiplexSelectorSkeleton.java
  
  Index: MultiplexSelectorSkeleton.java
  ===================================================================
  package org.apache.log4j.multiplex;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import org.apache.log4j.Appender;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * @author psmith
   *  
   */
  public abstract class MultiplexSelectorSkeleton implements MultiplexSelector {
  
  	private AppenderFactory appenderFactory;
  
  	protected Map multiplexedAppenders = Collections
  			.synchronizedMap(new HashMap());
  
  	public void close() {
  		//        TODO probable should log that we're closing each appender
  		for (Iterator iter = multiplexedAppenders.values().iterator(); iter
  				.hasNext();) {
  			Appender appender = (Appender) iter.next();
  			appender.close();
  		}
  
  	}
  
  	/**
  	 * @return Returns the appenderFactory.
  	 */
  	public final AppenderFactory getAppenderFactory() {
  		return appenderFactory;
  	}
  
  	/**
  	 * @param appenderFactory
  	 *            The appenderFactory to set.
  	 */
  	public final void setAppenderFactory(AppenderFactory appenderFactory) {
  		this.appenderFactory = appenderFactory;
  	}
  
  	protected Appender lookupOrCreateAsNeeded(Object key, LoggingEvent e) {
  		Appender appender = (Appender) multiplexedAppenders.get(key);
  		if (appender == null) {
  			appender = getAppenderFactory().create(e);
  			multiplexedAppenders.put(key, appender);
  		}
  		return appender;
  
  	}
  
  }
  
  
  1.1                  logging-log4j/tests/witness/multiplex/multiplex-test1_bob.txt
  
  Index: multiplex-test1_bob.txt
  ===================================================================
  Hello Bob
  I wonder what Jane's file looks like
  
  
  1.1                  logging-log4j/tests/witness/multiplex/multiplex-test1_jane.txt
  
  Index: multiplex-test1_jane.txt
  ===================================================================
  Hello Jane
  Bob.  Be quiet.
  
  
  1.64      +11 -0     logging-log4j/tests/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/logging-log4j/tests/build.xml,v
  retrieving revision 1.63
  retrieving revision 1.64
  diff -u -r1.63 -r1.64
  --- build.xml	7 Sep 2004 17:16:04 -0000	1.63
  +++ build.xml	17 Oct 2004 23:24:13 -0000	1.64
  @@ -447,6 +447,17 @@
         <test name="org.apache.log4j.rolling.TimeBasedRollingTest" />
       </junit>
     </target>
  +	
  +
  +	  <target name="MultiplexAppenderTest" depends="build, cleanOutputDir">
  +	    <junit printsummary="yes" fork="yes" haltonfailure="yes">
  +	      <classpath refid="tests.classpath"/>
  +	      <formatter type="plain" usefile="false"/>
  +	      <test name="org.apache.log4j.multiplex.MultiplexAppenderTest" />
  +	    </junit>
  +	  </target>
  +	
  +	
     
     <target name="Compress" depends="build, cleanOutputDir">
       <!-- The input files are erased as a result of compression. We -->
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message