Skip to content

Commit 1187b78

Browse files
committed
Allow Spring arbiter with no Spring environment present
Most Log4j configurations in Spring Boot are used twice: with no environment and with an environment present. Therefore the Spring Boot arbiter should not fail with no environment. Fixes #1783.
1 parent fbeb79a commit 1187b78

File tree

7 files changed

+120
-84
lines changed

7 files changed

+120
-84
lines changed

log4j-spring-boot/pom.xml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,39 @@
4747
<groupId>org.apache.logging.log4j</groupId>
4848
<artifactId>log4j-core</artifactId>
4949
</dependency>
50-
<dependency>
51-
<groupId>commons-logging</groupId>
52-
<artifactId>commons-logging</artifactId>
53-
</dependency>
5450
<dependency>
5551
<groupId>org.springframework.boot</groupId>
5652
<artifactId>spring-boot</artifactId>
5753
</dependency>
5854
<dependency>
5955
<groupId>org.springframework</groupId>
60-
<artifactId>spring-context</artifactId>
56+
<artifactId>spring-core</artifactId>
6157
</dependency>
6258
<dependency>
6359
<groupId>org.springframework</groupId>
64-
<artifactId>spring-context-support</artifactId>
60+
<artifactId>spring-context</artifactId>
6561
</dependency>
6662
<dependency>
6763
<groupId>org.apache.logging.log4j</groupId>
6864
<artifactId>log4j-api-test</artifactId>
6965
<scope>test</scope>
66+
<exclusions>
67+
<exclusion>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
</exclusion>
71+
</exclusions>
7072
</dependency>
7173
<dependency>
7274
<groupId>org.apache.logging.log4j</groupId>
7375
<artifactId>log4j-core-test</artifactId>
7476
<scope>test</scope>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>junit</groupId>
80+
<artifactId>junit</artifactId>
81+
</exclusion>
82+
</exclusions>
7583
</dependency>
7684
<dependency>
7785
<groupId>org.apache.logging.log4j</groupId>
@@ -84,8 +92,8 @@
8492
<scope>test</scope>
8593
</dependency>
8694
<dependency>
87-
<groupId>org.hamcrest</groupId>
88-
<artifactId>hamcrest</artifactId>
95+
<groupId>org.assertj</groupId>
96+
<artifactId>assertj-core</artifactId>
8997
<scope>test</scope>
9098
</dependency>
9199
<dependency>
@@ -103,16 +111,6 @@
103111
<artifactId>junit-pioneer</artifactId>
104112
<scope>test</scope>
105113
</dependency>
106-
<dependency>
107-
<groupId>org.junit.platform</groupId>
108-
<artifactId>junit-platform-commons</artifactId>
109-
<scope>test</scope>
110-
</dependency>
111-
<dependency>
112-
<groupId>org.junit.vintage</groupId>
113-
<artifactId>junit-vintage-engine</artifactId>
114-
<scope>test</scope>
115-
</dependency>
116114
<dependency>
117115
<groupId>org.springframework.boot</groupId>
118116
<artifactId>spring-boot-starter-test</artifactId>

log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringProfileArbiter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ public SpringProfileArbiter build() {
108108
if (loggerContext != null) {
109109
environment = (Environment) loggerContext.getObject(Log4j2SpringBootLoggingSystem.ENVIRONMENT_KEY);
110110
if (environment == null) {
111-
LOGGER.warn("Cannot create Arbiter, no Spring Environment provided");
112-
return null;
111+
LOGGER.debug("Creating Arbiter without a Spring Environment");
113112
}
114113

115114
return new SpringProfileArbiter(profileNames, environment);

log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootInitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testEnvironment() {
4242
final ListAppender app = context.getConfiguration().getAppender("Out");
4343
assertNotNull(app);
4444
assertEquals(1, app.getMessages().size());
45-
assertEquals("Started: log4j-spring-boot", app.getMessages().get(0));
45+
assertEquals("prod: Started: log4j-spring-boot", app.getMessages().get(0));
4646
}
4747

4848
@SpringBootApplication

log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/SpringLookupTest.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
import org.apache.logging.log4j.LogManager;
2020
import org.apache.logging.log4j.core.LoggerContext;
2121
import org.apache.logging.log4j.core.lookup.Interpolator;
22-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2323
import org.springframework.mock.env.MockEnvironment;
2424

25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertNotNull;
27-
import static org.junit.Assert.assertNull;
28-
25+
import static org.assertj.core.api.Assertions.assertThat;
2926
/**
3027
* Test SpringLookup.
3128
*/
@@ -42,22 +39,16 @@ public void testLookup() {
4239
final SpringLookup lookup = new SpringLookup();
4340
lookup.setLoggerContext(context);
4441
String result = lookup.lookup("profiles.active");
45-
assertNotNull("No active profiles", result);
46-
assertEquals("Incorrect active profile", "test", result);
42+
assertThat(result).as("Incorrect active profile").isEqualTo("test");
4743
result = lookup.lookup("profiles.active[0]");
48-
assertNotNull("No active profiles", result);
49-
assertEquals("Incorrect active profile", "test", result);
44+
assertThat(result).as("Incorrect active profile").isEqualTo("test");
5045
result = lookup.lookup("profiles.default");
51-
assertNotNull("No default profiles", result);
52-
assertEquals("Incorrect default profiles", "one,two", result);
46+
assertThat(result).as("Incorrect default profiles").isEqualTo("one,two");
5347
result = lookup.lookup("profiles.default[0]");
54-
assertNotNull("No default profiles", result);
55-
assertEquals("Incorrect default profiles", "one", result);
48+
assertThat(result).as("Incorrect default profiles").isEqualTo("one");
5649
result = lookup.lookup("profiles.default[2]");
57-
assertNull("Did not get index out of bounds", result);
5850
result = lookup.lookup("app.property");
59-
assertNotNull("Did not find property", result);
60-
assertEquals("Incorrect property value", "test", result);
51+
assertThat(result).as("Incorrect property value").isEqualTo("test");
6152
}
6253

6354
@Test
@@ -71,11 +62,9 @@ public void testSpringLookupWithDefaultInterpolator() {
7162
lookup.setConfiguration(context.getConfiguration());
7263
lookup.setLoggerContext(context);
7364
String result = lookup.lookup("spring:profiles.active");
74-
assertNotNull("No active profiles", result);
75-
assertEquals("Incorrect active profile", "test", result);
65+
assertThat(result).as("Incorrect active profile").isEqualTo("test");
7666

7767
result = lookup.lookup("spring:app.property");
78-
assertNotNull("Did not find property", result);
79-
assertEquals("Incorrect property value", "test", result);
68+
assertThat(result).as("Incorrect property value").isEqualTo("test");
8069
}
8170
}

log4j-spring-boot/src/test/java/org/apache/logging/log4j/spring/boot/SpringProfileTest.java

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,74 @@
1616
*/
1717
package org.apache.logging.log4j.spring.boot;
1818

19-
import java.io.File;
2019

21-
import org.apache.logging.log4j.LogManager;
2220
import org.apache.logging.log4j.core.Appender;
21+
import org.apache.logging.log4j.core.Layout;
2322
import org.apache.logging.log4j.core.LoggerContext;
2423
import org.apache.logging.log4j.core.appender.ConsoleAppender;
24+
import org.apache.logging.log4j.core.layout.PatternLayout;
2525
import org.apache.logging.log4j.core.test.appender.ListAppender;
26-
import org.junit.BeforeClass;
27-
import org.junit.Test;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
28+
import org.junit.jupiter.api.Test;
29+
import org.springframework.core.env.Environment;
2830
import org.springframework.mock.env.MockEnvironment;
2931

30-
import static org.junit.jupiter.api.Assertions.assertNotNull;
31-
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
import static org.assertj.core.api.Assertions.assertThat;
3233

3334
/**
3435
* Tests basic condition processing.
3536
*/
37+
@UsingStatusListener
3638
public class SpringProfileTest {
3739

38-
static final String CONFIG = "target/test-classes/log4j2-springProfile.xml";
39-
static LoggerContext loggerContext;
40-
static MockEnvironment env;
40+
private static final String CONFIG = "log4j2-springProfile.xml";
41+
private static final MockEnvironment env = new MockEnvironment();
42+
private static final String[] DEV_PROFILES = {"dev", "staging"};
4143

42-
@BeforeClass
43-
public static void before() {
44-
loggerContext = (LoggerContext) LogManager.getContext(false);
45-
env = new MockEnvironment();
44+
private void registerSpringEnvironment(final LoggerContext loggerContext, final Environment env) {
4645
loggerContext.putObject(Log4j2SpringBootLoggingSystem.ENVIRONMENT_KEY, env);
4746
}
4847

48+
private void clearSpringEnvironment(final LoggerContext loggerContext) {
49+
loggerContext.removeObject(Log4j2SpringBootLoggingSystem.ENVIRONMENT_KEY);
50+
}
4951

50-
@Test
51-
public void prodTest() {
52-
env.setActiveProfiles("prod");
53-
loggerContext.setConfigLocation(new File(CONFIG).toURI());
54-
assertNotNull(loggerContext);
52+
private void testAppenderOut(final LoggerContext loggerContext, final Class<? extends Appender> clazz, final String patternPrefix) {
5553
final Appender app = loggerContext.getConfiguration().getAppender("Out");
56-
assertNotNull(app);
57-
assertTrue(app instanceof ListAppender);
54+
assertThat(app).isInstanceOf(clazz);
55+
final Layout<?> layout = app.getLayout();
56+
assertThat(layout).isInstanceOf(PatternLayout.class);
57+
assertThat(((PatternLayout) layout).getConversionPattern()).startsWith(patternPrefix);
5858
}
5959

6060
@Test
61-
public void devTest() {
62-
env.setActiveProfiles("dev");
63-
loggerContext.setConfigLocation(new File(CONFIG).toURI());
64-
assertNotNull(loggerContext);
65-
final Appender app = loggerContext.getConfiguration().getAppender("Out");
66-
assertNotNull(app);
67-
assertTrue(app instanceof ConsoleAppender);
61+
@LoggerContextSource(CONFIG)
62+
void prodTest(final LoggerContext loggerContext) {
63+
testAppenderOut(loggerContext, ListAppender.class, "none:");
64+
registerSpringEnvironment(loggerContext, env);
65+
try {
66+
env.setActiveProfiles("prod");
67+
loggerContext.reconfigure();
68+
testAppenderOut(loggerContext, ListAppender.class, "prod:");
69+
} finally {
70+
clearSpringEnvironment(loggerContext);
71+
}
72+
}
73+
74+
@Test
75+
@LoggerContextSource(CONFIG)
76+
void devTest(final LoggerContext loggerContext) {
77+
testAppenderOut(loggerContext, ListAppender.class, "none:");
78+
registerSpringEnvironment(loggerContext, env);
79+
try {
80+
for (final String profile : DEV_PROFILES) {
81+
env.setActiveProfiles(profile);
82+
loggerContext.reconfigure();
83+
testAppenderOut(loggerContext, ConsoleAppender.class, "dev:");
84+
}
85+
} finally {
86+
clearSpringEnvironment(loggerContext);
87+
}
6888
}
6989
}

log4j-spring-boot/src/test/resources/log4j2-springProfile.xml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@
1515
~ See the License for the specific language governing permissions and
1616
~ limitations under the License.
1717
-->
18-
<Configuration name="ConfigTest" status="ERROR" monitorInterval="5">
18+
<Configuration name="ConfigTest" status="OFF">
1919
<Appenders>
20-
21-
<SpringProfile name="dev | staging">
22-
<Console name="Out">
23-
<PatternLayout pattern="%m%n"/>
24-
</Console>
25-
</SpringProfile>
26-
<SpringProfile name="prod">
27-
<List name="Out">
28-
<PatternLayout pattern="%m"/>
29-
</List>
30-
</SpringProfile>
31-
20+
<Select>
21+
<SpringProfile name="dev | staging">
22+
<Console name="Out">
23+
<PatternLayout pattern="dev: %m"/>
24+
</Console>
25+
</SpringProfile>
26+
<SpringProfile name="prod">
27+
<List name="Out">
28+
<PatternLayout pattern="prod: %m"/>
29+
</List>
30+
</SpringProfile>
31+
<DefaultArbiter>
32+
<List name="Out">
33+
<PatternLayout pattern="none: %m"/>
34+
</List>
35+
</DefaultArbiter>
36+
</Select>
3237
</Appenders>
3338
<Loggers>
34-
<Logger name="org.apache.logging.log4j.core.springtest" level="trace" additivity="false">
35-
<AppenderRef ref="Out"/>
36-
</Logger>
39+
<Logger name="org.apache.logging.log4j.core.springtest" level="trace"/>
3740
<Root level="error">
3841
<AppenderRef ref="Out"/>
3942
</Root>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
21+
type="fixed">
22+
<issue id="1783" link="https://github.com/apache/logging-log4j2/issues/1783"/>
23+
<author id="github:ppkarwasz"/>
24+
<description format="asciidoc">
25+
Allow using Spring Arbiter without a Spring environment.
26+
</description>
27+
</entry>

0 commit comments

Comments
 (0)