001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.core.lookup; 018 019 import org.apache.logging.log4j.core.LogEvent; 020 021 /** 022 * Lookup a String key to a String value. 023 * <p> 024 * This class represents the simplest form of a string to string map. 025 * It has a benefit over a map in that it can create the result on 026 * demand based on the key. 027 * <p> 028 * This class comes complete with various factory methods. 029 * If these do not suffice, you can subclass and implement your own matcher. 030 * <p> 031 * For example, it would be possible to implement a lookup that used the 032 * key as a primary key, and looked up the value on demand from the database 033 * 034 * @author Apache Software Foundation 035 * @version $Id$ 036 */ 037 public interface StrLookup { 038 /** 039 * Looks up a String key to a String value. 040 * <p> 041 * The internal implementation may use any mechanism to return the value. 042 * The simplest implementation is to use a Map. However, virtually any 043 * implementation is possible. 044 * <p> 045 * For example, it would be possible to implement a lookup that used the 046 * key as a primary key, and looked up the value on demand from the database 047 * Or, a numeric based implementation could be created that treats the key 048 * as an integer, increments the value and return the result as a string - 049 * converting 1 to 2, 15 to 16 etc. 050 * <p> 051 * The {@link #lookup(String)} method always returns a String, regardless of 052 * the underlying data, by converting it as necessary. For example: 053 * <pre> 054 * Map<String, Object> map = new HashMap<String, Object>(); 055 * map.put("number", new Integer(2)); 056 * assertEquals("2", StrLookup.mapLookup(map).lookup("number")); 057 * </pre> 058 * @param key the key to be looked up, may be null 059 * @return the matching value, null if no match 060 */ 061 String lookup(String key); 062 063 /** 064 * Looks up a String key to a String value possibly using the current LogEvent. 065 * <p> 066 * The internal implementation may use any mechanism to return the value. 067 * The simplest implementation is to use a Map. However, virtually any 068 * implementation is possible. 069 * <p> 070 * For example, it would be possible to implement a lookup that used the 071 * key as a primary key, and looked up the value on demand from the database 072 * Or, a numeric based implementation could be created that treats the key 073 * as an integer, increments the value and return the result as a string - 074 * converting 1 to 2, 15 to 16 etc. 075 * <p> 076 * The {@link #lookup(String)} method always returns a String, regardless of 077 * the underlying data, by converting it as necessary. For example: 078 * <pre> 079 * Map<String, Object> map = new HashMap<String, Object>(); 080 * map.put("number", new Integer(2)); 081 * assertEquals("2", StrLookup.mapLookup(map).lookup("number")); 082 * </pre> 083 * @param event The current LogEvent. 084 * @param key the key to be looked up, may be null 085 * @return the matching value, null if no match 086 */ 087 String lookup(LogEvent event, String key); 088 }