From a9ca63c3b0a9d5a834e5f59ac938b78acf2aeaac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20G=C5=82=C3=B3wka?= Date: Thu, 27 Oct 2022 20:53:05 +0200 Subject: [PATCH] BAEL-5691: Log properties in Spring Boot application (#12663) * BAEL-5691: Log properties in Spring Boot application * removed lombok * merged 2 streams into one * removed unnecessary exception handling --- .../spring-boot-properties-3/pom.xml | 4 ++ ...ontextRefreshedEventPropertiesPrinter.java | 55 +++++++++++++++++++ .../log/EnvironmentPropertiesPrinter.java | 29 ++++++++++ .../log/LogPropertiesDemoApplication.java | 13 +++++ .../src/main/resources/application.yml | 6 ++ 5 files changed, 107 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java create mode 100644 spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java diff --git a/spring-boot-modules/spring-boot-properties-3/pom.xml b/spring-boot-modules/spring-boot-properties-3/pom.xml index d72c410d9b..3de85c7175 100644 --- a/spring-boot-modules/spring-boot-properties-3/pom.xml +++ b/spring-boot-modules/spring-boot-properties-3/pom.xml @@ -19,6 +19,10 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.boot spring-boot-starter-test diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java new file mode 100644 index 0000000000..342371be45 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/AppContextRefreshedEventPropertiesPrinter.java @@ -0,0 +1,55 @@ +package com.baeldung.properties.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.stereotype.Component; + +import java.util.Collection; + +@Component +public class AppContextRefreshedEventPropertiesPrinter { + private static final Logger LOGGER = LoggerFactory.getLogger(AppContextRefreshedEventPropertiesPrinter.class); + + @EventListener + public void handleContextRefreshed(ContextRefreshedEvent event) { + printAllActiveProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment()); + + printAllApplicationProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment()); + } + + private void printAllActiveProperties(ConfigurableEnvironment env) { + + LOGGER.info("************************* ALL PROPERTIES(EVENT) ******************************"); + + env.getPropertySources() + .stream() + .filter(ps -> ps instanceof MapPropertySource) + .map(ps -> ((MapPropertySource) ps).getSource().keySet()) + .flatMap(Collection::stream) + .distinct() + .sorted() + .forEach(key -> LOGGER.info("{}={}", key, env.getProperty(key))); + + LOGGER.info("******************************************************************************"); + } + + private void printAllApplicationProperties(ConfigurableEnvironment env) { + + LOGGER.info("************************* APP PROPERTIES(EVENT) ******************************"); + + env.getPropertySources() + .stream() + .filter(ps -> ps instanceof MapPropertySource && ps.getName().contains("application.properties")) + .map(ps -> ((MapPropertySource) ps).getSource().keySet()) + .flatMap(Collection::stream) + .distinct() + .sorted() + .forEach(key -> LOGGER.info("{}={}", key, env.getProperty(key))); + + LOGGER.info("******************************************************************************"); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java new file mode 100644 index 0000000000..321593b31b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/EnvironmentPropertiesPrinter.java @@ -0,0 +1,29 @@ +package com.baeldung.properties.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class EnvironmentPropertiesPrinter { + private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentPropertiesPrinter.class); + private final Environment env; + + public EnvironmentPropertiesPrinter(Environment env) { + this.env = env; + } + + @PostConstruct + public void logApplicationProperties() { + LOGGER.info("************************* PROPERTIES(ENVIRONMENT) ******************************"); + + LOGGER.info("{}={}", "bael.property", env.getProperty("bael.property")); + LOGGER.info("{}={}", "app.name", env.getProperty("app.name")); + LOGGER.info("{}={}", "app.description", env.getProperty("app.description")); + + LOGGER.info("******************************************************************************"); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java new file mode 100644 index 0000000000..642a03edd6 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/properties/log/LogPropertiesDemoApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.properties.log; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LogPropertiesDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(LogPropertiesDemoApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml index 0f048ffa14..2e28da5ad3 100644 --- a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml @@ -55,3 +55,9 @@ application: uk: 14 fr: 42 us: 10 +--- +management: + endpoints: + web: + exposure: + include: env, health \ No newline at end of file