All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.slf4j.TtlLogbackMdcAdapter Maven / Gradle / Ivy

The newest version!
package org.slf4j;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.slf4j.spi.MDCAdapter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static artoria.common.constant.Numbers.TWENTY;

/**
 * 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 Ceki Gülcü * @see org.slf4j.MDC * @see org.slf4j.spi.MDCAdapter * @see ch.qos.logback.classic.util.LogbackMDCAdapter */ public class TtlLogbackMdcAdapter implements MDCAdapter { // The internal map is copied so as // 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/LOGBACK-620 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. /** * The copy on thread local. */ private final ThreadLocal> copyOnThreadLocal = new TransmittableThreadLocal>(); /** * The keeps track of the last operation performed. */ @SuppressWarnings("AlibabaThreadLocalShouldRemove") private final ThreadLocal lastOperation = new TransmittableThreadLocal(); private static final int WRITE_OPERATION = 1; private static final int MAP_COPY_OPERATION = 2; static { changeAdapter(); } public static void changeAdapter() { // Change mdc adapter. MDC.mdcAdapter = new TtlLogbackMdcAdapter(); } private Integer getAndSetLastOperation(int op) { Integer lastOp = lastOperation.get(); lastOperation.set(op); return lastOp; } private boolean wasLastOpReadOrNull(Integer lastOp) { return lastOp == null || lastOp == MAP_COPY_OPERATION; } private Map duplicateAndInsertNewMap(Map oldMap) { Map newMap = Collections.synchronizedMap(new HashMap(TWENTY)); if (oldMap != null) { // we don't want the parent thread modifying oldMap while we are // iterating over it synchronized (oldMap) { newMap.putAll(oldMap); } } copyOnThreadLocal.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 IllegalArgumentException in case the "key" parameter is null */ @Override public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } } /** * Remove the the context identified by the key parameter. *

*/ @Override public void remove(String key) { if (key == null) { return; } Map oldMap = copyOnThreadLocal.get(); if (oldMap == null) { return; } Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp)) { Map newMap = duplicateAndInsertNewMap(oldMap); newMap.remove(key); } else { oldMap.remove(key); } } /** * Clear all entries in the MDC. */ @Override public void clear() { lastOperation.set(WRITE_OPERATION); copyOnThreadLocal.remove(); } /** * Get the context identified by the key parameter. *

*/ @Override public String get(String key) { final Map map = copyOnThreadLocal.get(); 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 getPropertyMap() { lastOperation.set(MAP_COPY_OPERATION); return copyOnThreadLocal.get(); } /** * Returns the keys in the MDC as a {@link Set}. The returned value can be * null. */ public Set getKeys() { Map map = 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. */ @Override public Map getCopyOfContextMap() { Map hashMap = copyOnThreadLocal.get(); if (hashMap == null) { return null; } else { return new HashMap(hashMap); } } @Override public void setContextMap(Map contextMap) { lastOperation.set(WRITE_OPERATION); Map newMap = Collections.synchronizedMap(new HashMap(TWENTY)); newMap.putAll(contextMap); // the newMap replaces the old one for serialisation's sake copyOnThreadLocal.set(newMap); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy