Skip to content

Commit 4d37e27

Browse files
committed
startup logging and profile cleanup
1 parent 31b9968 commit 4d37e27

4 files changed

Lines changed: 101 additions & 38 deletions

File tree

‎common/src/main/java/org/baeldung/common/spring/util/Profiles.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public final class Profiles {
55
// these 3 profiles are modeling the environments - deployed is active for any deployment, dev and production for these specific environments
66
public static final String DEPLOYED = "deployed";
77
public static final String DEV = "dev";
8+
public static final String LOCAL = "local";
89

910
// common
1011
public static final String TEST = "test";

‎um-webapp/src/main/java/org/baeldung/um/persistence/setup/MyApplicationContextInitializer.java‎

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class MyApplicationContextInitializer implements ApplicationContextInitia
1515
private final Logger logger = LoggerFactory.getLogger(MyApplicationContextInitializer.class);
1616

1717
private static final String ENV_TARGET = "envTarget";
18-
private static final String PERSISTENCE_TARGET = "persistenceTarget";
1918

2019
public MyApplicationContextInitializer() {
2120
super();
@@ -35,61 +34,30 @@ public void initialize(final ConfigurableApplicationContext applicationContext)
3534
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env-" + envTarget + ".properties"));
3635

3736
final String activeProfiles = environment.getProperty("spring.profiles.active");
38-
logger.info("The active profiles are: {}", activeProfiles);
39-
4037
environment.setActiveProfiles(activeProfiles.split(","));
4138
} catch (final IOException ioEx) {
4239
if (envTarget != null) {
4340
logger.warn("Didn't find env-" + envTarget + ".properties in classpath so not loading it in the AppContextInitialized", ioEx);
4441
}
4542
}
46-
47-
final String persistenceTarget = environment.getProperty(PERSISTENCE_TARGET);
48-
if (persistenceTarget == null) {
49-
logger.info("Didn't find a value for variable: {}", PERSISTENCE_TARGET);
50-
} else {
51-
logger.info("value for variable: {} is: {}", PERSISTENCE_TARGET, persistenceTarget);
52-
}
5343
}
5444

5545
/**
5646
* @param environment
5747
* @return The env target variable.
5848
*/
5949
private String getEnvTarget(final ConfigurableEnvironment environment) {
60-
String envTarget;
61-
62-
final String targetOverride = getTargetFromOverride();
63-
if (targetOverride == null) {
64-
envTarget = environment.getProperty(ENV_TARGET);
65-
} else {
66-
envTarget = targetOverride;
67-
}
68-
if (envTarget == null) {
50+
String target = environment.getProperty(ENV_TARGET);
51+
if (target == null) {
6952
logger.warn("Didn't find a value for {} in the current Environment!", ENV_TARGET);
7053
}
7154

72-
if (envTarget == null) {
55+
if (target == null) {
7356
logger.info("Didn't find a value for {} in the current Environment!, using the default `dev`", ENV_TARGET);
74-
envTarget = "dev";
75-
}
76-
77-
return Preconditions.checkNotNull(envTarget);
78-
}
79-
80-
/**
81-
* This enables overriding the env-${envTarget}.properties location, which is by default resolved internally from the classpath. The entire purpose of overriding this specific properties file is to be able to control
82-
* the active Spring profile without defining system wide variables on the OS that runs staging
83-
*/
84-
private final String getTargetFromOverride() {
85-
try {
86-
final ResourcePropertySource overrideProperties = new ResourcePropertySource("file:///opt/override/overrides.properties");
87-
return (String) overrideProperties.getProperty(ENV_TARGET);
88-
} catch (final IOException e) {
89-
logger.debug("The file overrides.properties is not accessible. No property overridden by external properties");
57+
target = "dev";
9058
}
9159

92-
return null;
60+
return Preconditions.checkNotNull(target);
9361
}
9462

9563
}

‎um-webapp/src/main/java/org/baeldung/um/run/UmApp.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ protected SpringApplicationBuilder configure(final SpringApplicationBuilder appl
4242
}
4343

4444
public static void main(final String... args) {
45-
SpringApplication.run(CONFIGS, args);
45+
final SpringApplication springApplication = new SpringApplication(CONFIGS);
46+
springApplication.addInitializers(new MyApplicationContextInitializer());
47+
springApplication.run(args);
4648
}
4749

4850
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.baeldung.um.service;
2+
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.InitializingBean;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.core.env.Environment;
10+
import org.springframework.stereotype.Component;
11+
12+
import com.google.common.collect.Lists;
13+
14+
@Component
15+
public class StartupLoggingComponent implements InitializingBean {
16+
private final Logger logger = LoggerFactory.getLogger(getClass());
17+
18+
private static final String ENV_TARGET_KEY = "envTarget";
19+
private static final String PERSISTENCE_TARGET_KEY = "persistenceTarget";
20+
private static final String ACTIVE_SPRING_PROFILE_KEY = "spring.profiles.active";
21+
private static final String PERSISTENCE_HOST_KEY = "jdbc.url";
22+
23+
@Autowired
24+
private Environment env;
25+
26+
public StartupLoggingComponent() {
27+
super();
28+
}
29+
30+
//
31+
32+
@Override
33+
public void afterPropertiesSet() {
34+
logger.info("============================================================================");
35+
try {
36+
logEnvTarget(env);
37+
logPersistenceTarget(env);
38+
39+
logActiveSpringProfile(env);
40+
logPersistenceData(env);
41+
} catch (final Exception ex) {
42+
logger.warn("There was a problem logging data on startup", ex);
43+
}
44+
45+
logger.info("============================================================================");
46+
}
47+
48+
// UTIL
49+
50+
private void logEnvTarget(final Environment environment) {
51+
final String envTarget = getValueOfProperty(environment, ENV_TARGET_KEY, "dev", Lists.newArrayList("dev"));
52+
logger.info("{} = {}", ENV_TARGET_KEY, envTarget);
53+
}
54+
55+
private void logPersistenceTarget(final Environment environment) {
56+
final String envTarget = getValueOfProperty(environment, PERSISTENCE_TARGET_KEY, "h2", Lists.newArrayList("h2", "mysql", "cargo"));
57+
logger.info("{} = {}", PERSISTENCE_TARGET_KEY, envTarget);
58+
}
59+
60+
private void logActiveSpringProfile(final Environment environment) {
61+
final String activeSpringProfile = getValueOfProperty(environment, ACTIVE_SPRING_PROFILE_KEY, "none", null);
62+
logger.info("{} = {}", ACTIVE_SPRING_PROFILE_KEY, activeSpringProfile);
63+
}
64+
65+
private void logPersistenceData(final Environment environment) {
66+
final String persistenceHost = getValueOfProperty(environment, PERSISTENCE_HOST_KEY, "not-found", null);
67+
logger.info("{} = {}", PERSISTENCE_HOST_KEY, persistenceHost);
68+
}
69+
70+
//
71+
72+
private final String getValueOfProperty(final Environment environment, final String propertyKey, final String propertyDefaultValue, final List<String> acceptablePropertyValues) {
73+
String propValue = environment.getProperty(propertyKey);
74+
if (propValue == null) {
75+
propValue = propertyDefaultValue;
76+
logger.info("The {} doesn't have an explicit value; default value is = {}", propertyKey, propertyDefaultValue);
77+
}
78+
79+
if (acceptablePropertyValues != null) {
80+
if (!acceptablePropertyValues.contains(propValue)) {
81+
logger.warn("The property = {} has an invalid value = {}", propertyKey, propValue);
82+
}
83+
}
84+
85+
if (propValue == null) {
86+
logger.warn("The property = {} is null", propertyKey);
87+
}
88+
89+
return propValue;
90+
}
91+
92+
}

0 commit comments

Comments
 (0)