Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback how to set destination folder for log files

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?

like image 804
user79074 Avatar asked Jan 04 '15 09:01

user79074


People also ask

Where should log files be stored?

All logging files are stored in the same log path directory, which by default is the parent location directory. This path can be (and should be) set to a separate physical storage device, providing greater protection against a system hard disk crash.

How do I change the Logback configuration file?

Setting the location of the configuration file via a system property. You may specify the location of the default configuration file with a system property named "logback. configurationFile" . The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

Where should Logback xml be stored?

Configuring Logging with logback. xml. The configuration of logging using logback requires that you put a logback. xml file on the classpath of the application.


3 Answers

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>
like image 181
albogdano Avatar answered Oct 19 '22 12:10

albogdano


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.

like image 37
Andy Dufresne Avatar answered Oct 19 '22 12:10

Andy Dufresne


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

like image 3
razvang Avatar answered Oct 19 '22 13:10

razvang