logback how to set destination folder for log files

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.


I've wasted a lot of time configuring Logback to work with Spring Boot and I'd like to share my configuration, hoping to save other people from wasting their time.

My example is similar to Andy Dufresne's above, with one key difference - no <property> tag. This was really important in my case because if you include the <property name="logs_dir" value="." /> you won't be able to override it using system properties, which I wanted to do like this:

java -jar -Dlogs_dir=~/newLogsDir yourApp.jar 

Also note the default value is set inside the path variable - ${logs_dir:-.}. Hope this helps:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-20(%d{yyyy-MM-dd HH:mm:ss} %highlight([%-5level])) %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logs_dir:-.}/system.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover monthly -->
            <fileNamePattern>system-%d{yyyy-MM}.log.zip</fileNamePattern>
            <maxHistory>12</maxHistory>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%-26(%d [%-5level]) %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

</configuration>