Merge branch '2.4.x'

Closes gh-25259
pull/25264/head
Andy Wilkinson 4 years ago
commit 0a4c26532d

@ -148,7 +148,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
}
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);

@ -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;
@ -66,6 +67,16 @@ public class LogbackLoggingSystemProperties extends LoggingSystemProperties {
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();

@ -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;
@ -57,6 +61,7 @@ import org.springframework.core.convert.support.ConfigurableConversionService;
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;
@ -501,6 +506,30 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
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();

Loading…
Cancel
Save