Skip to content

Commit

Permalink
startup logging and profile cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenp committed Apr 25, 2016
1 parent 31b9968 commit 4d37e27
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class Profiles {
// these 3 profiles are modeling the environments - deployed is active for any deployment, dev and production for these specific environments
public static final String DEPLOYED = "deployed";
public static final String DEV = "dev";
public static final String LOCAL = "local";

// common
public static final String TEST = "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class MyApplicationContextInitializer implements ApplicationContextInitia
private final Logger logger = LoggerFactory.getLogger(MyApplicationContextInitializer.class);

private static final String ENV_TARGET = "envTarget";
private static final String PERSISTENCE_TARGET = "persistenceTarget";

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

final String activeProfiles = environment.getProperty("spring.profiles.active");
logger.info("The active profiles are: {}", activeProfiles);

environment.setActiveProfiles(activeProfiles.split(","));
} catch (final IOException ioEx) {
if (envTarget != null) {
logger.warn("Didn't find env-" + envTarget + ".properties in classpath so not loading it in the AppContextInitialized", ioEx);
}
}

final String persistenceTarget = environment.getProperty(PERSISTENCE_TARGET);
if (persistenceTarget == null) {
logger.info("Didn't find a value for variable: {}", PERSISTENCE_TARGET);
} else {
logger.info("value for variable: {} is: {}", PERSISTENCE_TARGET, persistenceTarget);
}
}

/**
* @param environment
* @return The env target variable.
*/
private String getEnvTarget(final ConfigurableEnvironment environment) {
String envTarget;

final String targetOverride = getTargetFromOverride();
if (targetOverride == null) {
envTarget = environment.getProperty(ENV_TARGET);
} else {
envTarget = targetOverride;
}
if (envTarget == null) {
String target = environment.getProperty(ENV_TARGET);
if (target == null) {
logger.warn("Didn't find a value for {} in the current Environment!", ENV_TARGET);
}

if (envTarget == null) {
if (target == null) {
logger.info("Didn't find a value for {} in the current Environment!, using the default `dev`", ENV_TARGET);
envTarget = "dev";
}

return Preconditions.checkNotNull(envTarget);
}

/**
* 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
* the active Spring profile without defining system wide variables on the OS that runs staging
*/
private final String getTargetFromOverride() {
try {
final ResourcePropertySource overrideProperties = new ResourcePropertySource("file:///opt/override/overrides.properties");
return (String) overrideProperties.getProperty(ENV_TARGET);
} catch (final IOException e) {
logger.debug("The file overrides.properties is not accessible. No property overridden by external properties");
target = "dev";
}

return null;
return Preconditions.checkNotNull(target);
}

}
4 changes: 3 additions & 1 deletion um-webapp/src/main/java/org/baeldung/um/run/UmApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ protected SpringApplicationBuilder configure(final SpringApplicationBuilder appl
}

public static void main(final String... args) {
SpringApplication.run(CONFIGS, args);
final SpringApplication springApplication = new SpringApplication(CONFIGS);
springApplication.addInitializers(new MyApplicationContextInitializer());
springApplication.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.baeldung.um.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

@Component
public class StartupLoggingComponent implements InitializingBean {
private final Logger logger = LoggerFactory.getLogger(getClass());

private static final String ENV_TARGET_KEY = "envTarget";
private static final String PERSISTENCE_TARGET_KEY = "persistenceTarget";
private static final String ACTIVE_SPRING_PROFILE_KEY = "spring.profiles.active";
private static final String PERSISTENCE_HOST_KEY = "jdbc.url";

@Autowired
private Environment env;

public StartupLoggingComponent() {
super();
}

//

@Override
public void afterPropertiesSet() {
logger.info("============================================================================");
try {
logEnvTarget(env);
logPersistenceTarget(env);

logActiveSpringProfile(env);
logPersistenceData(env);
} catch (final Exception ex) {
logger.warn("There was a problem logging data on startup", ex);
}

logger.info("============================================================================");
}

// UTIL

private void logEnvTarget(final Environment environment) {
final String envTarget = getValueOfProperty(environment, ENV_TARGET_KEY, "dev", Lists.newArrayList("dev"));
logger.info("{} = {}", ENV_TARGET_KEY, envTarget);
}

private void logPersistenceTarget(final Environment environment) {
final String envTarget = getValueOfProperty(environment, PERSISTENCE_TARGET_KEY, "h2", Lists.newArrayList("h2", "mysql", "cargo"));
logger.info("{} = {}", PERSISTENCE_TARGET_KEY, envTarget);
}

private void logActiveSpringProfile(final Environment environment) {
final String activeSpringProfile = getValueOfProperty(environment, ACTIVE_SPRING_PROFILE_KEY, "none", null);
logger.info("{} = {}", ACTIVE_SPRING_PROFILE_KEY, activeSpringProfile);
}

private void logPersistenceData(final Environment environment) {
final String persistenceHost = getValueOfProperty(environment, PERSISTENCE_HOST_KEY, "not-found", null);
logger.info("{} = {}", PERSISTENCE_HOST_KEY, persistenceHost);
}

//

private final String getValueOfProperty(final Environment environment, final String propertyKey, final String propertyDefaultValue, final List<String> acceptablePropertyValues) {
String propValue = environment.getProperty(propertyKey);
if (propValue == null) {
propValue = propertyDefaultValue;
logger.info("The {} doesn't have an explicit value; default value is = {}", propertyKey, propertyDefaultValue);
}

if (acceptablePropertyValues != null) {
if (!acceptablePropertyValues.contains(propValue)) {
logger.warn("The property = {} has an invalid value = {}", propertyKey, propValue);
}
}

if (propValue == null) {
logger.warn("The property = {} is null", propertyKey);
}

return propValue;
}

}

0 comments on commit 4d37e27

Please sign in to comment.