Browse Source

Polish Log4j2 changes

See gh-32742
pull/33080/head
Johnny Lim 3 years ago committed by Andy Wilkinson
parent
commit
ea4f22684d
  1. 8
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc
  2. 2
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java
  3. 4
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringProfileArbiter.java
  4. 3
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java
  5. 2
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java
  6. 4
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringProfileArbiterTests.java
  7. 2
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java

8
spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc

@ -487,7 +487,7 @@ NOTE: Because the standard `log4j2.xml` configuration file is loaded too early, @@ -487,7 +487,7 @@ NOTE: Because the standard `log4j2.xml` configuration file is loaded too early,
You need to either use `log4j2-spring.xml` or define a configprop:logging.config[] property.
NOTE: The extensions supersede the https://logging.apache.org/log4j/2.x/log4j-spring-boot/index.html[Spring Boot support] provided by Log4J.
You should make sure not include the `org.apache.logging.log4j:log4j-spring-boot` module in your build.
You should make sure not to include the `org.apache.logging.log4j:log4j-spring-boot` module in your build.
@ -536,12 +536,12 @@ NOTE: The lookup key should be specified in kebab case (such as `my.property-nam @@ -536,12 +536,12 @@ NOTE: The lookup key should be specified in kebab case (such as `my.property-nam
[[features.logging.log4j2-extensions.environment-peroperty-source]]
[[features.logging.log4j2-extensions.environment-property-source]]
==== Log4j2 System Properties
Log4j2 supports a number of https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties[System Properties] that can be used configure various items.
Log4j2 supports a number of https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties[System Properties] that can be used to configure various items.
For example, the `log4j2.skipJansi` system property can be used to configure if the `ConsoleAppender` will try to use a https://github.com/fusesource/jansi[Jansi] output stream on Windows.
All system properties that are loaded after the Log4J initialization can be obtained from the Spring `Environment`.
All system properties that are loaded after the Log4j2 initialization can be obtained from the Spring `Environment`.
For example, you could add `log4j2.skipJansi=false` to your `application.properties` file to have the `ConsoleAppender` use a Jansi on Windows.
NOTE: The Spring `Environment` is only considered when system properties and OS environment variables do not contain the value being loaded.

2
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java

@ -44,7 +44,7 @@ class SpringEnvironmentLookup implements LoggerContextAware, StrLookup { @@ -44,7 +44,7 @@ class SpringEnvironmentLookup implements LoggerContextAware, StrLookup {
@Override
public String lookup(String key) {
Assert.state(this.environment != null, "Unable to obtain Spring Environment from LoggerContext");
return (this.environment != null) ? this.environment.getProperty(key) : null;
return this.environment.getProperty(key);
}
@Override

4
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringProfileArbiter.java

@ -64,7 +64,7 @@ final class SpringProfileArbiter implements Arbiter { @@ -64,7 +64,7 @@ final class SpringProfileArbiter implements Arbiter {
/**
* Standard Builder to create the Arbiter.
*/
public static final class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
static final class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
private static final Logger statusLogger = StatusLogger.getLogger();
@ -86,7 +86,7 @@ final class SpringProfileArbiter implements Arbiter { @@ -86,7 +86,7 @@ final class SpringProfileArbiter implements Arbiter {
* @return this
* @see Profiles#of(String...)
*/
public Builder setName(String name) {
Builder setName(String name) {
this.name = name;
return this;
}

3
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

@ -463,11 +463,10 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @@ -463,11 +463,10 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test
void initializeAddsSpringEnvironmentPropertySource() {
PropertiesUtil properties = PropertiesUtil.getProperties();
this.environment.setProperty("spring", "boot");
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
properties = PropertiesUtil.getProperties();
PropertiesUtil properties = PropertiesUtil.getProperties();
assertThat(properties.getStringProperty("spring")).isEqualTo("boot");
}

2
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java

@ -68,7 +68,7 @@ class SpringEnvironmentLookupTests { @@ -68,7 +68,7 @@ class SpringEnvironmentLookupTests {
void lookupWhenNoSpringEnvironmentThrowsException() {
this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
Interpolator lookup = createLookup(this.loggerContext);
assertThatIllegalStateException().isThrownBy(() -> assertThat(lookup.lookup("spring:test")).isEqualTo("test"))
assertThatIllegalStateException().isThrownBy(() -> lookup.lookup("spring:test"))
.withMessage("Unable to obtain Spring Environment from LoggerContext");
}

4
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringProfileArbiterTests.java

@ -154,9 +154,7 @@ class SpringProfileArbiterTests { @@ -154,9 +154,7 @@ class SpringProfileArbiterTests {
private String getPackageResource(String fileName) {
String path = ClassUtils.getPackageName(getClass());
path = path.replace('.', '/');
path = path + "/" + fileName;
return "src/test/resources/" + path;
return "src/test/resources/" + path.replace('.', '/') + "/" + fileName;
}
}

2
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java

@ -25,7 +25,7 @@ import org.apache.logging.log4j.core.config.Configuration; @@ -25,7 +25,7 @@ import org.apache.logging.log4j.core.config.Configuration;
class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
private List<String> availableClasses = new ArrayList<>();
private final List<String> availableClasses = new ArrayList<>();
TestLog4J2LoggingSystem() {
super(TestLog4J2LoggingSystem.class.getClassLoader());

Loading…
Cancel
Save