Author: ggregory
Date: Fri May 16 18:14:45 2014
New Revision: 1595279

URL: http://svn.apache.org/r1595279
Log:
Add LogEvent.getContextMap(String) to support easy ports from Log4j version 
1.2's getProperty(String) method. Since LogEvent.getContextMap() can return 
null, it is cumbersome to use since a null check is required for bullet-proof 
code, adding this 1-arg String method addresses the issue cleanly. 
getContextMap(String) is not a great method name, but 
getContextMapValue(String) seems weird and Log4j 1.2's getProperty(String) name 
does not fit.

Modified:
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLogEvent.java
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/BasicLogEventEntity.java
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
    
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
    
logging/log4j/log4j2/trunk/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEvent.java

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLogEvent.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLogEvent.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLogEvent.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/AbstractLogEvent.java
 Fri May 16 18:14:45 2014
@@ -37,6 +37,11 @@ public abstract class AbstractLogEvent i
     }
 
     @Override
+    public String getContextMap(String key) {
+        return null;
+    }
+
+    @Override
     public ContextStack getContextStack() {
         return null;
     }

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
 Fri May 16 18:14:45 2014
@@ -39,6 +39,15 @@ public interface LogEvent extends Serial
     Map<String, String> getContextMap();
 
     /**
+     * Gets the value at the given key in the context map.
+     * 
+     * @param key the key to query
+     * @return the value to which the specified key is mapped, or {@code null} 
if this map contains no mapping for the key or there is no
+     *         map.
+     */
+    String getContextMap(String key);
+
+    /**
      * Gets the NDC data.
      * 
      * @return A copy of the Nested Diagnostic Context or null;
@@ -160,4 +169,5 @@ public interface LogEvent extends Serial
      * @see #getSource()
      */
     void setIncludeLocation(boolean locationRequired);
+
 }

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/BasicLogEventEntity.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/BasicLogEventEntity.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/BasicLogEventEntity.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jpa/BasicLogEventEntity.java
 Fri May 16 18:14:45 2014
@@ -207,6 +207,18 @@ public abstract class BasicLogEventEntit
     }
 
     /**
+     * Gets the value at the given key in the context map.
+     * 
+     * @param key the key to query
+     * @return the value to which the specified key is mapped, or {@code null} 
if this map contains no mapping for the key or there is no
+     *         map.
+     */
+    @Override
+    public String getContextMap(String key) {
+        return this.getWrappedEvent().getContextMap(key);
+    }
+
+    /**
      * Gets the context stack. Annotated with {@code @Convert(converter = 
ContextStackAttributeConverter.class)}.
      *
      * @return the context stack.

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
 Fri May 16 18:14:45 2014
@@ -190,6 +190,11 @@ public class RingBufferLogEvent implemen
                return contextMap;
        }
 
+    @Override
+    public String getContextMap(String key) {
+        return contextMap == null ? null : contextMap.get(key);
+    }
+
        @Override
        public ContextStack getContextStack() {
                return contextStack;

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
 Fri May 16 18:14:45 2014
@@ -299,6 +299,18 @@ public class Log4jLogEvent implements Lo
     }
 
     /**
+     * Gets the value at the given key in the context map.
+     * 
+     * @param key the key to query
+     * @return the value to which the specified key is mapped, or {@code null} 
if this map contains no mapping for the key or there is no
+     *         map.
+     */
+    @Override
+    public String getContextMap(String key) {
+        return contextMap == null ? null : contextMap.get(key);
+    }
+
+    /**
      * Returns the StackTraceElement for the caller. This will be the entry 
that occurs right
      * before the first occurrence of FQCN as a class name.
      * @return the StackTraceElement for the caller.

Modified: 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HTMLLayout.java
 Fri May 16 18:14:45 2014
@@ -166,7 +166,7 @@ public final class HTMLLayout extends Ab
         sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
         sbuf.append("</tr>").append(Constants.LINE_SEPARATOR);
 
-        if (event.getContextStack().getDepth() > 0) {
+        if (event.getContextStack() != null && 
!event.getContextStack().isEmpty()) {
             sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : 
").append(fontSize);
             sbuf.append(";\" colspan=\"6\" ");
             sbuf.append("title=\"Nested Diagnostic Context\">");
@@ -174,7 +174,7 @@ public final class HTMLLayout extends Ab
             sbuf.append("</td></tr>").append(Constants.LINE_SEPARATOR);
         }
 
-        if (event.getContextMap().size() > 0) {
+        if (event.getContextMap() != null && !event.getContextMap().isEmpty()) 
{
             sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : 
").append(fontSize);
             sbuf.append(";\" colspan=\"6\" ");
             sbuf.append("title=\"Mapped Diagnostic Context\">");

Modified: 
logging/log4j/log4j2/trunk/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEvent.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEvent.java?rev=1595279&r1=1595278&r2=1595279&view=diff
==============================================================================
--- 
logging/log4j/log4j2/trunk/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEvent.java
 (original)
+++ 
logging/log4j/log4j2/trunk/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumeEvent.java
 Fri May 16 18:14:45 2014
@@ -62,7 +62,7 @@ public class FlumeEvent extends SimpleEv
 
     private final LogEvent event;
 
-    private final Map<String, String> ctx = new HashMap<String, String>();
+    private final Map<String, String> contextMap = new HashMap<String, 
String>();
 
     private final boolean compress;
 
@@ -95,7 +95,7 @@ public class FlumeEvent extends SimpleEv
                 for (String str : array) {
                     str = str.trim();
                     if (mdc.containsKey(str)) {
-                        ctx.put(str, mdc.get(str));
+                        contextMap.put(str, mdc.get(str));
                     }
                 }
             }
@@ -108,12 +108,12 @@ public class FlumeEvent extends SimpleEv
                 }
                 for (final Map.Entry<String, String> entry : mdc.entrySet()) {
                     if (!list.contains(entry.getKey())) {
-                        ctx.put(entry.getKey(), entry.getValue());
+                        contextMap.put(entry.getKey(), entry.getValue());
                     }
                 }
             }
         } else {
-            ctx.putAll(mdc);
+            contextMap.putAll(mdc);
         }
 
         if (required != null) {
@@ -140,7 +140,7 @@ public class FlumeEvent extends SimpleEv
             headers.put(GUID, guid);
         }
 
-        addContextData(mdcPrefix, headers, ctx);
+        addContextData(mdcPrefix, headers, contextMap);
     }
 
     protected void addStructuredData(final String prefix, final Map<String, 
String> fields,
@@ -291,7 +291,19 @@ public class FlumeEvent extends SimpleEv
      */
     @Override
     public Map<String, String> getContextMap() {
-        return ctx;
+        return contextMap;
+    }
+
+    /**
+     * Gets the value at the given key in the context map.
+     * 
+     * @param key the key to query
+     * @return the value to which the specified key is mapped, or {@code null} 
if this map contains no mapping for the key or there is no
+     *         map.
+     */
+    @Override
+    public String getContextMap(String key) {
+        return contextMap == null ? null : contextMap.get(key);
     }
 
     /**


Reply via email to