logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1178468 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers: log4j2-api/src/main/java/org/apache/logging/log4j/ log4j2-api/src/test/java/org/apache/logging/log4j/ log4j2-core/src/main/java/org/apache/logging/log4j/core/ log4j2-core...
Date Mon, 03 Oct 2011 17:27:39 GMT
Author: rgoers
Date: Mon Oct  3 17:27:39 2011
New Revision: 1178468

URL: http://svn.apache.org/viewvc?rev=1178468&view=rev
Log:
ThreadContext Map and Stack can be removed. Correct places where MDC was returning an Object

Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/flume/FlumeEvent.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/ThreadContext.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
Mon Oct  3 17:27:39 2011
@@ -33,21 +33,13 @@ public final class ThreadContext {
 
     private static ThreadLocal<Map<String, String>> localMap =
         new InheritableThreadLocal<Map<String, String>>() {
-            protected Map<String, String> initialValue() {
-                return new HashMap<String, String>();
-            }
-
             protected Map<String, String> childValue(Map<String, String> parentValue)
{
-                return parentValue == null ? new HashMap<String, String>() : new HashMap<String,
String>(parentValue);
+                return parentValue == null ? null : new HashMap<String, String>(parentValue);
             }
         };
 
     private static ThreadLocal<Stack<String>> localStack =
         new InheritableThreadLocal<Stack<String>>() {
-            protected Stack<String> initialValue() {
-                return new Stack<String>();
-            }
-
             protected Stack<String> childValue(Stack<String> parentValue) {
                 return parentValue == null ? null : (Stack<String>) parentValue.clone();
             }
@@ -70,7 +62,12 @@ public final class ThreadContext {
      * @param value The key value.
      */
     public static void put(String key, String value) {
-        localMap.get().put(key, value);
+        Map<String, String> map = localMap.get();
+        if (map == null) {
+            map = new HashMap<String, String>();
+            localMap.set(map);
+        }
+        map.put(key, value);
     }
 
     /**
@@ -81,7 +78,8 @@ public final class ThreadContext {
      * @return The value associated with the key or null.
      */
     public static String get(String key) {
-        return localMap.get().get(key);
+        Map<String, String> map = localMap.get();
+        return map == null ? null : map.get(key);
     }
 
     /**
@@ -90,14 +88,17 @@ public final class ThreadContext {
      * @param key The key to remove.
      */
     public static void remove(String key) {
-        localMap.get().remove(key);
+        Map<String, String> map = localMap.get();
+        if (map != null) {
+            map.remove(key);
+        }
     }
 
     /**
      * Clear the context.
      */
     public static void clear() {
-        localMap.get().clear();
+        localMap.remove();
     }
 
     /**
@@ -106,7 +107,8 @@ public final class ThreadContext {
      * @return True if the key is in the context, false otherwise.
      */
     public static boolean containsKey(String key) {
-        return localMap.get().containsKey(key);
+        Map<String, String> map = localMap.get();
+        return map == null ? false : map.containsKey(key);
     }
 
     /**
@@ -114,15 +116,16 @@ public final class ThreadContext {
      * intended to be used internally.
      * @return a copy of the context.
      */
-    public static Map<String, Object> getContext() {
-        return new HashMap<String, Object>(localMap.get());
+    public static Map<String, String> getContext() {
+        Map<String, String> map = localMap.get();
+        return map == null ? new HashMap<String, String>() : new HashMap<String,
String>(localMap.get());
     }
 
     /**
      * Clear the stack for this thread.
      */
     public static void clearStack() {
-        localStack.get().clear();
+        localStack.remove();
     }
 
     /**
@@ -130,7 +133,8 @@ public final class ThreadContext {
      * @return A copy of this thread's stack.
      */
     public static Stack<String> cloneStack() {
-        return (Stack<String>) localStack.get().clone();
+        Stack<String> stack = localStack.get();
+        return stack == null ? new Stack<String>() : (Stack<String>) stack.clone();
     }
 
     /**
@@ -148,7 +152,8 @@ public final class ThreadContext {
      * @see #setMaxDepth
      */
     public static int getDepth() {
-        return localStack.get().size();
+        Stack<String> stack = localStack.get();
+        return stack == null ? 0 : stack.size();
     }
 
     /**
@@ -161,7 +166,7 @@ public final class ThreadContext {
      */
     public static String pop() {
         Stack<String> s = localStack.get();
-        if (s.isEmpty()) {
+        if (s == null || s.isEmpty()) {
             return "";
         }
         return s.pop();
@@ -177,8 +182,8 @@ public final class ThreadContext {
      * @return String The innermost diagnostic context.
      */
     public static String peek() {
-         Stack<String> s = localStack.get();
-        if (s.isEmpty()) {
+        Stack<String> s = localStack.get();
+        if (s == null || s.isEmpty()) {
             return "";
         }
         return s.peek();
@@ -193,7 +198,11 @@ public final class ThreadContext {
      * @param message The new diagnostic context information.
      */
     public static void push(String message) {
-        localStack.get().push(message);
+        Stack<String> stack = localStack.get();
+        if (stack == null) {
+            stack = new Stack<String>();
+        }
+        stack.push(message);
     }
 
     /**

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-api/src/test/java/org/apache/logging/log4j/SimpleLogger.java
Mon Oct  3 17:27:39 2011
@@ -42,7 +42,7 @@ public class SimpleLogger extends Abstra
         sb.append(level.toString());
         sb.append(" ");
         sb.append(msg.getFormattedMessage());
-        Map<String, Object> mdc = ThreadContext.getContext();
+        Map<String, String> mdc = ThreadContext.getContext();
         if (mdc.size() > 0) {
             sb.append(" ");
             sb.append(mdc.toString());

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEvent.java
Mon Oct  3 17:27:39 2011
@@ -91,7 +91,7 @@ public interface LogEvent extends Serial
      * @doubt as mentioned elsewhere, think MDC and NDC should be combined into a thread
context object.
      * (RG) Still to do.
      */
-    Map<String, Object> getContextMap();
+    Map<String, String> getContextMap();
 
     /**
      * Get the NDC data.

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/flume/FlumeEvent.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/flume/FlumeEvent.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/flume/FlumeEvent.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/flume/FlumeEvent.java
Mon Oct  3 17:27:39 2011
@@ -45,7 +45,7 @@ class FlumeEvent extends EventBaseImpl i
 
     private final String hostname;
 
-    private final Map<String, Object> ctx = new HashMap<String, Object>();
+    private final Map<String, String> ctx = new HashMap<String, String>();
 
     private static final String DEFAULT_MDC_PREFIX = "mdc:";
 
@@ -71,7 +71,7 @@ class FlumeEvent extends EventBaseImpl i
             eventPrefix = DEFAULT_EVENT_PREFIX;
         }
         this.fields = new HashMap<String, byte[]>();
-        Map<String, Object> mdc = event.getContextMap();
+        Map<String, String> mdc = event.getContextMap();
         if (includes != null) {
             String[] array = includes.split(",");
             if (array.length > 0) {
@@ -85,7 +85,7 @@ class FlumeEvent extends EventBaseImpl i
             String[] array = excludes.split(",");
             if (array.length > 0) {
                 List<String> list = Arrays.asList(array);
-                for (Map.Entry<String, Object> entry : mdc.entrySet()) {
+                for (Map.Entry<String, String> entry : mdc.entrySet()) {
                     if (!list.contains(entry.getKey())) {
                         ctx.put(entry.getKey(), entry.getValue());
                     }
@@ -114,7 +114,7 @@ class FlumeEvent extends EventBaseImpl i
             }
         }
 
-        for (Map.Entry<String, Object> entry : ctx.entrySet()) {
+        for (Map.Entry<String, String> entry : ctx.entrySet()) {
             fields.put(mdcPrefix + entry.getKey(), entry.getValue().toString().getBytes());
         }
 
@@ -212,7 +212,7 @@ class FlumeEvent extends EventBaseImpl i
         return event.getThrown();
     }
 
-    public Map<String, Object> getContextMap() {
+    public Map<String, String> getContextMap() {
         return ctx;
     }
 

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/filter/ThreadContextMapFilter.java
Mon Oct  3 17:27:39 2011
@@ -73,7 +73,7 @@ public class ThreadContextMapFilter exte
 
     @Override
     public Result filter(LogEvent event) {
-        Map<String, Object> ctx = event.getContextMap();
+        Map<String, String> ctx = event.getContextMap();
         boolean match = false;
         for (String key : map.keySet()) {
             match = map.get(key).equals(ctx.get(key));

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java
Mon Oct  3 17:27:39 2011
@@ -44,7 +44,7 @@ public class Log4jLogEvent implements Lo
     private final Message message;
     private final long timestamp;
     private final ThrowableProxy throwable;
-    private final Map<String, Object> mdc;
+    private final Map<String, String> mdc;
     private final Stack<String> ndc;
     private String threadName = null;
     private StackTraceElement location;
@@ -78,7 +78,7 @@ public class Log4jLogEvent implements Lo
      * @param timestamp The timestamp of the event.
      */
     public Log4jLogEvent(String loggerName, Marker marker, String fqcn, Level level, Message
message, Throwable t,
-                         Map<String, Object> mdc, Stack<String> ndc, String threadName,
StackTraceElement location,
+                         Map<String, String> mdc, Stack<String> ndc, String threadName,
StackTraceElement location,
                          long timestamp) {
         name = loggerName;
         this.marker = marker;
@@ -131,7 +131,7 @@ public class Log4jLogEvent implements Lo
      * @doubt Allows direct access to the map passed into the constructor, would allow appender
      * or layout to manipulate event as seen by other appenders.
      */
-    public Map<String, Object> getContextMap() {
+    public Map<String, String> getContextMap() {
         return mdc;
     }
 
@@ -210,7 +210,7 @@ public class Log4jLogEvent implements Lo
         private final Message message;
         private final long timestamp;
         private final Throwable throwable;
-        private final HashMap<String, Object> mdc;
+        private final HashMap<String, String> mdc;
         private final Stack<String> ndc;
         private String threadName;
         private StackTraceElement location;
@@ -223,7 +223,7 @@ public class Log4jLogEvent implements Lo
             this.message = event.message;
             this.timestamp = event.timestamp;
             this.throwable = event.throwable;
-            this.mdc = new HashMap<String, Object>(event.mdc);
+            this.mdc = new HashMap<String, String>(event.mdc);
             this.ndc = event.ndc;
             this.location = event.getSource();
             this.threadName = event.getThreadName();

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/RFC5424Layout.java
Mon Oct  3 17:27:39 2011
@@ -300,7 +300,7 @@ public class RFC5424Layout extends Abstr
         buf.append(Integer.toString(val));
     }
 
-    private void formatStructuredElement(StructuredDataId id, Map<String, Object> data,
StringBuilder sb,
+    private void formatStructuredElement(StructuredDataId id, Map<String, String> data,
StringBuilder sb,
                                          ListChecker checker)
     {
         if (id == null && defaultId == null)
@@ -330,19 +330,19 @@ public class RFC5424Layout extends Abstr
         return sb.toString();
     }
 
-    private void checkRequired(Map<String, Object> map) {
+    private void checkRequired(Map<String, String> map) {
         for (String key : mdcRequired) {
-            Object value = map.get(key);
+            String value = map.get(key);
             if (value == null) {
                 throw new LoggingException("Required key " + key + " is missing from the
" + mdcId);
             }
         }
     }
 
-    private void appendMap(Map<String, Object> map, StringBuilder sb, ListChecker checker)
+    private void appendMap(Map<String, String> map, StringBuilder sb, ListChecker checker)
     {
-        SortedMap<String, Object> sorted = new TreeMap<String, Object>(map);
-        for (Map.Entry<String, Object> entry : sorted.entrySet())
+        SortedMap<String, String> sorted = new TreeMap<String, String>(map);
+        for (Map.Entry<String, String> entry : sorted.entrySet())
         {
             if (checker.check(entry.getKey())) {
                 sb.append(" ");

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/layout/XMLLayout.java
Mon Oct  3 17:27:39 2011
@@ -144,7 +144,7 @@ public class XMLLayout extends AbstractS
 
         if (properties && event.getContextMap().size() > 0) {
             buf.append("<log4j:properties>\r\n");
-            for (Map.Entry<String, Object> entry : event.getContextMap().entrySet())
{
+            for (Map.Entry<String, String> entry : event.getContextMap().entrySet())
{
                 buf.append("<log4j:data name=\"");
                 buf.append(Transform.escapeTags(entry.getKey()));
                 buf.append("\" value=\"");

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
Mon Oct  3 17:27:39 2011
@@ -63,7 +63,7 @@ public final class MDCPatternConverter e
      * {@inheritDoc}
      */
     public void format(final LogEvent event, final StringBuilder toAppendTo) {
-        Map<String, Object> contextMap = event.getContextMap();
+        Map<String, String> contextMap = event.getContextMap();
         // if there is no additional options, we output every single
         // Key/Value pair for the MDC in a similar format to Hashtable.toString()
         if (key == null) {

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java?rev=1178468&r1=1178467&r2=1178468&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/slf4j/helpers/Log4JMDCAdapter.java
Mon Oct  3 17:27:39 2011
@@ -44,13 +44,7 @@ public class Log4JMDCAdapter implements 
     }
 
     public Map getCopyOfContextMap() {
-        Map<String, Object> ctx = ThreadContext.getContext();
-        Map<String, String> map = new HashMap<String, String>();
-
-        for (Map.Entry<String, Object> entry : ctx.entrySet()) {
-            map.put(entry.getKey(), entry.getValue().toString());
-        }
-        return map;
+        return ThreadContext.getContext();
     }
 
     public void setContextMap(Map map) {



Mime
View raw message