Skip to content

Commit b642f1a

Browse files
committed
Split off YAML configuration into its own module
This splits `YamlConfiguration` into its own module and removes `jackson-dataformat-yaml` from `log4j-core`'s optional dependencies.
1 parent 01b6e2d commit b642f1a

28 files changed

+337
-274
lines changed

log4j-config-yaml/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>org.apache.logging.log4j</groupId>
22+
<artifactId>log4j</artifactId>
23+
<version>${revision}</version>
24+
<relativePath>../log4j-parent</relativePath>
25+
</parent>
26+
27+
<artifactId>log4j-config-yaml</artifactId>
28+
29+
<dependencies>
30+
31+
<dependency>
32+
<groupId>org.apache.logging.log4j</groupId>
33+
<artifactId>log4j-core</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>com.fasterxml.jackson.dataformat</groupId>
38+
<artifactId>jackson-dataformat-yaml</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.apache.logging.log4j</groupId>
43+
<artifactId>log4j-core-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<configuration>
55+
<annotationProcessorPaths combine.children="append">
56+
<path>
57+
<groupId>org.apache.logging.log4j</groupId>
58+
<artifactId>log4j-plugin-processor</artifactId>
59+
<version>${project.version}</version>
60+
</path>
61+
</annotationProcessorPaths>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/MultipleTriggeringPolicyTest.java renamed to log4j-config-yaml/src/test/java/org/apache/logging/log4j/config/yaml/MultipleTriggeringPolicyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.core.config;
17+
package org.apache.logging.log4j.config.yaml;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -24,6 +24,7 @@
2424
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
2525
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
2626
import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
27+
import org.apache.logging.log4j.core.config.Configuration;
2728
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2829
import org.junit.jupiter.api.Disabled;
2930
import org.junit.jupiter.api.Tag;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.config.yaml;
18+
19+
import java.io.IOException;
20+
import java.nio.file.Path;
21+
import org.apache.logging.log4j.core.LoggerContext;
22+
import org.apache.logging.log4j.core.config.AbstractConfigurationFactoryTest;
23+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
24+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
25+
import org.junit.jupiter.api.Test;
26+
27+
class YamlConfigurationFactoryTest extends AbstractConfigurationFactoryTest {
28+
29+
@TempLoggingDir
30+
private static Path loggingPath;
31+
32+
@Test
33+
@LoggerContextSource("YamlConfigurationFactoryTest.yaml")
34+
void yamlConfiguration(final LoggerContext context) throws IOException {
35+
checkConfiguration(context);
36+
final Path logFile = loggingPath.resolve("test-yaml.log");
37+
checkFileLogger(context, logFile);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.config;
18+
19+
import static org.apache.logging.log4j.util.Unbox.box;
20+
import static org.junit.jupiter.api.Assertions.assertAll;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.util.Iterator;
30+
import java.util.List;
31+
import java.util.Map;
32+
import org.apache.logging.log4j.Level;
33+
import org.apache.logging.log4j.core.Appender;
34+
import org.apache.logging.log4j.core.Filter;
35+
import org.apache.logging.log4j.core.Logger;
36+
import org.apache.logging.log4j.core.LoggerContext;
37+
import org.apache.logging.log4j.core.appender.ConsoleAppender;
38+
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
39+
import org.apache.logging.log4j.util.Strings;
40+
41+
/**
42+
* Common base class for configuration factory tests.
43+
*/
44+
public abstract class AbstractConfigurationFactoryTest {
45+
46+
private static final String LOGGER_NAME = "org.apache.logging.log4j.test1.Test";
47+
private static final String FILE_LOGGER_NAME = "org.apache.logging.log4j.test2.Test";
48+
private static final String APPENDER_NAME = "STDOUT";
49+
50+
/**
51+
* Runs various configuration checks on a configured LoggerContext that should match the equivalent configuration in
52+
* {@code log4j-test1.xml}.
53+
*/
54+
protected static void checkConfiguration(final LoggerContext context) {
55+
final Configuration configuration = context.getConfiguration();
56+
final Map<String, Appender> appenders = configuration.getAppenders();
57+
// these used to be separate tests
58+
assertAll(
59+
() -> assertNotNull(appenders),
60+
() -> assertEquals(3, appenders.size()),
61+
() -> assertNotNull(configuration.getLoggerContext()),
62+
() -> assertEquals(configuration.getRootLogger(), configuration.getLoggerConfig(Strings.EMPTY)),
63+
() -> assertThrows(NullPointerException.class, () -> configuration.getLoggerConfig(null)));
64+
65+
final Logger logger = context.getLogger(LOGGER_NAME);
66+
assertEquals(Level.DEBUG, logger.getLevel());
67+
68+
assertEquals(1, logger.filterCount());
69+
final Iterator<Filter> filterIterator = logger.getFilters();
70+
assertTrue(filterIterator.hasNext());
71+
assertTrue(filterIterator.next() instanceof ThreadContextMapFilter);
72+
73+
final Appender appender = appenders.get(APPENDER_NAME);
74+
assertTrue(appender instanceof ConsoleAppender);
75+
assertEquals(APPENDER_NAME, appender.getName());
76+
}
77+
78+
protected static void checkFileLogger(final LoggerContext context, final Path logFile) throws IOException {
79+
final long currentThreadId = Thread.currentThread().getId();
80+
final Logger logger = context.getLogger(FILE_LOGGER_NAME);
81+
logger.debug("Greetings from ConfigurationFactoryTest in thread#{}", box(currentThreadId));
82+
final List<String> lines = Files.readAllLines(logFile);
83+
assertEquals(1, lines.size());
84+
assertTrue(lines.get(0).endsWith(Long.toString(currentThreadId)));
85+
}
86+
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationFactoryTest.java

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,78 +16,19 @@
1616
*/
1717
package org.apache.logging.log4j.core.config;
1818

19-
import static org.apache.logging.log4j.util.Unbox.box;
20-
import static org.junit.jupiter.api.Assertions.assertAll;
21-
import static org.junit.jupiter.api.Assertions.assertEquals;
22-
import static org.junit.jupiter.api.Assertions.assertNotNull;
23-
import static org.junit.jupiter.api.Assertions.assertThrows;
24-
import static org.junit.jupiter.api.Assertions.assertTrue;
25-
2619
import java.io.IOException;
27-
import java.nio.file.Files;
2820
import java.nio.file.Path;
29-
import java.util.Iterator;
30-
import java.util.List;
31-
import java.util.Map;
32-
import org.apache.logging.log4j.Level;
33-
import org.apache.logging.log4j.core.Appender;
34-
import org.apache.logging.log4j.core.Filter;
35-
import org.apache.logging.log4j.core.Logger;
3621
import org.apache.logging.log4j.core.LoggerContext;
37-
import org.apache.logging.log4j.core.appender.ConsoleAppender;
38-
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
3922
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
4023
import org.apache.logging.log4j.test.junit.TempLoggingDir;
41-
import org.apache.logging.log4j.util.Strings;
4224
import org.junit.jupiter.api.Tag;
4325
import org.junit.jupiter.api.Test;
4426

45-
class ConfigurationFactoryTest {
46-
47-
static final String LOGGER_NAME = "org.apache.logging.log4j.test1.Test";
48-
static final String FILE_LOGGER_NAME = "org.apache.logging.log4j.test2.Test";
49-
static final String APPENDER_NAME = "STDOUT";
27+
class ConfigurationFactoryTest extends AbstractConfigurationFactoryTest {
5028

5129
@TempLoggingDir
5230
private static Path loggingPath;
5331

54-
/**
55-
* Runs various configuration checks on a configured LoggerContext that should match the equivalent configuration in
56-
* {@code log4j-test1.xml}.
57-
*/
58-
static void checkConfiguration(final LoggerContext context) {
59-
final Configuration configuration = context.getConfiguration();
60-
final Map<String, Appender> appenders = configuration.getAppenders();
61-
// these used to be separate tests
62-
assertAll(
63-
() -> assertNotNull(appenders),
64-
() -> assertEquals(3, appenders.size()),
65-
() -> assertNotNull(configuration.getLoggerContext()),
66-
() -> assertEquals(configuration.getRootLogger(), configuration.getLoggerConfig(Strings.EMPTY)),
67-
() -> assertThrows(NullPointerException.class, () -> configuration.getLoggerConfig(null)));
68-
69-
final Logger logger = context.getLogger(LOGGER_NAME);
70-
assertEquals(Level.DEBUG, logger.getLevel());
71-
72-
assertEquals(1, logger.filterCount());
73-
final Iterator<Filter> filterIterator = logger.getFilters();
74-
assertTrue(filterIterator.hasNext());
75-
assertTrue(filterIterator.next() instanceof ThreadContextMapFilter);
76-
77-
final Appender appender = appenders.get(APPENDER_NAME);
78-
assertTrue(appender instanceof ConsoleAppender);
79-
assertEquals(APPENDER_NAME, appender.getName());
80-
}
81-
82-
static void checkFileLogger(final LoggerContext context, final Path logFile) throws IOException {
83-
final long currentThreadId = Thread.currentThread().getId();
84-
final Logger logger = context.getLogger(FILE_LOGGER_NAME);
85-
logger.debug("Greetings from ConfigurationFactoryTest in thread#{}", box(currentThreadId));
86-
final List<String> lines = Files.readAllLines(logFile);
87-
assertEquals(1, lines.size());
88-
assertTrue(lines.get(0).endsWith(Long.toString(currentThreadId)));
89-
}
90-
9132
@Test
9233
@LoggerContextSource("log4j-test1.xml")
9334
void xml(final LoggerContext context) throws IOException {
@@ -113,15 +54,6 @@ void json(final LoggerContext context) throws IOException {
11354
checkFileLogger(context, logFile);
11455
}
11556

116-
@Test
117-
@Tag("yaml")
118-
@LoggerContextSource("log4j-test1.yaml")
119-
void yaml(final LoggerContext context) throws IOException {
120-
checkConfiguration(context);
121-
final Path logFile = loggingPath.resolve("test-yaml.log");
122-
checkFileLogger(context, logFile);
123-
}
124-
12557
@Test
12658
@LoggerContextSource("log4j-test1.properties")
12759
void properties(final LoggerContext context) throws IOException {

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/JiraLog4j2_2134Test.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525
import org.apache.logging.log4j.core.LoggerContext;
2626
import org.apache.logging.log4j.core.appender.FileAppender;
2727
import org.apache.logging.log4j.core.layout.PatternLayout;
28-
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
29-
import org.junit.jupiter.api.Tag;
3028
import org.junit.jupiter.api.Test;
3129

32-
@Tag("yaml")
33-
@LoggerContextSource("log4j2-2134.yaml")
3430
public class JiraLog4j2_2134Test {
3531

3632
@Test

0 commit comments

Comments
 (0)