Skip to content
Open
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 @@ -43,17 +43,25 @@ public class PropertiesConfigurationFactory extends ConfigurationFactory {
*/
protected static final String DEFAULT_PREFIX = "log4j";

@Override
protected boolean isActive() {
return PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE);
}

@Override
protected String[] getSupportedTypes() {
if (!PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
return null;
if (!isActive()) {
throw new IllegalStateException(getClass().getName() + " is inactive");
}
return new String[] {FILE_EXTENSION};
}

@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
if (!isActive()) {
throw new IllegalStateException(getClass().getName() + " is inactive");
}
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
return new PropertiesConfiguration(loggerContext, source, interval);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Log4j 1.x compatibility layer.
*/
@Export
@Version("2.20.1")
@Version("2.27.0")
@Open("org.apache.logging.log4j.core")
package org.apache.log4j.config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,25 @@ public class XmlConfigurationFactory extends ConfigurationFactory {
*/
protected static final String DEFAULT_PREFIX = "log4j";

@Override
protected boolean isActive() {
return PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE);
}

@Override
protected String[] getSupportedTypes() {
if (!PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
return null;
if (!isActive()) {
throw new IllegalStateException(getClass().getName() + " is inactive");
}
return new String[] {FILE_EXTENSION};
}

@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
if (!isActive()) {
throw new IllegalStateException(getClass().getName() + " is inactive");
}
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
return new XmlConfiguration(loggerContext, source, interval);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Log4j 1.x compatibility layer.
*/
@Export
@Version("2.25.3")
@Version("2.27.0")
package org.apache.log4j.xml;

import org.osgi.annotation.bundle.Export;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
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.test.junit.SetTestProperty;
import org.apache.logging.log4j.util.SortedArrayStringMap;
import org.junit.jupiter.api.Test;

/**
* Tests Jira3410.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class LoggerJira3410Test {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.apache.log4j.Logger;
import org.apache.log4j.bridge.AppenderAdapter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test configuration from XML.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
@UsingStatusListener
class AsyncAppenderTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.log4j.config;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.net.URL;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.WritesSystemProperty;

@WritesSystemProperty
class PropertiesConfigurationFactoryActivationTest {

@Test
void activationTracksTheCompatibilityProperty() throws Exception {
final String property = ConfigurationFactory.LOG4J1_EXPERIMENTAL;
final String previousValue = System.getProperty(property);
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
try {
System.setProperty(property, "false");
assertFalse(factory.isActive());
assertInactive(factory);

System.setProperty(property, "true");
assertTrue(factory.isActive());
assertArrayEquals(new String[] {".properties"}, factory.getSupportedTypes());
final URL resource = getClass().getResource("/config-1.2/log4j-console-SimpleLayout.properties");
final ConfigurationSource source = ConfigurationSource.fromUri(resource.toURI());
try (final LoggerContext context = new LoggerContext("test")) {
assertInstanceOf(PropertiesConfiguration.class, factory.getConfiguration(context, source));
}

System.setProperty(property, "false");
assertFalse(factory.isActive());
assertInactive(factory);
} finally {
if (previousValue == null) {
System.clearProperty(property);
} else {
System.setProperty(property, previousValue);
}
}
}

private static void assertInactive(final PropertiesConfigurationFactory factory) {
assertThrows(IllegalStateException.class, factory::getSupportedTypes);
assertThrows(
IllegalStateException.class, () -> factory.getConfiguration(null, ConfigurationSource.NULL_SOURCE));
assertThrows(
IllegalStateException.class,
() -> factory.getConfiguration(null, "test", URI.create("classpath:log4j.properties")));
assertThrows(
IllegalStateException.class,
() -> factory.getConfiguration(
null,
"test",
URI.create("classpath:log4j.properties"),
PropertiesConfigurationFactoryActivationTest.class.getClassLoader()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@
import org.apache.logging.log4j.core.filter.Filterable;
import org.apache.logging.log4j.core.filter.LevelRangeFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Test configuration from Properties.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class PropertiesConfigurationTest extends AbstractLog4j1ConfigurationTest {

private static final String TEST_KEY = "log4j.test.tmpdir";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
import org.apache.logging.log4j.core.config.ConfigurationListener;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Test reconfiguring with an XML configuration.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class PropertiesReconfigurationTest {

private class TestListener implements ConfigurationListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.net.Protocol;
import org.apache.logging.log4j.core.net.TcpSocketManager;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Tests configuring a Syslog appender.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class SocketAppenderConfigurationTest {

private SocketAppender check(final Protocol expected, final Configuration configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.net.AbstractSocketManager;
import org.apache.logging.log4j.core.net.Protocol;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -39,6 +40,7 @@
/**
* Tests configuring a Syslog appender.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
@UsingStatusListener
@WritesSystemProperty
class SyslogAppenderConfigurationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Test configuration from XML.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class XmlConfigurationTest extends AbstractLog4j1ConfigurationTest {

private static final String SUFFIX = ".xml";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationListener;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Test reconfiguring with an XML configuration.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class XmlReconfigurationTest {

private static final String CONFIG = "target/test-classes/log4j1-file.xml";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.log4j.xml;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.net.URL;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.WritesSystemProperty;

@WritesSystemProperty
class XmlConfigurationFactoryActivationTest {

@Test
void activationTracksTheCompatibilityProperty() throws Exception {
final String property = ConfigurationFactory.LOG4J1_EXPERIMENTAL;
final String previousValue = System.getProperty(property);
final XmlConfigurationFactory factory = new XmlConfigurationFactory();
try {
System.setProperty(property, "false");
assertFalse(factory.isActive());
assertInactive(factory);

System.setProperty(property, "true");
assertTrue(factory.isActive());
assertArrayEquals(new String[] {".xml"}, factory.getSupportedTypes());
final URL resource = getClass().getResource("/config-1.2/log4j-console-SimpleLayout.xml");
final ConfigurationSource source = ConfigurationSource.fromUri(resource.toURI());
try (final LoggerContext context = new LoggerContext("test")) {
assertInstanceOf(XmlConfiguration.class, factory.getConfiguration(context, source));
}

System.setProperty(property, "false");
assertFalse(factory.isActive());
assertInactive(factory);
} finally {
if (previousValue == null) {
System.clearProperty(property);
} else {
System.setProperty(property, previousValue);
}
}
}

private static void assertInactive(final XmlConfigurationFactory factory) {
assertThrows(IllegalStateException.class, factory::getSupportedTypes);
assertThrows(
IllegalStateException.class, () -> factory.getConfiguration(null, ConfigurationSource.NULL_SOURCE));
assertThrows(
IllegalStateException.class,
() -> factory.getConfiguration(null, "test", URI.create("classpath:log4j.xml")));
assertThrows(
IllegalStateException.class,
() -> factory.getConfiguration(
null,
"test",
URI.create("classpath:log4j.xml"),
XmlConfigurationFactoryActivationTest.class.getClassLoader()));
}
}
28 changes: 28 additions & 0 deletions log4j-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,34 @@
<groups>org.apache.logging.log4j.core.test.categories.AsyncLoggers</groups>
</configuration>
</execution>
<execution>
<id>test-without-jackson-databind</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.fasterxml.jackson.core:jackson-databind</classpathDependencyExclude>
</classpathDependencyExcludes>
<includes>
<include>**/JsonConfigurationFactoryMissingDependenciesIT.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>test-without-jackson-dataformat-yaml</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.fasterxml.jackson.dataformat:jackson-dataformat-yaml</classpathDependencyExclude>
</classpathDependencyExcludes>
<includes>
<include>**/YamlConfigurationFactoryMissingDependenciesIT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

Expand Down
Loading