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
This commit is contained in:
parent
8e709cd717
commit
e0ff03f828
@ -19,6 +19,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -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("******************************************************************************");
|
||||
}
|
||||
}
|
@ -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("******************************************************************************");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -55,3 +55,9 @@ application:
|
||||
uk: 14
|
||||
fr: 42
|
||||
us: 10
|
||||
---
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: env, health
|
Loading…
x
Reference in New Issue
Block a user