logback how to set destination folder for log files

user79074 picture user79074 · Jan 4, 2015 · Viewed 29.9k times · Source

Is there a way to set a single destination folder, such that I can specify where all log files should be created rather than having to set it on an appender by appender basis?

Answer

Andy Dufresne picture Andy Dufresne · Jan 4, 2015

You can define a property in the logback configuration file an use it as below

<configuration>

  <property name="USER_HOME" value="/home/sebastien" />

  <appender name="SPRING_LOGS" class="ch.qos.logback.core.FileAppender">
    <file>${USER_HOME}/spring.log</file>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${USER_HOME}/myApp.log</file>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Note that logback can read the variables from System properties or a separate properties file too. Follow the manual for more details.