You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2015/11/11 12:42:37 UTC
[2/6] logging-log4j2 git commit: LOG4J2-1172 made
ThreadNameCachingStrategy a top-level class
LOG4J2-1172 made ThreadNameCachingStrategy a top-level class
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a02bce46
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a02bce46
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a02bce46
Branch: refs/heads/master
Commit: a02bce4657743350aa0aa621ba9e67c280839f70
Parents: 680eb6e
Author: rpopma <rp...@apache.org>
Authored: Wed Nov 11 18:30:33 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Nov 11 18:30:33 2015 +0900
----------------------------------------------------------------------
.../core/async/ThreadNameCachingStrategy.java | 63 ++++++++++++++++++++
.../AsyncLoggerThreadNameStrategyTest.java | 24 ++++----
2 files changed, 74 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a02bce46/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java
new file mode 100644
index 0000000..69fb55b
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/ThreadNameCachingStrategy.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.async;
+
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.PropertiesUtil;
+
+/**
+ * Strategy for deciding whether thread name should be cached or not.
+ */
+enum ThreadNameCachingStrategy { // LOG4J2-467
+ CACHED {
+ @Override
+ public String getThreadName() {
+ String result = THREADLOCAL_NAME.get();
+ if (result == null) {
+ result = Thread.currentThread().getName();
+ THREADLOCAL_NAME.set(result);
+ }
+ return result;
+ }
+ },
+ UNCACHED {
+ @Override
+ public String getThreadName() {
+ return Thread.currentThread().getName();
+ }
+ };
+
+ private static final StatusLogger LOGGER = StatusLogger.getLogger();
+ private static final ThreadLocal<String> THREADLOCAL_NAME = new ThreadLocal<>();
+
+
+ abstract String getThreadName();
+
+ static ThreadNameCachingStrategy create() {
+ final String name = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ThreadNameStrategy",
+ CACHED.name());
+ try {
+ final ThreadNameCachingStrategy result = ThreadNameCachingStrategy.valueOf(name);
+ LOGGER.debug("AsyncLogger.ThreadNameStrategy={}", result);
+ return result;
+ } catch (final Exception ex) {
+ LOGGER.debug("Using AsyncLogger.ThreadNameStrategy.CACHED: '{}' not valid: {}", name, ex.toString());
+ return CACHED;
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a02bce46/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
index 0cf60bf..b904871 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerThreadNameStrategyTest.java
@@ -1,7 +1,7 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
+ * this work for additional rmation regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@@ -24,46 +24,44 @@ public class AsyncLoggerThreadNameStrategyTest {
@Test
public void testDefaultThreadNameIsCached() throws Exception {
- final Info.ThreadNameStrategy tns = Info.ThreadNameStrategy.create();
- assertSame(Info.ThreadNameStrategy.CACHED, tns);
+ final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create();
+ assertSame(ThreadNameCachingStrategy.CACHED, tns);
}
@Test
public void testUseCachedThreadNameIfInvalidConfig() throws Exception {
System.setProperty("AsyncLogger.ThreadNameStrategy", "\\%%InValid ");
- final Info.ThreadNameStrategy tns = Info.ThreadNameStrategy.create();
- assertSame(Info.ThreadNameStrategy.CACHED, tns);
+ final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create();
+ assertSame(ThreadNameCachingStrategy.CACHED, tns);
}
@Test
public void testUseUncachedThreadNameIfConfigured() throws Exception {
System.setProperty("AsyncLogger.ThreadNameStrategy", "UNCACHED");
- final Info.ThreadNameStrategy tns = Info.ThreadNameStrategy.create();
- assertSame(Info.ThreadNameStrategy.UNCACHED, tns);
+ final ThreadNameCachingStrategy tns = ThreadNameCachingStrategy.create();
+ assertSame(ThreadNameCachingStrategy.UNCACHED, tns);
}
@Test
public void testUncachedThreadNameStrategyReturnsCurrentThreadName() throws Exception {
- final Info info = new Info(null, "original", false);
final String name1 = "MODIFIED-THREADNAME1";
Thread.currentThread().setName(name1);
- assertEquals(name1, Info.ThreadNameStrategy.UNCACHED.getThreadName(info));
+ assertEquals(name1, ThreadNameCachingStrategy.UNCACHED.getThreadName());
final String name2 = "OTHER-THREADNAME2";
Thread.currentThread().setName(name2);
- assertEquals(name2, Info.ThreadNameStrategy.UNCACHED.getThreadName(info));
+ assertEquals(name2, ThreadNameCachingStrategy.UNCACHED.getThreadName());
}
@Test
public void testCachedThreadNameStrategyReturnsCachedThreadName() throws Exception {
final String original = "Original-ThreadName";
Thread.currentThread().setName(original);
- final Info info = new Info(null, original, false);
- assertEquals(original, Info.ThreadNameStrategy.CACHED.getThreadName(info));
+ assertEquals(original, ThreadNameCachingStrategy.CACHED.getThreadName());
final String name2 = "OTHER-THREADNAME2";
Thread.currentThread().setName(name2);
- assertEquals(original, Info.ThreadNameStrategy.CACHED.getThreadName(info));
+ assertEquals(original, ThreadNameCachingStrategy.CACHED.getThreadName());
}
}