Logback how to set destination folder for log files

Is there a way to set one destination folder so that I can specify where all the log files should be created, and not install them based on appender on the basis of appender?

+7
source share
3 answers

You can define a property in the log configuration file and use it 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 variables from System properties or a separate property file. For more information, follow manual .

+10
source

I spent a lot of time setting up Logback to work with Spring Boot , and I would like to share my configuration, hoping to save other people from wasting my time.

My example is similar to Andy Dufresne above, with one key difference - the <property> . This was really important in my case, because if you include <property name="logs_dir" value="." /> <property name="logs_dir" value="." /> , you cannot override it with the system properties, which I would like to do as follows:

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

Also note that the default value is set in 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> 
+2
source

I have a boot spring application and I am running fat .jar as a systemd service.

For a couple of hours I tried to set LOG_PATH relative to the user's home directory.

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 user home path in application.properties in Spring Boot

Access application properties in logback.xml

Boot Spring Registration Path

logback how to set destination folder for log files

0
source

Source: https://habr.com/ru/post/1210346/


All Articles