Skip to content

Update DI system with DSL for configuring bindings #2148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.plugins.di.Binding;

/**
* Base Configuration for Log4j 1.
Expand Down Expand Up @@ -64,7 +63,7 @@ public BuilderManager getBuilderManager() {
*/
@Override
public void initialize() {
instanceFactory.registerBinding(Binding.from(Configuration.KEY).toInstance(this));
instanceFactory.registerBinding(Configuration.KEY, () -> this);
getStrSubstitutor().setConfiguration(this);
getConfigurationStrSubstitutor().setConfiguration(this);
super.getScheduler().start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.ConfigurationFactoryType;
import org.apache.logging.log4j.message.MapMessage;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
Expand All @@ -47,6 +47,7 @@
/**
* Tests of Category.
*/
@ConfigurationFactoryType(BasicConfigurationFactory.class)
public class CategoryTest {

private static final String VERSION1_APPENDER_NAME = "Version1List";
Expand All @@ -58,15 +59,11 @@ public class CategoryTest {
public static void setupClass() {
appender.start();
version1Appender.setName(VERSION1_APPENDER_NAME);
System.setProperty(
Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey(),
BasicConfigurationFactory.class.getName());
}

@AfterAll
public static void cleanupClass() {
appender.stop();
System.clearProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey());
}

@BeforeEach
Expand Down
13 changes: 2 additions & 11 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.junit.jupiter.api.AfterAll;
import org.apache.logging.log4j.core.test.junit.ConfigurationFactoryType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* Used for internal unit testing the Logger class.
*/
@ConfigurationFactoryType(BasicConfigurationFactory.class)
public class LoggerTest {

Appender a1;
Expand All @@ -64,15 +64,6 @@ public static void setUpClass() {

rbCH = ResourceBundle.getBundle("L7D", new Locale("fr", "CH"));
assertNotNull(rbCH, "Got a null resource bundle.");

System.setProperty(
Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey(),
BasicConfigurationFactory.class.getName());
}

@AfterAll
public static void tearDownClass() {
System.clearProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey());
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -37,7 +37,7 @@
public class AutoConfigTest {

@Test
@LoggerContextSource(value = "log4j.xml", v1config = true)
@LegacyLoggerContextSource("log4j.xml")
public void testListAppender(final org.apache.logging.log4j.core.LoggerContext context) {
final Logger logger = LogManager.getLogger("test");
logger.debug("This is a test of the root logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
package org.apache.log4j.config;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.assertj.core.api.InstanceOfAssertFactories.STRING;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.HashMap;
import java.util.List;
Expand All @@ -29,7 +33,7 @@
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
import org.junit.jupiter.api.Test;

Expand All @@ -40,14 +44,14 @@
public class MapRewriteAppenderTest {

@Test
@LoggerContextSource(value = "log4j1-mapRewrite.xml", v1config = true)
@LegacyLoggerContextSource("log4j1-mapRewrite.xml")
public void testRewrite() {
final Logger logger = LogManager.getLogger("test");
final Map<String, String> map = new HashMap<>();
map.put("message", "This is a test");
map.put("hello", "world");
logger.debug(map);
final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
final LoggerContext context = LoggerContext.getContext(false);
final Configuration configuration = context.getConfiguration();
final Map<String, Appender> appenders = configuration.getAppenders();
ListAppender eventAppender = null;
Expand All @@ -58,9 +62,13 @@ public void testRewrite() {
}
assertNotNull(eventAppender, "No Event Appender");
final List<LoggingEvent> events = eventAppender.getEvents();
assertTrue(events != null && events.size() > 0, "No events");
assertNotNull(events.get(0).getProperties(), "No properties in the event");
assertTrue(events.get(0).getProperties().containsKey("hello"), "Key was not inserted");
assertEquals("world", events.get(0).getProperties().get("hello"), "Key value is incorrect");
assertThat(events)
.isNotNull()
.isNotEmpty()
.first()
.extracting(LoggingEvent::getProperties, as(MAP))
.containsKey("hello")
.extractingByKey("hello", as(STRING))
.isEqualTo("world");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import java.nio.file.Paths;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
* Test configuration from Properties.
Expand All @@ -34,19 +33,10 @@ public class PropertiesRollingWithPropertiesTest {

private static final String TEST_DIR = "target/PropertiesRollingWithPropertiesTest";

@BeforeAll
static void beforeAll() {
System.setProperty("test.directory", TEST_DIR);
}

@AfterAll
static void afterAll() {
System.clearProperty("test.directory");
}

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@CleanUpDirectories(TEST_DIR)
@LoggerContextSource(value = "log4j1-rolling-properties.properties", v1config = true)
@LegacyLoggerContextSource("log4j1-rolling-properties.properties")
public void testProperties(final LoggerContext context) throws Exception {
final Logger logger = context.getLogger("test");
logger.debug("This is a test of the root logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.apache.log4j.config;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;
Expand All @@ -29,7 +31,7 @@
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
import org.junit.jupiter.api.Test;

Expand All @@ -40,7 +42,7 @@
public class RewriteAppenderTest {

@Test
@LoggerContextSource(value = "log4j1-rewrite.xml", v1config = true)
@LegacyLoggerContextSource("log4j1-rewrite.xml")
public void testRewrite(final LoggerContext context) {
final Logger logger = LogManager.getLogger("test");
ThreadContext.put("key1", "This is a test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import java.nio.file.Paths;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
* Test configuration from Properties.
Expand All @@ -34,19 +33,10 @@ public class XmlRollingWithPropertiesTest {

private static final String TEST_DIR = "target/XmlRollingWithPropertiesTest";

@BeforeAll
static void beforeAll() {
System.setProperty("test.directory", TEST_DIR);
}

@AfterAll
static void afterAll() {
System.clearProperty("test.directory");
}

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@CleanUpDirectories(TEST_DIR)
@LoggerContextSource(value = "log4j1-rolling-properties.xml", v1config = true)
@LegacyLoggerContextSource("log4j1-rolling-properties.xml")
public void testProperties(final LoggerContext context) {
// ${test.directory}/logs/etl.log
final Logger logger = context.getLogger("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,4 @@ public static void testPut() {
ThreadContext.put("testKey", "testValue");
assertEquals("testValue", ThreadContext.get("testKey"));
}

public static void reset() {
ThreadContext.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
*/
package org.apache.logging.log4j.test.junit;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.apache.logging.log4j.lang.NullMarked;
import org.apache.logging.log4j.lang.Nullable;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.platform.commons.PreconditionViolationException;

@NullMarked
public class ExtensionContextAnchor
implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {

public static Namespace LOG4J2_NAMESPACE = Namespace.create("org.apache.logging.log4j.junit");
private static final ThreadLocal<ExtensionContext> EXTENSION_CONTEXT = new InheritableThreadLocal<>();
private static final ThreadLocal<@Nullable ExtensionContext> EXTENSION_CONTEXT = new InheritableThreadLocal<>();

private static void bind(final ExtensionContext context) {
EXTENSION_CONTEXT.set(context);
Expand All @@ -39,27 +41,45 @@ private static void unbind(final ExtensionContext context) {
EXTENSION_CONTEXT.set(context.getParent().orElse(null));
}

public static ExtensionContext getContext() {
public static @Nullable ExtensionContext getContext() {
return EXTENSION_CONTEXT.get();
}

public static ExtensionContext getContext(final ExtensionContext context) {
public static @Nullable ExtensionContext getContext(final @Nullable ExtensionContext context) {
return context != null ? context : EXTENSION_CONTEXT.get();
}

public static <T> T getAttribute(final Object key, final Class<T> clazz, final ExtensionContext context) {
public static ExtensionContext getRequiredContext(final @Nullable ExtensionContext context) {
final ExtensionContext actualContext = getContext(context);
assertNotNull(actualContext, "missing ExtensionContext");
return actualContext.getStore(LOG4J2_NAMESPACE).get(key, clazz);
if (actualContext == null) {
throw new PreconditionViolationException("No ExtensionContext available");
}
return actualContext;
}

public static void setAttribute(final Object key, final Object value, final ExtensionContext context) {
final ExtensionContext actualContext = getContext(context);
assertNotNull(actualContext, "missing ExtensionContext");
actualContext.getStore(LOG4J2_NAMESPACE).put(key, value);
public static ExtensionContext.Store getRequiredStore(final @Nullable ExtensionContext context) {
return getRequiredContext(context).getStore(LOG4J2_NAMESPACE);
}

public static <T> @Nullable T getAttribute(
final Object key, final Class<T> clazz, final @Nullable ExtensionContext context) {
return getRequiredStore(context).get(key, clazz);
}

public static <T> T getRequiredAttribute(
final Object key, final Class<T> clazz, final @Nullable ExtensionContext context) {
final T attribute = getRequiredStore(context).get(key, clazz);
if (attribute == null) {
throw new PreconditionViolationException("Unable to find instance of " + clazz.getCanonicalName());
}
return attribute;
}

public static void setAttribute(final Object key, final Object value, final @Nullable ExtensionContext context) {
getRequiredStore(context).put(key, value);
}

public static void removeAttribute(final Object key, final ExtensionContext context) {
public static void removeAttribute(final Object key, final @Nullable ExtensionContext context) {
final ExtensionContext actualContext = getContext(context);
if (actualContext != null) {
actualContext.getStore(LOG4J2_NAMESPACE).remove(key);
Expand Down

This file was deleted.

Loading