You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by sd...@apache.org on 2004/07/20 08:31:40 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/varia ReflectionFilter.java

sdeboy      2004/07/19 23:31:40

  Added:       src/java/org/apache/log4j/varia ReflectionFilter.java
  Log:
  Added a new filter which expects JavaBeans-style objects passed in the logger.debug, etc. calls as the message.
  The filter runs the collection of methods available from the BeanInfo PropertyDescriptor array and sets each method name/value pair as a property of the logging event
  The object being examined will rely on toString as the message unless an explicit 'message' property is provided.
  NOTE: This filter MODIFIES the logging event by setting properties and setting the rendered message (if the message property was provided).
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/log4j/varia/ReflectionFilter.java
  
  Index: ReflectionFilter.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.varia;
  
  import java.beans.IntrospectionException;
  import java.beans.Introspector;
  import java.beans.PropertyDescriptor;
  import java.lang.reflect.InvocationTargetException;
  import java.util.Hashtable;
  import java.util.Map;
  
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  
  public class ReflectionFilter extends Filter {
  
  	/**
  	 * NOTE: This filter modifies logging events by adding   
  	 * properties to the event.
  	 * 
  	 * The object passed in as the message must provide a message via toString 
  	 * or provide a 'message' property, which will be set as the rendered message.
  	 * 
  	 * This ReflectionFilter uses the JavaBeans BeanInfo and PropertyDescriptor mechanisms to discover 
  	 * readMethods available on the 'message' object provided by the event.
  	 *  
  	 * For each method available on the object via the BeanInfo PropertyDescriptors, the method is executed
  	 * and a property is added to the event, using the results of the method call as the value 
  	 * and the method name as the key.
  	 * 
  	 * @since 1.3
  	 */
  	public int decide(LoggingEvent event) {
  		Map properties = event.getProperties();
  		Hashtable eventProps = null;
  		if (properties == null) {
  			eventProps = new Hashtable();
  		} else {
  			eventProps = new Hashtable(properties);
  		}
  	
  		//ignore strings and the object class properties
  		if (!(event.getMessage() instanceof String)) {
  			PropertyDescriptor[] props;
  			try {
  				props = Introspector.getBeanInfo(event.getMessage().getClass(), Object.class).getPropertyDescriptors();
  				for (int i=0;i<props.length;i++) {
  					if ("message".equalsIgnoreCase(props[i].getName())) {
  						event.setRenderedMessage(props[i].getReadMethod().invoke(event.getMessage(), null).toString());
  					} else {
  						eventProps.put(props[i].getName(), props[i].getReadMethod().invoke(event.getMessage(), null).toString());
  					}
  				}
  				event.setProperties(eventProps);
  			} catch (IntrospectionException e) {
  				e.printStackTrace();
  			} catch (IllegalArgumentException e1) {
  				e1.printStackTrace();
  			} catch (IllegalAccessException e1) {
  				e1.printStackTrace();
  			} catch (InvocationTargetException e1) {
  				e1.printStackTrace();
  			}
  		}
  		return Filter.NEUTRAL;
  	}
  }
  
  
  

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