views:

106

answers:

1

Hi!

I am using http://java.sun.com/jsp/jstl/fmt tag library for outputting messages from Message Resource Bundle which is set up in Spring configuration. Also message resolving can go in Controllers.

I would like to log situations when the keys are not found in resource bundle file.

Does any body know how to do it?

+1  A: 

Spring uses a MessageSource to retrieve the messages so one solution could be to write your own MessageSource that encapsulate the Spring one and overwrite the getMessage methods to insert your logging.

Something like the following (not tested):

 <bean id="messageSource" class="com.acme.LoggingMessageSource">
   <property name="pathtru">
     <bean class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basenames">
         <list>
           <value>buttons</value>
           <value>labels</value>
         </list>    
       </property>
     </bean>
   </property>
 </bean>

with the class:

public class LoggingMessageSource implements MessageSource {

  private MessageSource pathtru;

  public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
    // ...
  }

  public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
    // ...
  }

  public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
    // ...
  }
}

I don't know if some methods are calling the other ones so you will have to experiment a bit.

Vladimir
Thanks! That works. I've just came to the same solution :)
glaz666