Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.classic.util;
 
 import java.util.Map;
 import java.util.Set;
 
 
A Mapped Diagnostic Context, or MDC in short, is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

The MDC is managed on a per thread basis. A child thread automatically inherits a copy of the mapped diagnostic context of its parent.

For more information about MDC, please refer to the online manual at http://logback.qos.ch/manual/mdc.html

Author(s):
Ceki Gülcü
 
 public final class LogbackMDCAdapter implements MDCAdapter {
 
   // We wish to avoid unnecessarily copying of the map. To ensure
   // efficient/timely copying, we have a variable keeping track of the last
   // operation. A copy is necessary on 'put' or 'remove' but only if the last
   // operation was a 'get'. Get operations never necessitate a copy nor
   // successive 'put/remove' operations, only a get followed by a 'put/remove'
   // requires copying the map.
   // See http://jira.qos.ch/browse/LBCLASSIC-254 for the original discussion.
 
   // We no longer use CopyOnInheritThreadLocal in order to solve LBCLASSIC-183
   // Initially the contents of the thread local in parent and child threads
   // reference the same map. However, as soon as a thread invokes the put()
   // method, the maps diverge as they should.
 
   private static final int WRITE_OPERATION = 1;
   private static final int READ_OPERATION = 2;
 
   // keeps track of the last operation performed
 
   private Integer getAndSetLastOperation(int op) {
     Integer lastOp = .get();
     .set(op);
     return lastOp;
   }
 
   private boolean wasLastOpReadOrNull(Integer lastOp) {
     return lastOp == null || lastOp.intValue() == ;
   }
 
   private Map<StringStringduplicateAndInsertNewMap(Map<StringStringoldMap) {
     Map<StringStringnewMap = Collections.synchronizedMap(new HashMap<StringString>());
     if (oldMap != null) {
         // we don't want the parent thread modifying oldMap while we are
         // iterating over it
         synchronized (oldMap) {
           newMap.putAll(oldMap);
         }
     }
 
     .set(newMap);
     return newMap;
   }

  
Put a context value (the val parameter) as identified with the key parameter into the current thread's context map. Note that contrary to log4j, the val parameter can be null.

If the current thread does not have a context map it is created as a side effect of this call.

Throws:
java.lang.IllegalArgumentException in case the "key" parameter is null
 
   public void put(String keyString valthrows IllegalArgumentException {
     if (key == null) {
       throw new IllegalArgumentException("key cannot be null");
    }
    Map<StringStringoldMap = .get();
    if (wasLastOpReadOrNull(lastOp) || oldMap == null) {
      Map<StringStringnewMap = duplicateAndInsertNewMap(oldMap);
      newMap.put(keyval);
    } else {
      oldMap.put(keyval);
    }
  }

  
Remove the the context identified by the key parameter.

  public void remove(String key) {
    if (key == null) {
      return;
    }
    Map<StringStringoldMap = .get();
    if (oldMap == nullreturn;
    if (wasLastOpReadOrNull(lastOp)) {
      Map<StringStringnewMap = duplicateAndInsertNewMap(oldMap);
      newMap.remove(key);
    } else {
      oldMap.remove(key);
    }
  }


  
Clear all entries in the MDC.
  public void clear() {
  }

  
Get the context identified by the key parameter.

  public String get(String key) {
    Map<StringStringmap = getPropertyMap();
    if ((map != null) && (key != null)) {
      return map.get(key);
    } else {
      return null;
    }
  }

  
Get the current thread's MDC as a map. This method is intended to be used internally.
  public Map<StringStringgetPropertyMap() {
    return .get();
  }

  
Returns the keys in the MDC as a java.util.Set. The returned value can be null.
  public Set<StringgetKeys() {
    Map<StringStringmap = getPropertyMap();
    if (map != null) {
      return map.keySet();
    } else {
      return null;
    }
  }

  
Return a copy of the current thread's context map. Returned value may be null.
  public Map getCopyOfContextMap() {
    Map<StringStringhashMap = .get();
    if (hashMap == null) {
      return null;
    } else {
      return new HashMap<StringString>(hashMap);
    }
  }
  @SuppressWarnings("unchecked")
  public void setContextMap(Map contextMap) {
    Map<StringStringnewMap = Collections.synchronizedMap(new HashMap<StringString>());
    newMap.putAll(contextMap);
    // the newMap replaces the old one for serialisation's sake
  }
New to GrepCode? Check out our FAQ X