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/12/20 06:57:08 UTC

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

sdeboy      2004/12/19 21:57:08

  Added:       src/java/org/apache/log4j/varia MapFilter.java
  Log:
  MapFilter converts an event messages which implement java.util.Map into properties on the event.  (Similar to ReflectionFilter but map based instead of JavaBeans based).
  If the map contains an entry with a "message" key, the value of the entry is used as the rendered message.
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/log4j/varia/MapFilter.java
  
  Index: MapFilter.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.util.Hashtable;
  import java.util.Iterator;
  import java.util.Map;
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  
  public class MapFilter extends Filter {
  
  	/**
  	 * NOTE: This filter modifies logging events by adding properties to the event.
  	 * 
  	 * The object passed in as the event message must implement java.util.Map.
     * 
     * This filter converts the event message (a Map) into properties on the event.
     * 
     * If the map holds an entry with a key of "message", the value of the entry is used
     * as the rendered message.
     * 
  	 * @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);
  		}
  	
  		if (event.getMessage() instanceof Map) {
  			for (Iterator iter = ((Map)event.getMessage()).entrySet().iterator();iter.hasNext();) {
          Map.Entry entry = (Map.Entry)iter.next();
  				if ("message".equalsIgnoreCase(entry.getKey().toString())) {
  					event.setRenderedMessage(entry.getValue().toString());
  				} else {
  					eventProps.put(entry.getKey(), entry.getValue());
  				}
  			}
  			event.setProperties(eventProps);
  		}
  		return Filter.NEUTRAL;
  	}
  }
  
  
  

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