Skip to content

Commit

Permalink
Apply Logback system properties and not just common properties to con…
Browse files Browse the repository at this point in the history
…text

Closes gh-24894
  • Loading branch information
wilkinsona committed Feb 12, 2021
1 parent e763627 commit 392be57
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Expand Up @@ -148,7 +148,7 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
}
Environment environment = initializationContext.getEnvironment();
// Apply system properties directly in case the same JVM runs multiple apps
new LoggingSystemProperties(environment, context::putProperty).apply(logFile);
new LogbackLoggingSystemProperties(environment, context::putProperty).apply(logFile);
LogbackConfigurator configurator = debug ? new DebugLogbackConfigurator(context)
: new LogbackConfigurator(context);
new DefaultLogbackConfiguration(initializationContext, logFile).apply(configurator);
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.logging.logback;

import java.nio.charset.Charset;
import java.util.function.BiConsumer;

import ch.qos.logback.core.util.FileSize;

Expand Down Expand Up @@ -66,6 +67,16 @@ public LogbackLoggingSystemProperties(Environment environment) {
super(environment);
}

/**
* Create a new {@link LogbackLoggingSystemProperties} instance.
* @param environment the source environment
* @param setter setter used to apply the property
* @since 2.4.3
*/
public LogbackLoggingSystemProperties(Environment environment, BiConsumer<String, String> setter) {
super(environment, setter);
}

@Override
protected Charset getDefaultCharset() {
return Charset.defaultCharset();
Expand Down
Expand Up @@ -17,11 +17,15 @@
package org.springframework.boot.logging.logback;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Handler;
import java.util.logging.LogManager;
Expand Down Expand Up @@ -57,6 +61,7 @@
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -501,6 +506,30 @@ void initializeShouldSetSystemProperty() {
assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).endsWith("example.log");
}

@Test
void initializeShouldApplyLogbackSystemPropertiesToTheContext() {
this.environment.setProperty("logging.logback.rollingpolicy.file-name-pattern", "file-name-pattern");
this.environment.setProperty("logging.logback.rollingpolicy.clean-history-on-start", "true");
this.environment.setProperty("logging.logback.rollingpolicy.max-file-size", "10MB");
this.environment.setProperty("logging.logback.rollingpolicy.total-size-cap", "100MB");
this.environment.setProperty("logging.logback.rollingpolicy.max-history", "20");
this.loggingSystem.beforeInitialize();
initialize(this.initializationContext, null, null);
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
Map<String, String> properties = loggerContext.getCopyOfPropertyMap();
Set<String> expectedProperties = new HashSet<String>();
ReflectionUtils.doWithFields(LogbackLoggingSystemProperties.class,
(field) -> expectedProperties.add((String) field.get(null)), this::isPublicStaticFinal);
expectedProperties.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH"));
assertThat(properties).containsOnlyKeys(expectedProperties);
assertThat(properties).containsEntry("CONSOLE_LOG_CHARSET", Charset.defaultCharset().name());
}

private boolean isPublicStaticFinal(Field field) {
int modifiers = field.getModifiers();
return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);
}

@Test
void initializationIsOnlyPerformedOnceUntilCleanedUp() {
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
Expand Down

0 comments on commit 392be57

Please sign in to comment.