logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1425430 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/ core/src/main/java/org/apache/logging/log4j/core/config/ core/src/main/java/org/apache/logging/log4j/core/config/plugins/ core/src/main/java/org/ap...
Date Sun, 23 Dec 2012 07:33:10 GMT
Author: rgoers
Date: Sun Dec 23 07:33:09 2012
New Revision: 1425430

URL: http://svn.apache.org/viewvc?rev=1425430&view=rev
Log:
Added hostName and contextName to property map.

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/NetUtils.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test2.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
Sun Dec 23 07:33:09 2012
@@ -18,6 +18,8 @@ package org.apache.logging.log4j.core;
 
 import java.io.File;
 import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.locks.Lock;
@@ -29,6 +31,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.DefaultConfiguration;
 import org.apache.logging.log4j.core.config.NullConfiguration;
 import org.apache.logging.log4j.core.config.Reconfigurable;
+import org.apache.logging.log4j.core.helpers.NetUtils;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.spi.AbstractLogger;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -260,6 +263,10 @@ public class LoggerContext implements or
         }
         final Configuration prev = this.config;
         config.addListener(this);
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("hostName", NetUtils.getLocalHostname());
+        map.put("contextName", name);
+        config.addComponent(Configuration.CONTEXT_PROPERTIES, map);
         config.start();
         this.config = config;
         updateLoggers();

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
Sun Dec 23 07:33:09 2012
@@ -35,6 +35,7 @@ import org.apache.logging.log4j.core.fil
 import org.apache.logging.log4j.core.helpers.NameUtil;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 import org.apache.logging.log4j.core.lookup.Interpolator;
+import org.apache.logging.log4j.core.lookup.MapLookup;
 import org.apache.logging.log4j.core.lookup.StrLookup;
 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.status.StatusLogger;
@@ -45,6 +46,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -105,6 +107,10 @@ public class BaseConfiguration extends A
         rootNode = new Node();
     }
 
+    public Map<String, String> getProperties() {
+        return (Map<String, String>) componentMap.get(CONTEXT_PROPERTIES);
+    }
+
     /**
      * Initialize the configuration.
      */
@@ -165,7 +171,9 @@ public class BaseConfiguration extends A
                 }
                 continue;
             } else if (tempLookup == subst.getVariableResolver()) {
-                subst.setVariableResolver(new Interpolator(null));
+                Map<String, String> map = (Map<String, String>) componentMap.get(CONTEXT_PROPERTIES);
+                StrLookup lookup = map == null ? null : new MapLookup(map);
+                subst.setVariableResolver(new Interpolator(lookup));
             }
             if (child.getName().equalsIgnoreCase("appenders")) {
                 appenders = (ConcurrentMap<String, Appender>) child.getObject();

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java
Sun Dec 23 07:33:09 2012
@@ -30,6 +30,8 @@ import java.util.Map;
  */
 public interface Configuration extends Filterable {
 
+    public static final String CONTEXT_PROPERTIES = "ContextProperties";
+
     /**
      * Returns the configuration name.
      * @return the name of the configuration.
@@ -58,6 +60,8 @@ public interface Configuration extends F
 
     void setLoggerAdditive(Logger logger, boolean additive);
 
+    Map<String, String> getProperties();
+
     void start();
 
     void stop();

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
Sun Dec 23 07:33:09 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.config.plugins;
 
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.lookup.Interpolator;
 import org.apache.logging.log4j.core.lookup.MapLookup;
@@ -39,11 +40,12 @@ public final class PropertiesPlugin {
      * @return An Interpolator that includes the configuration properties.
      */
     @PluginFactory
-    public static StrLookup configureSubstitutor(@PluginElement("properties") final Property[]
properties) {
+    public static StrLookup configureSubstitutor(@PluginElement("properties") final Property[]
properties,
+                                                 @PluginConfiguration final Configuration
config) {
         if (properties == null) {
             return new Interpolator(null);
         }
-        final Map<String, String> map = new HashMap<String, String>();
+        final Map<String, String> map = new HashMap<String, String>(config.getProperties());
 
         for (final Property prop : properties) {
             map.put(prop.getName(), prop.getValue());

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/NetUtils.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/NetUtils.java?rev=1425430&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/NetUtils.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/NetUtils.java
Sun Dec 23 07:33:09 2012
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.core.helpers;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
+
+/**
+ *
+ */
+public final class NetUtils {
+
+    private static Logger LOGGER = StatusLogger.getLogger();
+
+    private NetUtils() {
+    }
+
+    /**
+     * This method gets the network name of the machine we are running on.
+     * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name
+     * cannot be found.
+     *
+     * @return String the name of the local host
+     */
+    public static String getLocalHostname() {
+        try {
+            final InetAddress addr = InetAddress.getLocalHost();
+            return addr.getHostName();
+        } catch (final UnknownHostException uhe) {
+            try {
+                final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+                while (interfaces.hasMoreElements()) {
+                    final NetworkInterface nic = interfaces.nextElement();
+                    final Enumeration<InetAddress> addresses = nic.getInetAddresses();
+                    while (addresses.hasMoreElements()) {
+                        final InetAddress address = addresses.nextElement();
+                        if (!address.isLoopbackAddress()) {
+                            final String hostname = address.getHostName();
+                            if (hostname != null) {
+                                return hostname;
+                            }
+                        }
+                    }
+                }
+            } catch (final SocketException se) {
+                LOGGER.error("Could not determine local host name", uhe);
+                return "UNKNOWN_LOCALHOST";
+            }
+            LOGGER.error("Could not determine local host name", uhe);
+            return "UNKNOWN_LOCALHOST";
+        }
+    }
+}

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
Sun Dec 23 07:33:09 2012
@@ -23,6 +23,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.helpers.NetUtils;
 import org.apache.logging.log4j.core.net.Facility;
 import org.apache.logging.log4j.core.net.Priority;
 import org.apache.logging.log4j.message.Message;
@@ -97,7 +98,7 @@ public final class RFC5424Layout extends
         this.mdcId = mdcId;
         this.appName = appName;
         this.messageId = messageId;
-        this.localHostName = getLocalHostname();
+        this.localHostName = NetUtils.getLocalHostname();
         ListChecker c = null;
         if (excludes != null) {
             final String[] array = excludes.split(",");
@@ -220,42 +221,6 @@ public final class RFC5424Layout extends
         return "-";
     }
 
-    /**
-     * This method gets the network name of the machine we are running on.
-     * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name
-     * cannot be found.
-     *
-     * @return String the name of the local host
-     */
-    public String getLocalHostname() {
-        try {
-            final InetAddress addr = InetAddress.getLocalHost();
-            return addr.getHostName();
-        } catch (final UnknownHostException uhe) {
-            try {
-                final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
-                while (interfaces.hasMoreElements()) {
-                    final NetworkInterface nic = interfaces.nextElement();
-                    final Enumeration<InetAddress> addresses = nic.getInetAddresses();
-                    while (addresses.hasMoreElements()) {
-                        final InetAddress address = addresses.nextElement();
-                        if (!address.isLoopbackAddress()) {
-                            final String hostname = address.getHostName();
-                            if (hostname != null) {
-                                return hostname;
-                            }
-                        }
-                    }
-                }
-            } catch (final SocketException se) {
-                LOGGER.error("Could not determine local host name", uhe);
-                return "UNKNOWN_LOCALHOST";
-            }
-            LOGGER.error("Could not determine local host name", uhe);
-            return "UNKNOWN_LOCALHOST";
-        }
-    }
-
     protected List<String> getMdcExcludes() {
         return mdcExcludes;
     }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java
Sun Dec 23 07:33:09 2012
@@ -24,21 +24,20 @@ import java.util.Map;
 
 /**
  * The basis for a lookup based on a Map.
- * @param <V> The type of object contained in the Map.
  */
 @Plugin(name = "map", type = "Lookup")
-public class MapLookup<V> implements StrLookup<V> {
+public class MapLookup implements StrLookup {
     /**
      * Map keys are variable names and value.
      */
-    private final Map<String, V> map;
+    private final Map<String, String> map;
 
     /**
      * Creates a new instance backed by a Map. Used by the default lookup.
      *
      * @param map the map of keys to values, may be null
      */
-    public MapLookup(final Map<String, V> map) {
+    public MapLookup(final Map<String, String> map) {
         this.map = map;
     }
 
@@ -62,11 +61,11 @@ public class MapLookup<V> implements Str
         if (map == null) {
             return null;
         }
-        final Object obj = map.get(key);
+        final String obj = map.get(key);
         if (obj == null) {
             return null;
         }
-        return obj.toString();
+        return obj;
     }
 
     public String lookup(final LogEvent event, final String key) {
@@ -74,9 +73,9 @@ public class MapLookup<V> implements Str
             return null;
         }
         if (map != null && map.containsKey(key)) {
-            final Object obj = map.get(key);
+            final String obj = map.get(key);
             if (obj != null) {
-                return obj.toString();
+                return obj;
             }
         }
         if (event.getMessage() instanceof MapMessage) {

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java
Sun Dec 23 07:33:09 2012
@@ -33,10 +33,8 @@ import org.apache.logging.log4j.core.Log
  *
  * @author Apache Software Foundation
  * @version $Id$
- *
- * @param <V> The type of the value that is being queried.
  */
-public interface StrLookup<V> {
+public interface StrLookup {
     /**
      * Looks up a String key to a String value.
      * <p>

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
Sun Dec 23 07:33:09 2012
@@ -133,7 +133,7 @@ public class StrSubstitutor {
     /**
      * Variable resolution is delegated to an implementor of VariableResolver.
      */
-    private StrLookup<?> variableResolver;
+    private StrLookup variableResolver;
     /**
      * The flag whether substitution in variable names is enabled.
      */
@@ -151,37 +151,34 @@ public class StrSubstitutor {
      * Creates a new instance and initializes it. Uses defaults for variable
      * prefix and suffix and the escaping character.
      *
-     * @param <V> The type of object contained in the Map.
      * @param valueMap  the map with the variables' values, may be null
      */
-    public <V> StrSubstitutor(final Map<String, V> valueMap) {
+    public StrSubstitutor(final Map<String, String> valueMap) {
         this(new MapLookup(valueMap), DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
     }
 
     /**
      * Creates a new instance and initializes it. Uses a default escaping character.
      *
-     * @param <V> The type of object contained in the Map.
      * @param valueMap  the map with the variables' values, may be null
      * @param prefix  the prefix for variables, not null
      * @param suffix  the suffix for variables, not null
      * @throws IllegalArgumentException if the prefix or suffix is null
      */
-    public <V> StrSubstitutor(final Map<String, V> valueMap, final String prefix,
final String suffix) {
+    public StrSubstitutor(final Map<String, String> valueMap, final String prefix,
final String suffix) {
         this(new MapLookup(valueMap), prefix, suffix, DEFAULT_ESCAPE);
     }
 
     /**
      * Creates a new instance and initializes it.
      *
-     * @param <V> The type of object contained in the Map.
      * @param valueMap  the map with the variables' values, may be null
      * @param prefix  the prefix for variables, not null
      * @param suffix  the suffix for variables, not null
      * @param escape  the escape character
      * @throws IllegalArgumentException if the prefix or suffix is null
      */
-    public <V> StrSubstitutor(final Map<String, V> valueMap, final String prefix,
final String suffix, final char escape) {
+    public StrSubstitutor(final Map<String, String> valueMap, final String prefix,
final String suffix, final char escape) {
         this(new MapLookup(valueMap), prefix, suffix, escape);
     }
 
@@ -190,7 +187,7 @@ public class StrSubstitutor {
      *
      * @param variableResolver  the variable resolver, may be null
      */
-    public StrSubstitutor(final StrLookup<?> variableResolver) {
+    public StrSubstitutor(final StrLookup variableResolver) {
         this(variableResolver, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
     }
 
@@ -203,7 +200,7 @@ public class StrSubstitutor {
      * @param escape  the escape character
      * @throws IllegalArgumentException if the prefix or suffix is null
      */
-    public StrSubstitutor(final StrLookup<?> variableResolver, final String prefix,
final String suffix, final char escape) {
+    public StrSubstitutor(final StrLookup variableResolver, final String prefix, final String
suffix, final char escape) {
         this.setVariableResolver(variableResolver);
         this.setVariablePrefix(prefix);
         this.setVariableSuffix(suffix);
@@ -219,7 +216,7 @@ public class StrSubstitutor {
      * @param escape  the escape character
      * @throws IllegalArgumentException if the prefix or suffix is null
      */
-    public StrSubstitutor(final StrLookup<?> variableResolver, final StrMatcher prefixMatcher,
final StrMatcher suffixMatcher,
+    public StrSubstitutor(final StrLookup variableResolver, final StrMatcher prefixMatcher,
final StrMatcher suffixMatcher,
                           final char escape) {
         this.setVariableResolver(variableResolver);
         this.setVariablePrefixMatcher(prefixMatcher);
@@ -231,12 +228,11 @@ public class StrSubstitutor {
      * Replaces all the occurrences of variables in the given source object with
      * their matching values from the map.
      *
-     * @param <V> The type of object contained in the Map.
      * @param source  the source text containing the variables to substitute, null returns
null
      * @param valueMap  the map with the values, may be null
      * @return the result of the replace operation
      */
-    public static <V> String replace(final Object source, final Map<String, V>
valueMap) {
+    public static String replace(final Object source, final Map<String, String> valueMap)
{
         return new StrSubstitutor(valueMap).replace(source);
     }
 
@@ -245,7 +241,6 @@ public class StrSubstitutor {
      * their matching values from the map. This method allows to specifiy a
      * custom variable prefix and suffix
      *
-     * @param <V> The type of object contained in the Map.
      * @param source  the source text containing the variables to substitute, null returns
null
      * @param valueMap  the map with the values, may be null
      * @param prefix  the prefix of variables, not null
@@ -253,7 +248,7 @@ public class StrSubstitutor {
      * @return the result of the replace operation
      * @throws IllegalArgumentException if the prefix or suffix is null
      */
-    public static <V> String replace(final Object source, final Map<String, V>
valueMap, final String prefix, final String suffix) {
+    public static String replace(final Object source, final Map<String, String> valueMap,
final String prefix, final String suffix) {
         return new StrSubstitutor(valueMap, prefix, suffix).replace(source);
     }
 
@@ -897,7 +892,7 @@ public class StrSubstitutor {
      */
     protected String resolveVariable(final LogEvent event, final String variableName, final
StringBuilder buf, final int startPos,
                                      final int endPos) {
-        final StrLookup<?> resolver = getVariableResolver();
+        final StrLookup resolver = getVariableResolver();
         if (resolver == null) {
             return null;
         }
@@ -1063,7 +1058,7 @@ public class StrSubstitutor {
      *
      * @return the VariableResolver
      */
-    public StrLookup<?> getVariableResolver() {
+    public StrLookup getVariableResolver() {
         return this.variableResolver;
     }
 
@@ -1072,7 +1067,7 @@ public class StrSubstitutor {
      *
      * @param variableResolver  the VariableResolver
      */
-    public void setVariableResolver(final StrLookup<?> variableResolver) {
+    public void setVariableResolver(final StrLookup variableResolver) {
         this.variableResolver = variableResolver;
     }
 

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
Sun Dec 23 07:33:09 2012
@@ -31,6 +31,7 @@ import org.apache.logging.log4j.MarkerMa
 import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.XMLConfigurationFactory;
+import org.apache.logging.log4j.core.helpers.NetUtils;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.message.ParameterizedMessageFactory;
 import org.apache.logging.log4j.message.StringFormatterMessageFactory;
@@ -50,6 +51,7 @@ public class LoggerTest {
     private static final String CONFIG = "log4j-test2.xml";
     private static Configuration config;
     private static ListAppender app;
+    private static ListAppender host;
     private static LoggerContext ctx;
 
     @BeforeClass
@@ -71,11 +73,14 @@ public class LoggerTest {
         for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet())
{
             if (entry.getKey().equals("List")) {
                 app = (ListAppender) entry.getValue();
-                break;
+            } else if (entry.getKey().equals("HostTest")) {
+                host = (ListAppender) entry.getValue();
             }
         }
         assertNotNull("No Appender", app);
+        assertNotNull("No Host Appender", host);
         app.clear();
+        host.clear();
     }
 
 
@@ -142,7 +147,7 @@ public class LoggerTest {
         final Logger testLogger =  testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatchNull",
StringFormatterMessageFactory.INSTANCE, null);
         testLogger.debug("%,d", Integer.MAX_VALUE);
         final List<LogEvent> events = app.getEvents();
-        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);        
+        assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size()
== 1);
         assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
     }
 
@@ -172,6 +177,18 @@ public class LoggerTest {
     }
 
     @Test
+    public void testHostname() {
+        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
+        testLogger.debug("Hello, {}", "World");
+        final List<String> msgs = host.getMessages();
+        assertTrue("Incorrect number of events. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
+        String expected = NetUtils.getLocalHostname() + System.getProperty("line.separator");
+        assertTrue("Incorrect hostname - expected " + expected + " actual - " + msgs.get(0),
+            msgs.get(0).endsWith(expected));
+
+    }
+
+    @Test
     public void mdc() {
         ThreadContext.put("TestYear", new Integer(2010).toString());
         logger.debug("Debug message");

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
Sun Dec 23 07:33:09 2012
@@ -51,7 +51,7 @@ public class InterpolatorTest {
     public void testLookup() {
         final Map<String, String> map = new HashMap<String, String>();
         map.put(TESTKEY, TESTVAL);
-        final StrLookup lookup = new Interpolator(new MapLookup<String>(map));
+        final StrLookup lookup = new Interpolator(new MapLookup(map));
         ThreadContext.put(TESTKEY, TESTVAL);
         String value = lookup.lookup(TESTKEY);
         assertEquals(TESTVAL, value);

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
Sun Dec 23 07:33:09 2012
@@ -50,7 +50,7 @@ public class StrSubstitutorTest {
     public void testLookup() {
         final Map<String, String> map = new HashMap<String, String>();
         map.put(TESTKEY, TESTVAL);
-        final StrLookup lookup = new Interpolator(new MapLookup<String>(map));
+        final StrLookup lookup = new Interpolator(new MapLookup(map));
         final StrSubstitutor subst = new StrSubstitutor(lookup);
         ThreadContext.put(TESTKEY, TESTVAL);
         String value = subst.replace("${TestKey}-${ctx:TestKey}-${sys:TestKey}");

Modified: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test2.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test2.xml?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test2.xml (original)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test2.xml Sun Dec 23 07:33:09
2012
@@ -44,6 +44,9 @@
     </File>
     <List name="List">
     </List>
+    <List name="HostTest">
+      <PatternLayout pattern="%d %p %C{1.} [%t] %m ${hostName}%n"/>
+    </List>
   </appenders>
 
   <loggers>
@@ -54,6 +57,10 @@
       <appender-ref ref="STDOUT"/>
     </logger>>
 
+    <logger name="org.apache.logging.log4j.hosttest" level="debug" additivity="false">
+      <appender-ref ref="HostTest"/>
+    </logger>
+
     <logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
       <appender-ref ref="File"/>
     </logger>>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Dec 23 07:33:09 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action dev="rgoers" type="add">
+        Added hostName and contextName to property map.
+      </action>
       <action issue="LOG4J2-135" dev="ggregory" type="fix" due-to="Ingo Feltes">
         BaseConfiguration does not close the first appender.
       </action>

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm?rev=1425430&r1=1425429&r2=1425430&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm Sun Dec 23 07:33:09
2012
@@ -696,7 +696,7 @@ public class Bar {
             Log4j 2 supports the ability to specify tokens in the configuration as references
to properties defined
             elsewhere. Some of these properties will be resolved when the configuration file
is interpreted while
             others may be passed to components where they will be evaluated at runtime. To
accomplish this, Log4j
-            uses variations Apache Commons Lang's
+            uses variations of Apache Commons Lang's
             <a href="../log4j-core/apidocs/org/apache/logging/log4j/core/lookup/StrSubstitutor">StrSubstitutor</a>
             and <a href="../log4j-core/apidocs/org/apache/logging/log4j/core/lookup/StrLookup">StrLookup</a>
             classes. In a manner similar to Ant or Maven, this allows variables declared
as <code>${name}</code>
@@ -784,6 +784,12 @@ public class Bar {
             </table>
           </p>
           <p>
+            A default property map can be declared in the configuration file. If the value
cannot be located in
+            the specified lookup the value in the default property map will be used. The
default map is
+            pre-populated with a value for "hostName" that is the current system's host name
or ip address and
+            the "contextName" with is the value of the current logging context.
+          </p>
+          <p>
             An interesting feature of StrLookup processing is that when a variable reference
is declared with
             multiple leading '$' characters each time the variable is resolved the leading
'$' is simply removed.
             In the previous example the "Routes" element is capable of resolving the variable
at runtime. To allow



Mime
View raw message