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..86bebd65851f 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 @@ -52,6 +52,8 @@ 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())) { return Hazelcast.getOrCreateHazelcastInstance(config); @@ -123,6 +125,22 @@ HazelcastConfigCustomizer springManagedContextHazelcastConfigCustomizer(Applicat } + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(org.slf4j.Logger.class) + static class HazelcastLoggingConfigCustomizerConfiguration { + + @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..5833ed0a2b7a 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 autoConfiguredConfigSetsHazelcastLoggingToSlf4j() { + this.contextRunner.run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); + assertThat(config.getProperty(HazelcastServerConfiguration.HAZELCAST_LOGGING_TYPE)).isEqualTo("slf4j"); + }); + } + + @Test + void autoConfiguredConfigCanOverrideHazelcastLogging() { + 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 {