14

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

Thanks

La Chamelle
  • 2,927
  • 4
  • 36
  • 54
  • See also https://stackoverflow.com/q/24448947/685806 and https://stackoverflow.com/q/48212761/685806 (they are newer than this one so this one cannot be considered a duplicate, however they have better answers). – Pino Oct 28 '22 at 14:06

2 Answers2

14

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.

chrisjleu
  • 4,329
  • 7
  • 42
  • 55
Leo Yung
  • 149
  • 1
  • 4
  • Not true anymore: since v4.3.3 the value is not logged in order to avoid accidental logging of sensitive settings but you can override method `logKeyFound` to restore the old behavior. See https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/PropertySourcesPropertyResolver.html – Pino Oct 28 '22 at 08:42
2

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());
    }
  }
}
mrembisz
  • 12,722
  • 7
  • 36
  • 32