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 */
017package org.apache.logging.log4j.core.lookup;
018
019import 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 * <p>
029 * This class comes complete with various factory methods.
030 * If these do not suffice, you can subclass and implement your own matcher.
031 * </p>
032 * <p>
033 * For example, it would be possible to implement a lookup that used the
034 * key as a primary key, and looked up the value on demand from the database
035 * </p>
036 */
037public interface StrLookup {
038
039    /**
040     * Main plugin category for StrLookup plugins.
041     *
042     * @since 2.1
043     */
044    String CATEGORY = "Lookup";
045
046    /**
047     * Looks up a String key to a String value.
048     * <p>
049     * The internal implementation may use any mechanism to return the value.
050     * The simplest implementation is to use a Map. However, virtually any
051     * implementation is possible.
052     * </p>
053     * <p>
054     * For example, it would be possible to implement a lookup that used the
055     * key as a primary key, and looked up the value on demand from the database
056     * Or, a numeric based implementation could be created that treats the key
057     * as an integer, increments the value and return the result as a string -
058     * converting 1 to 2, 15 to 16 etc.
059     * </p>
060     * <p>
061     * This method always returns a String, regardless of
062     * the underlying data, by converting it as necessary. For example:
063     * </p>
064     * <pre>
065     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
066     * map.put("number", new Integer(2));
067     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
068     * </pre>
069     * @param key  the key to be looked up, may be null
070     * @return the matching value, null if no match
071     */
072    String lookup(String key);
073
074    /**
075     * Looks up a String key to a String value possibly using the current LogEvent.
076     * <p>
077     * The internal implementation may use any mechanism to return the value.
078     * The simplest implementation is to use a Map. However, virtually any
079     * implementation is possible.
080     * </p>
081     * <p>
082     * For example, it would be possible to implement a lookup that used the
083     * key as a primary key, and looked up the value on demand from the database
084     * Or, a numeric based implementation could be created that treats the key
085     * as an integer, increments the value and return the result as a string -
086     * converting 1 to 2, 15 to 16 etc.
087     * </p>
088     * <p>
089     * This method always returns a String, regardless of
090     * the underlying data, by converting it as necessary. For example:
091     * </p>
092     * <pre>
093     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
094     * map.put("number", new Integer(2));
095     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
096     * </pre>
097     * @param event The current LogEvent.
098     * @param key  the key to be looked up, may be null
099     * @return the matching value, null if no match
100     */
101    String lookup(LogEvent event, String key);
102}