Search code examples
spring-bootlog4j2error-logging

Log Springboot classes (like DefaultMessageListenerContainer) logs to custom file


I am facing one problem to log DefaultMessageListenerContainer.java (which is as Springboot class) error logs in a custom file using Log4j2 configuration.

Below is the configuration setup in Log4j2.xml file:

<Logger name="org.springframework.jms.listener.DeafultMessageListenerContainer" level="error">
    <AppenderRef ref="customErrorFile">
</Logger>

But still I am getting below error logs in Console output and not in custom file. Can anyone please suggest what I am doing wrong here?

ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-1) Could not refresh JMS Connection for destination 'wfQueue' - retrying using FixedBackOff{interval=5000, currentAttempts=44, maxAttempts=unlimited}.

Solution

  • Found the solution. Below are two different options to print DeafultMessageListenerContainer class logs in custom file:

    1. Add Exception Listener implementation by using setExceptionListener(), and print custom logs in onJmsException() method, like below:

       public class MyExceptionListener implements ExceptionListener{
      
         @Override
         public void onException(JMSException arg0) {
           customErrorLogger.error("Custom error message");
         }
       }
      

      And set exception listener as below:

      deafultMessageListenerContainer.setExceptionListener(new MyExceptionListener());
      
    2. DeafultMessageListenerContainer (Spring class) is using "this.logger" to print exception/error logs so though to add DeafultMessageListenerContainer class package as Logger in Log4j2.xml. But "this.logger" is defined in parent class of this listener class which is JmsAccessor class, so defining below Logger in Log4j2.xml also solved this issue:

      <Logger name="org.springframework.jms.support.JmsAccessor" level="DEBUG" additivity="false">
         <Appender-ref ref="customErrorFile" level="error"/>
      </Logger>