[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. v_0.9.28-19-gce40eed
added by portage for gitosis-gentoo
git-noreply at pixie.qos.ch
Mon Mar 7 23:27:52 CET 2011
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Logback: the generic, reliable, fast and flexible logging framework.".
The branch, master has been updated
via ce40eedafcb09985ad861044dfecd803cfc0e9cc (commit)
from 6cacf873a1691b3ce233e5eebaf846f652cdb12b (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=ce40eedafcb09985ad861044dfecd803cfc0e9cc
http://github.com/ceki/logback/commit/ce40eedafcb09985ad861044dfecd803cfc0e9cc
commit ce40eedafcb09985ad861044dfecd803cfc0e9cc
Author: Ceki Gulcu <ceki at qos.ch>
Date: Mon Mar 7 23:23:35 2011 +0100
optimized LogbackMDCAppender as requested in http://jira.qos.ch/browse/LBCLASSIC-254
diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/util/LogbackMDCAdapter.java b/logback-classic/src/main/java/ch/qos/logback/classic/util/LogbackMDCAdapter.java
index 83129b9..acf427b 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/util/LogbackMDCAdapter.java
+++ b/logback-classic/src/main/java/ch/qos/logback/classic/util/LogbackMDCAdapter.java
@@ -44,9 +44,22 @@ public class LogbackMDCAdapter implements MDCAdapter {
// method, the maps diverge as they should.
final InheritableThreadLocal<HashMap<String, String>> copyOnInheritThreadLocal = new InheritableThreadLocal<HashMap<String, String>>();
+ private static final int WRITE_OPERATION = 1;
+ private static final int READ_OPERATION = 2;
+
+ final ThreadLocal<Integer> lastOperation = new ThreadLocal<Integer>();
+
+
+
public LogbackMDCAdapter() {
}
+ private Integer getAndSetLastOperation(int op) {
+ Integer lastOp = lastOperation.get();
+ lastOperation.set(WRITE_OPERATION);
+ return lastOp;
+ }
+
/**
* Put a context value (the <code>val</code> parameter) as identified with the
* <code>key</code> parameter into the current thread's context map. Note that
@@ -70,14 +83,19 @@ public class LogbackMDCAdapter implements MDCAdapter {
}
HashMap<String, String> oldMap = copyOnInheritThreadLocal.get();
-
- HashMap<String, String> newMap = new HashMap<String, String>();
- if (oldMap != null) {
- newMap.putAll(oldMap);
+ Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);
+
+ if(lastOp == null || lastOp.intValue() == READ_OPERATION) {
+ HashMap<String, String> newMap = new HashMap<String, String>();
+ if (oldMap != null) {
+ newMap.putAll(oldMap);
+ }
+ // the newMap replaces the old one for serialisation's sake
+ copyOnInheritThreadLocal.set(newMap);
+ newMap.put(key, val);
+ } else {
+ oldMap.put(key, val);
}
- // the newMap replaces the old one for serialisation's sake
- copyOnInheritThreadLocal.set(newMap);
- newMap.put(key, val);
}
/**
@@ -96,6 +114,7 @@ public class LogbackMDCAdapter implements MDCAdapter {
}
}
+
/**
* Remove the the context identified by the <code>key</code> parameter.
* <p/>
@@ -110,14 +129,19 @@ public class LogbackMDCAdapter implements MDCAdapter {
return;
}
HashMap<String, String> oldMap = copyOnInheritThreadLocal.get();
+ if(oldMap == null) return;
- HashMap<String, String> newMap = new HashMap<String, String>();
- if (oldMap != null) {
+ Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);
+
+ if(lastOp == null || lastOp.intValue() == READ_OPERATION) {
+ HashMap<String, String> newMap = new HashMap<String, String>();
newMap.putAll(oldMap);
+ // the newMap replaces the old one for serialisation's sake
+ copyOnInheritThreadLocal.set(newMap);
+ newMap.remove(key);
+ } else {
+ oldMap.remove(key);
}
- // the newMap replaces the old one for serialisation's sake
- copyOnInheritThreadLocal.set(newMap);
- newMap.remove(key);
}
/**
@@ -125,11 +149,8 @@ public class LogbackMDCAdapter implements MDCAdapter {
*/
public void clear() {
HashMap<String, String> hashMap = copyOnInheritThreadLocal.get();
-
- if (hashMap != null) {
- hashMap.clear();
- copyOnInheritThreadLocal.remove();
- }
+ Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);
+ copyOnInheritThreadLocal.remove();
}
/**
@@ -137,6 +158,7 @@ public class LogbackMDCAdapter implements MDCAdapter {
* internally.
*/
public Map<String, String> getPropertyMap() {
+ lastOperation.set(READ_OPERATION);
return copyOnInheritThreadLocal.get();
}
@@ -158,6 +180,7 @@ public class LogbackMDCAdapter implements MDCAdapter {
* null.
*/
public Set<String> getKeys() {
+ lastOperation.set(READ_OPERATION);
HashMap<String, String> hashMap = copyOnInheritThreadLocal.get();
if (hashMap != null) {
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/util/LogbackMDCAdapterTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/util/LogbackMDCAdapterTest.java
index 8edb657..767b490 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/util/LogbackMDCAdapterTest.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/util/LogbackMDCAdapterTest.java
@@ -19,6 +19,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.slf4j.MDC;
@@ -32,6 +33,7 @@ public class LogbackMDCAdapterTest {
int diff = RandomUtil.getPositiveInt();
+
/**
* Test that CopyOnInheritThreadLocal does not barf when the
* MDC hashmap is null
@@ -92,29 +94,30 @@ public class LogbackMDCAdapterTest {
*/
@Test
public void copyOnInheritenceTest() throws InterruptedException {
- String mdcKey = "x" + diff;
- String otherMDCKey = "o" + diff;
- MDC.put(mdcKey, mdcKey + A_SUFFIX);
-
+ CountDownLatch countDownLatch = new CountDownLatch(1);
+ String firstKey = "x" + diff;
+ String secondKey = "o" + diff;
+ MDC.put(firstKey, firstKey + A_SUFFIX);
- ChildThreadForMDC childThread = new ChildThreadForMDC(mdcKey, otherMDCKey);
+ ChildThreadForMDC childThread = new ChildThreadForMDC(firstKey, secondKey, countDownLatch);
childThread.start();
- MDC.put(mdcKey, mdcKey + B_SUFFIX);
+ countDownLatch.await();
+ MDC.put(firstKey, firstKey + B_SUFFIX);
childThread.join();
- assertNull(MDC.get(otherMDCKey));
+ assertNull(MDC.get(secondKey));
assertTrue(childThread.successul);
HashMap<String, String> parentHM = getHashMapFromMDC();
assertTrue(parentHM != childThread.childHM);
HashMap<String, String> parentHMWitness = new HashMap<String, String>();
- parentHMWitness.put(mdcKey, mdcKey + B_SUFFIX);
+ parentHMWitness.put(firstKey, firstKey + B_SUFFIX);
assertEquals(parentHMWitness, parentHM);
HashMap<String, String> childHMWitness = new HashMap<String, String>();
- childHMWitness.put(mdcKey, mdcKey + A_SUFFIX);
- childHMWitness.put(otherMDCKey, otherMDCKey + A_SUFFIX);
+ childHMWitness.put(firstKey, firstKey + A_SUFFIX);
+ childHMWitness.put(secondKey, secondKey + A_SUFFIX);
assertEquals(childHMWitness, childThread.childHM);
}
@@ -122,22 +125,25 @@ public class LogbackMDCAdapterTest {
class ChildThreadForMDC extends Thread {
- String mdcKey;
- String otherMDCKey;
+ String firstKey;
+ String secondKey;
boolean successul;
HashMap<String, String> childHM;
+ CountDownLatch countDownLatch;
- ChildThreadForMDC(String mdcKey, String otherMDCKey) {
- this.mdcKey = mdcKey;
- this.otherMDCKey = otherMDCKey;
+ ChildThreadForMDC(String firstKey, String secondKey, CountDownLatch countDownLatch) {
+ this.firstKey = firstKey;
+ this.secondKey = secondKey;
+ this.countDownLatch = countDownLatch;
}
@Override
public void run() {
- MDC.put(otherMDCKey, otherMDCKey + A_SUFFIX);
- assertNotNull(MDC.get(mdcKey));
- assertEquals(mdcKey + A_SUFFIX, MDC.get(mdcKey));
- assertEquals(otherMDCKey + A_SUFFIX, MDC.get(otherMDCKey));
+ MDC.put(secondKey, secondKey + A_SUFFIX);
+ assertNotNull(MDC.get(firstKey));
+ assertEquals(firstKey + A_SUFFIX, MDC.get(firstKey));
+ countDownLatch.countDown();
+ assertEquals(secondKey + A_SUFFIX, MDC.get(secondKey));
successul = true;
childHM = getHashMapFromMDC();
}
-----------------------------------------------------------------------
Summary of changes:
.../logback/classic/util/LogbackMDCAdapter.java | 57 ++++++++++++++------
.../classic/util/LogbackMDCAdapterTest.java | 44 +++++++++-------
2 files changed, 65 insertions(+), 36 deletions(-)
hooks/post-receive
--
Logback: the generic, reliable, fast and flexible logging framework.
More information about the logback-dev
mailing list