Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log properties loaded by Spring?

Tags:

spring

is it possible to simply log all the content of the properties file loaded by spring with <context:property-placeholder /> ?

Thanks

like image 221
La Chamelle Avatar asked Jan 13 '13 12:01

La Chamelle


2 Answers

You can set the log level of org.springframework.core.env.PropertySourcesPropertyResolver to "debug". Then, you will be able to see the value of the properties during resolving.

like image 180
Leo Yung Avatar answered Sep 22 '22 20:09

Leo Yung


You can do it this way:

<context:property-placeholder properties-ref="myProperties"/>

<bean id="myProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      .. locations
    </list>
  </property>
</bean>

and add a logging bean similar to one below (here annotation based and with slf4j api):

@Component
public class PropertiesLogger {
  private static Logger logger = LoggerFactory.getLogger(PropertiesLogger.class);

  @Resource("myProperties")
  private Properties props;

  @PostConstruct
  public void init() {
    for (Map.Entry<Object, Object> prop : props.entrySet()) {
      logger.debug("{}={}", prop.getKey(), prop.getValue());
    }
  }
}
like image 35
mrembisz Avatar answered Sep 20 '22 20:09

mrembisz