You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/06/29 00:40:30 UTC
[logging-log4j2] branch release-2.x updated (64dc5f9 -> c3acf33)
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a change to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.
from 64dc5f9 LOG4J2-2877 - Determine the container id to obtain container and image information
new 102c294 LOG4J2-2867 - Measure the time of writing the first log event
new f5392e6 LOG4J2-2867 - Obtain ContextDataProviders asynchronously
new c3acf33 Remove unused imports
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/logging/log4j/core/LoggerContext.java | 4 ++
.../log4j/core/impl/ThreadContextDataInjector.java | 49 ++++++++++++++----
.../JsonLayoutMillisTest.java => LoggingTest.java} | 58 ++++++++++------------
.../RollingRandomAccessFileManagerTest.java | 10 ++--
4 files changed, 73 insertions(+), 48 deletions(-)
copy log4j-core/src/test/java/org/apache/logging/log4j/core/{layout/JsonLayoutMillisTest.java => LoggingTest.java} (53%)
[logging-log4j2] 02/03: LOG4J2-2867 - Obtain ContextDataProviders
asynchronously
Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit f5392e6b87ea4829894eb0744bf1293cbc43fd99
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Jun 28 15:41:27 2020 -0700
LOG4J2-2867 - Obtain ContextDataProviders asynchronously
---
.../apache/logging/log4j/core/LoggerContext.java | 4 ++
.../log4j/core/impl/ThreadContextDataInjector.java | 49 +++++++++++++++++-----
.../org/apache/logging/log4j/core/LoggingTest.java | 4 +-
3 files changed, 44 insertions(+), 13 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
index fe1ec01..a7e5b60 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
@@ -27,6 +27,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -43,6 +44,7 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.impl.ThreadContextDataInjector;
import org.apache.logging.log4j.core.jmx.Server;
import org.apache.logging.log4j.core.util.Cancellable;
import org.apache.logging.log4j.core.util.ExecutorServices;
@@ -136,6 +138,7 @@ public class LoggerContext extends AbstractLifeCycle
externalMap.put(EXTERNAL_CONTEXT_KEY, externalContext);
}
this.configLocation = configLocn;
+ CompletableFuture.runAsync(ThreadContextDataInjector::initServiceProviders);
}
/**
@@ -164,6 +167,7 @@ public class LoggerContext extends AbstractLifeCycle
} else {
configLocation = null;
}
+ CompletableFuture.runAsync(ThreadContextDataInjector::initServiceProviders);
}
public void addShutdownListener(LoggerContextShutdownAware listener) {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
index 5ce7819..89623cd 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java
@@ -23,6 +23,8 @@ import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
@@ -52,7 +54,7 @@ import org.apache.logging.log4j.util.StringMap;
*/
public class ThreadContextDataInjector {
- private static Logger LOGGER = StatusLogger.getLogger();
+ private static final Logger LOGGER = StatusLogger.getLogger();
/**
* ContextDataProviders loaded via OSGi.
@@ -60,6 +62,38 @@ public class ThreadContextDataInjector {
public static Collection<ContextDataProvider> contextDataProviders =
new ConcurrentLinkedDeque<>();
+ private static volatile List<ContextDataProvider> serviceProviders = null;
+ private static final Lock providerLock = new ReentrantLock();
+
+ public static void initServiceProviders() {
+ if (serviceProviders == null) {
+ providerLock.lock();
+ try {
+ if (serviceProviders == null) {
+ serviceProviders = getServiceProviders();
+ }
+ } finally {
+ providerLock.unlock();
+ }
+ }
+ }
+
+ private static List<ContextDataProvider> getServiceProviders() {
+ List<ContextDataProvider> providers = new ArrayList<>();
+ for (final ClassLoader classLoader : LoaderUtil.getClassLoaders()) {
+ try {
+ for (final ContextDataProvider provider : ServiceLoader.load(ContextDataProvider.class, classLoader)) {
+ if (providers.stream().noneMatch((p) -> p.getClass().isAssignableFrom(provider.getClass()))) {
+ providers.add(provider);
+ }
+ }
+ } catch (final Throwable ex) {
+ LOGGER.debug("Unable to access Context Data Providers {}", ex.getMessage());
+ }
+ }
+ return providers;
+ }
+
/**
* Default {@code ContextDataInjector} for the legacy {@code Map<String, String>}-based ThreadContext (which is
* also the ThreadContext implementation used for web applications).
@@ -248,17 +282,10 @@ public class ThreadContextDataInjector {
}
private static List<ContextDataProvider> getProviders() {
+ initServiceProviders();
final List<ContextDataProvider> providers = new ArrayList<>(contextDataProviders);
- for (final ClassLoader classLoader : LoaderUtil.getClassLoaders()) {
- try {
- for (final ContextDataProvider provider : ServiceLoader.load(ContextDataProvider.class, classLoader)) {
- if (providers.stream().noneMatch((p) -> p.getClass().isAssignableFrom(provider.getClass()))) {
- providers.add(provider);
- }
- }
- } catch (final Throwable ex) {
- LOGGER.debug("Unable to access Context Data Providers {}", ex.getMessage());
- }
+ if (serviceProviders != null) {
+ providers.addAll(serviceProviders);
}
return providers;
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
index 4491627..6664ddf 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
@@ -86,11 +86,11 @@ public class LoggingTest {
logger.info("This is a test");
System.out.println(timer.stop());
timer = new Timer("more", 100);
- timer.start();
+/* timer.start();
for (int i=0; i < 100; ++i) {
logger.info("This is another test");
}
- System.out.println(timer.stop());
+ System.out.println(timer.stop());*/
}
}
[logging-log4j2] 03/03: Remove unused imports
Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit c3acf334850b168d4e0b862601430ff14f33d135
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Jun 28 17:40:17 2020 -0700
Remove unused imports
---
.../org/apache/logging/log4j/core/LoggingTest.java | 28 ----------------------
1 file changed, 28 deletions(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
index 6664ddf..0217d7b 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
@@ -16,31 +16,9 @@
*/
package org.apache.logging.log4j.core;
-import java.io.File;
-import java.util.Date;
-import java.util.HashMap;
import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.MarkerManager;
-import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.Configurator;
-import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.apache.logging.log4j.message.Message;
-import org.apache.logging.log4j.message.MessageFactory;
-import org.apache.logging.log4j.message.ParameterizedMessageFactory;
-import org.apache.logging.log4j.message.SimpleMessage;
-import org.apache.logging.log4j.message.StringFormatterMessageFactory;
-import org.apache.logging.log4j.message.StructuredDataMessage;
-import org.apache.logging.log4j.spi.AbstractLogger;
-import org.apache.logging.log4j.spi.MessageFactory2Adapter;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.apache.logging.log4j.util.Timer;
import org.junit.Before;
@@ -48,13 +26,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
public class LoggingTest {
[logging-log4j2] 01/03: LOG4J2-2867 - Measure the time of writing
the first log event
Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 102c29427c0aad60da3e51f48d03773b2028ebae
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Jun 28 15:14:44 2020 -0700
LOG4J2-2867 - Measure the time of writing the first log event
---
.../org/apache/logging/log4j/core/LoggingTest.java | 96 ++++++++++++++++++++++
.../RollingRandomAccessFileManagerTest.java | 10 +--
2 files changed, 101 insertions(+), 5 deletions(-)
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
new file mode 100644
index 0000000..4491627
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggingTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.MarkerManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.MessageFactory;
+import org.apache.logging.log4j.message.ParameterizedMessageFactory;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.message.StringFormatterMessageFactory;
+import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.apache.logging.log4j.spi.AbstractLogger;
+import org.apache.logging.log4j.spi.MessageFactory2Adapter;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.apache.logging.log4j.util.Timer;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class LoggingTest {
+
+ private static final String CONFIG = "log4j-list.xml";
+
+ @Rule
+ public final TestName testName = new TestName();
+ private ListAppender list;
+
+
+ @Rule
+ public LoggerContextRule context = new LoggerContextRule(CONFIG);
+
+ private void assertEventCount(final List<LogEvent> events, final int expected) {
+ assertEquals("Incorrect number of events.", expected, events.size());
+ }
+
+ @Before
+ public void before() {
+ logger = context.getLogger("LoggerTest");
+ }
+
+ org.apache.logging.log4j.Logger logger;
+
+ @Test
+ public void logTime() {
+ Timer timer = new Timer("initial");
+ timer.start();
+ logger.info("This is a test");
+ System.out.println(timer.stop());
+ timer = new Timer("more", 100);
+ timer.start();
+ for (int i=0; i < 100; ++i) {
+ logger.info("This is another test");
+ }
+ System.out.println(timer.stop());
+ }
+}
+
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
index e1a1dd3..5e21478 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
@@ -228,11 +228,11 @@ public class RollingRandomAccessFileManagerTest {
// Create the manager.
final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy
.newBuilder()
- .setMax("7")
- .setMin("1")
- .setFileIndex("max")
- .setStopCustomActionsOnError(false)
- .setConfig(new DefaultConfiguration())
+ .withMax("7")
+ .withMin("1")
+ .withFileIndex("max")
+ .withStopCustomActionsOnError(false)
+ .withConfig(new DefaultConfiguration())
.build();
final RollingRandomAccessFileManager manager =
RollingRandomAccessFileManager.getRollingRandomAccessFileManager(