Search code examples
javaweb-serviceslogginglogbacklarge-files

Logback - dynamic configuration


Is it possible to configure logback in such way that custom logback property :

<configuration scan="true" scanPeriod="60 seconds">

  <property name="logFullMessage" value="false" />

  <!-- project appenders defitions -->
  <!-- project loggers defititions -->

</configuration>

will influence which pattern or appender will be used ? We do have web services application which operates with large requests/responses, by default we do not wish to log request/response body, but when problem occurs we would like to have option to switch it on (logFullMessage=true) and store full response body into the log file.


Solution

  • To switch the used appender you could do (only relevant lines are shown):

    <property name="USE_APPENDER" value="FILE1" />
    
    <appender name="FILE1" class="ch.qos.logback.core.FileAppender">
      ....
    </appender>
    
    <appender name="FILE2" class="ch.qos.logback.core.FileAppender">
      ....
    </appender>
    
    <root level="warn">
        <appender-ref ref="${USE_APPENDER}"/>
    </root>
    

    This in combination with the rescan options which you already have enabled makes it possible to switch the appenders.