From 24f3b2b1b7eda6631738462c56e61d73420592b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dziedziul?= Date: Mon, 29 Aug 2022 12:54:03 +0200 Subject: [PATCH] Make sure Hazelcast shutdown logs are available See gh-32184 --- .../HazelcastServerConfiguration.java | 17 +++++++++++ ...HazelcastAutoConfigurationServerTests.java | 28 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java index acfc128ea22f..6edd2d20d926 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java @@ -51,6 +51,7 @@ class HazelcastServerConfiguration { static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config"; + static final String HAZELCAST_LOGGING_TYPE = "hazelcast.logging.type"; private static HazelcastInstance getHazelcastInstance(Config config) { if (StringUtils.hasText(config.getInstanceName())) { @@ -123,6 +124,22 @@ HazelcastConfigCustomizer springManagedContextHazelcastConfigCustomizer(Applicat } + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(org.slf4j.Logger.class) + static class LoggingHazelcastConfigCustomizerConfiguration { + + @Bean + @Order(0) + HazelcastConfigCustomizer loggingHazelcastConfigCustomizer() { + return (config) -> { + if (!config.getProperties().containsKey(HAZELCAST_LOGGING_TYPE)) { + config.setProperty(HAZELCAST_LOGGING_TYPE, "slf4j"); + } + }; + } + + } + /** * {@link HazelcastConfigResourceCondition} that checks if the * {@code spring.hazelcast.config} configuration key is defined. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index 3909ba702989..798eb9845f70 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java @@ -206,6 +206,22 @@ void autoConfiguredContextCanOverrideManagementContextUsingCustomizer() { }); } + @Test + void configWithDefaultLoggingTypeSlf4j() { + this.contextRunner.run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); + assertThat(config.getProperty(HazelcastServerConfiguration.HAZELCAST_LOGGING_TYPE)).isEqualTo("slf4j"); + }); + } + + @Test + void configWithExplicitLoggingType() { + this.contextRunner.withUserConfiguration(HazelcastConfigWithJDKLogging.class).run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); + assertThat(config.getProperty(HazelcastServerConfiguration.HAZELCAST_LOGGING_TYPE)).isEqualTo("jdk"); + }); + } + private static Config createTestConfig(String instanceName) { Config config = new Config(instanceName); JoinConfig join = config.getNetworkConfig().getJoin(); @@ -236,6 +252,18 @@ Config anotherHazelcastConfig() { } + @Configuration(proxyBeanMethods = false) + static class HazelcastConfigWithJDKLogging { + + @Bean + Config anotherHazelcastConfig() { + Config config = new Config(); + config.setProperty(HazelcastServerConfiguration.HAZELCAST_LOGGING_TYPE, "jdk"); + return config; + } + + } + @SpringAware static class SpringAwareEntryProcessor implements EntryProcessor {