Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Spring to print out what spring profiles are active?

Tags:

java

spring

I am using spring 3.1 profiles and want would like Spring to print out on startup what profiles are active. For example are the first few lines of the output in the log file.

02:59:43,451 INFO  [ContextLoader] Root WebApplicationContext: initialization started
02:59:43,544 INFO  [XmlWebApplicationContext] Refreshing Root WebApplicationContext: startup date [Sun Dec 30 02:59:43 EST 2012]; root of context hierarchy
02:59:43,610 INFO  [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring.xml]
02:59:43,835 INFO  [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring-security.xml]
02:59:43,971 INFO  [SpringSecurityCoreVersion] You are running with Spring Security Core 3.1.3.RELEASE
02:59:43,971 INFO  [SecurityNamespaceHandler] Spring Security 'config' module version is 3.1.3.RELEASE

What I want to see from spring is something that prints out the version of spring in use along with which profiles are currently active.

How can I get spring to printout it's version and what profiles are active?

like image 963
ams Avatar asked Dec 30 '12 08:12

ams


People also ask

Where do I set spring profiles active?

Spring profiles can also be activated via Maven profiles, by specifying the spring. profiles. active configuration property. This command will package the application for prod profile.

How do I keep my spring boot profiles different?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.


2 Answers

Implement EnvironmentAware interface

For example

class MyEnvironmentAware implements EnvironmentAware{
    private static Environment env = null;

    @Override
    public void setEnvironment(Environment environment) {
            env = environment;
            //log the stuff you want here
     }
}

Mark this class as Spring bean

or

just inject Environment in one of your eagerly loading bean and print the detail you need from it

like

@Autowired
Environment env;

in your eagerly loading bean, and print it

like image 114
jmj Avatar answered Sep 24 '22 10:09

jmj


You could get it by configuring log4j, as the Environment object logs the activating of the profile at DEBUG level.

log4j.logger.org.springframework.core.env=DEBUG, A1

If A1 is your log appender. Unfortunately, there is a lot of other stuff coming at DEBUG level, so it is not really nice, but you get the active profile without source modification.

The config logs in a standalone Swing app on startup:

58   [main] DEBUG org.springframework.core.env.StandardEnvironment  - Activating profile 'production'

Note, this is fragile, as it relies on debug level logs, which can change rapidly in every commit to spring.

like image 31
burna Avatar answered Sep 20 '22 10:09

burna