You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/01/19 08:18:46 UTC
[logging-log4j2] branch release-2.x updated: Removes dependency on log4j-samples-configuration
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 519008aff2 Removes dependency on log4j-samples-configuration
519008aff2 is described below
commit 519008aff20cb11812d12a6fede01c3f22dd64a2
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Jan 19 09:10:32 2023 +0100
Removes dependency on log4j-samples-configuration
Depending on an unpublished artifact causes problems with `mvn site`.
---
log4j-osgi/pom.xml | 6 --
.../log4j/osgi/tests/AbstractLoadBundleTest.java | 77 +-------------
.../logging/log4j/osgi/tests/CoreOsgiTest.java | 102 ++++++++++++++++++
.../log4j/osgi/tests/CustomConfiguration.java | 115 +++++++++++++++++++++
.../osgi/tests/CustomConfigurationFactory.java | 63 +++++++++++
log4j-osgi/src/test/resources/log4j2.custom | 1 +
6 files changed, 286 insertions(+), 78 deletions(-)
diff --git a/log4j-osgi/pom.xml b/log4j-osgi/pom.xml
index 1a4156f62a..adee48268a 100644
--- a/log4j-osgi/pom.xml
+++ b/log4j-osgi/pom.xml
@@ -83,12 +83,6 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.apache.logging.log4j.samples</groupId>
- <artifactId>log4j-samples-configuration</artifactId>
- <version>${project.version}</version>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
diff --git a/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
index c5a0843abd..f93c6def87 100644
--- a/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
+++ b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
@@ -16,10 +16,6 @@
*/
package org.apache.logging.log4j.osgi.tests;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -38,6 +34,9 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.FrameworkFactory;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
/**
* Tests a basic Log4J 'setup' in an OSGi container.
*/
@@ -71,10 +70,6 @@ public abstract class AbstractLoadBundleTest {
return installBundle("org.apache.logging.log4j.core");
}
- private Bundle getDummyBundle() throws BundleException {
- return installBundle("org.apache.logging.log4j.samples.log4j-samples-configuration");
- }
-
private Bundle get12ApiBundle() throws BundleException {
return installBundle("org.apache.logging.log4j.1.2-api");
}
@@ -224,68 +219,6 @@ public abstract class AbstractLoadBundleTest {
api.uninstall();
}
- /**
- * Tests LOG4J2-920.
- */
- @Test
- public void testLoadingOfConfigurableCoreClasses() throws BundleException, ReflectiveOperationException {
-
- final Bundle api = getApiBundle();
- final Bundle core = getCoreBundle();
- final Bundle dummy = getDummyBundle();
-
- start(api, core, dummy);
-
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final PrintStream logStream = new PrintStream(baos);
-
- final PrintStream bakStream = setupStream(api, logStream);
-
- log(dummy);
-
- setupStream(api, bakStream);
-
- // org.apache.logging.log4j.core.osgi.BundleContextSelector cannot be found by org.apache.logging.log4j.api
- final boolean result = baos.toString().contains("BundleContextSelector cannot be found");
- Assert.assertFalse("Core class BundleContextSelector cannot be loaded in OSGI setup", result);
-
- stop(api, core, dummy);
- uninstall(api, core, dummy);
- }
-
- /**
- * Tests the log of a simple message in an OSGi container
- */
- @Test
- public void testSimpleLogInAnOsgiContext() throws BundleException, ReflectiveOperationException {
-
- final Bundle api = getApiBundle();
- final Bundle core = getCoreBundle();
- final Bundle dummy = getDummyBundle();
-
- start(api, core, dummy);
-
- final PrintStream bakStream = System.out;
- try {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final PrintStream logStream = new PrintStream(baos);
- System.setOut(logStream);
-
- log(dummy);
-
- final String result = baos.toString().substring(
- 12).trim(); // remove the instant then the spaces at start and end, that are non constant
- Assert.assertEquals("[main] ERROR org.apache.logging.log4j.configuration.CustomConfiguration - Test OK",
- result);
- } finally {
- System.setOut(bakStream);
- }
-
- stop(api, core, dummy);
- uninstall(api, core, dummy);
- }
-
-
/**
* Tests the loading of the 1.2 Compatibility API bundle, its classes should be loadable from the Core bundle,
* and the class loader should be the same between a class from core and a class from compat
@@ -315,7 +248,7 @@ public abstract class AbstractLoadBundleTest {
/**
* Tests whether the {@link ServiceLoaderUtil} finds services in other bundles.
- *
+ *
* @throws BundleException
* @throws ReflectiveOperationException
*/
@@ -345,4 +278,4 @@ public abstract class AbstractLoadBundleTest {
uninstall(apiTests, api, core);
assertEquals("api-tests is not in ACTIVE state", Bundle.UNINSTALLED, apiTests.getState());
}
-}
\ No newline at end of file
+}
diff --git a/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java
new file mode 100644
index 0000000000..b4764ab512
--- /dev/null
+++ b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.osgi.tests;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.impl.Log4jContextFactory;
+import org.apache.logging.log4j.core.osgi.BundleContextSelector;
+import org.apache.logging.log4j.core.selector.ContextSelector;
+import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+import static org.junit.Assert.assertEquals;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.linkBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class CoreOsgiTest {
+
+ @org.ops4j.pax.exam.Configuration
+ public Option[] config() {
+ return options(linkBundle("org.apache.logging.log4j.api"),
+ linkBundle("org.apache.logging.log4j.core"),
+ linkBundle("org.apache.logging.log4j.1.2-api").start(false),
+ // required by Pax Exam's logging
+ linkBundle("slf4j.api"),
+ linkBundle("ch.qos.logback.classic"),
+ linkBundle("ch.qos.logback.core"),
+ junitBundles());
+ }
+
+ @Test
+ public void testSimpleLogInAnOsgiContext() {
+ final CustomConfiguration custom = getConfiguration();
+ // Logging
+ final Logger logger = LogManager.getLogger(getClass());
+ logger.info("Hello OSGI from Log4j2!");
+ assertEquals(1, custom.getEvents().size());
+ final LogEvent event = custom.getEvents().get(0);
+ assertEquals("Hello OSGI from Log4j2!", event.getMessage().getFormattedMessage());
+ assertEquals(Level.INFO, event.getLevel());
+ custom.clearEvents();
+ }
+
+ @Test
+ public void testLog4j12InAnOsgiContext() {
+ final CustomConfiguration custom = getConfiguration();
+ // Logging
+ final org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger(getClass());
+ logger.info("Hello OSGI from Log4j 1.2!");
+ assertEquals(1, custom.getEvents().size());
+ final LogEvent event = custom.getEvents().get(0);
+ assertEquals("Hello OSGI from Log4j 1.2!", event.getMessage().getFormattedMessage());
+ assertEquals(Level.INFO, event.getLevel());
+ custom.clearEvents();
+ }
+
+ /**
+ * Tests LOG4J2-920.
+ */
+ @Test
+ public void testBundleContextSelector() {
+ final LoggerContextFactory factory = LogManager.getFactory();
+ assertEquals(Log4jContextFactory.class, factory.getClass());
+ final ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
+ assertEquals(BundleContextSelector.class, selector.getClass());
+ }
+
+ private static CustomConfiguration getConfiguration() {
+ final LoggerContextFactory factory = LogManager.getFactory();
+ assertEquals(Log4jContextFactory.class, factory.getClass());
+ final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ final Configuration config = ctx.getConfiguration();
+ assertEquals(CustomConfiguration.class, config.getClass());
+ return (CustomConfiguration) config;
+ }
+}
diff --git a/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
new file mode 100644
index 0000000000..579572a3bc
--- /dev/null
+++ b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
@@ -0,0 +1,115 @@
+/*
+ * 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.osgi.tests;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.AbstractLifeCycle;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.ErrorHandler;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.AbstractConfiguration;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+
+/**
+ * This Configuration is the same as the DefaultConfiguration but shows how a
+ * custom configuration can be built programmatically
+ */
+public class CustomConfiguration extends AbstractConfiguration {
+
+ /**
+ * The name of the default configuration.
+ */
+ public static final String CONFIG_NAME = "Custom";
+
+ private final ListAppender appender = new ListAppender();
+
+ public CustomConfiguration(final LoggerContext loggerContext) {
+ this(loggerContext, ConfigurationSource.NULL_SOURCE);
+ }
+
+ /**
+ * Constructor to create the default configuration.
+ */
+ public CustomConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
+ super(loggerContext, source);
+ setName(CONFIG_NAME);
+ appender.start();
+ addAppender(appender);
+ final LoggerConfig root = getRootLogger();
+ root.addAppender(appender, null, null);
+ root.setLevel(Level.ALL);
+ }
+
+ @Override
+ protected void doConfigure() {
+ }
+
+ public List<LogEvent> getEvents() {
+ return appender.getEvents();
+ }
+
+ public void clearEvents() {
+ appender.getEvents().clear();
+ }
+
+ private static class ListAppender extends AbstractLifeCycle implements Appender {
+
+ private final List<LogEvent> events = Collections.<LogEvent>synchronizedList(new ArrayList<>());
+
+ @Override
+ public void append(LogEvent event) {
+ events.add(event.toImmutable());
+ }
+
+ @Override
+ public String getName() {
+ return "LIST";
+ }
+
+ @Override
+ public Layout<? extends Serializable> getLayout() {
+ return null;
+ }
+
+ @Override
+ public boolean ignoreExceptions() {
+ return false;
+ }
+
+ @Override
+ public ErrorHandler getHandler() {
+ return null;
+ }
+
+ @Override
+ public void setHandler(ErrorHandler handler) {
+ }
+
+ public List<LogEvent> getEvents() {
+ return events;
+ }
+
+ }
+}
diff --git a/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java
new file mode 100644
index 0000000000..92e3c15c82
--- /dev/null
+++ b/log4j-osgi/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.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.osgi.tests;
+
+import java.net.URI;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.Order;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+
+/**
+ * Factory to construct a CustomConfiguration.
+ */
+@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
+@Order(50)
+public class CustomConfigurationFactory extends ConfigurationFactory {
+
+ /**
+ * Valid file extensions for XML files.
+ */
+ public static final String[] SUFFIXES = new String[] {".custom"};
+
+ /**
+ * Returns the Configuration.
+ * @param source The InputSource.
+ * @return The Configuration.
+ */
+ @Override
+ public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
+ return new CustomConfiguration(loggerContext, source);
+ }
+
+ @Override
+ public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
+ return new CustomConfiguration(loggerContext);
+ }
+
+ /**
+ * Returns the file suffixes for XML files.
+ * @return An array of File extensions.
+ */
+ @Override
+ public String[] getSupportedTypes() {
+ return SUFFIXES;
+ }
+}
diff --git a/log4j-osgi/src/test/resources/log4j2.custom b/log4j-osgi/src/test/resources/log4j2.custom
new file mode 100644
index 0000000000..36435a1e28
--- /dev/null
+++ b/log4j-osgi/src/test/resources/log4j2.custom
@@ -0,0 +1 @@
+# This file enables the CustomConfigurationFactory
\ No newline at end of file