Skip to content

Commit

Permalink
Merge pull request #32742 from izeye
Browse files Browse the repository at this point in the history
* gh-32742:
  Polish Log4j2 changes

Closes gh-32742
  • Loading branch information
wilkinsona committed Nov 8, 2022
2 parents 3c51845 + ea4f226 commit 3c061dc
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.



Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String lookup(LogEvent event, String key) {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static Builder newBuilder() {
/**
* 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();

Expand All @@ -86,7 +86,7 @@ private Builder() {
* @return this
* @see Profiles#of(String...)
*/
public Builder setName(String name) {
Builder setName(String name) {
this.name = name;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,10 @@ void initializeAttachesEnvironmentToLoggerContext() {

@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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void lookupWhenNotFoundInEnvironmentReturnsNull() {
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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ private void initialize(String config) {

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {

private List<String> availableClasses = new ArrayList<>();
private final List<String> availableClasses = new ArrayList<>();

TestLog4J2LoggingSystem() {
super(TestLog4J2LoggingSystem.class.getClassLoader());
Expand Down

0 comments on commit 3c061dc

Please sign in to comment.