logback how to set destination folder for log files
Asked Answered
S

3

14

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?

Shirlshirlee answered 4/1, 2015 at 9:13 Comment(0)
B
23

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.

Brigid answered 4/1, 2015 at 17:21 Comment(1)
You don't even have to set a property. The "user.home" system property is already there. So you could use that.Rooky
A
12

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>
Acherman answered 21/12, 2017 at 18:16 Comment(0)
D
3

I have a spring boot app, and I run the fat .jar as a systemd service.

I struggled a couple of hours on how to set the LOG_PATH relative to the user's home dir.

Here is what worked for me:

  • in application.properties I have:

logging.path=${HOME}/attach_logs

  • in logback-spring.xml I have:

<springProperty scope="context" name="LOG_PATH" source="logging.path"/>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/console.log</file>

References:

Getting the user home path in application.properties in Spring Boot

Accessing the application properties in logback.xml

Spring boot logging path

logback how to set destination folder for log files

Drug answered 20/8, 2019 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.