1. The background

SpringBoot uses the Logback framework as the logging framework by default. Recently, I came up with the idea of “Since multiple environments are configured, such as development environment, test environment, etc., I want to specify the storage location of log files according to different environments”.

Act 2.

Divided into the following steps:

  • Step 1: Configure multiple environments
  • Step 2: Configure parameters for different environments
  • Step 3: Configure the logback configuration file
  • Step 4: Configure the appender

2.1 Step 1: Configure multiple environments

I have configured several environments as follows:

  • application-dev.yml
  • application-test.yml
  • application-release.yml

Reference: www.jianshu.com/p/61758ef6b…

2.2 Step 2: Configure parameters in different environments

Application dev. Yml: application dev. Yml: application dev. Yml

logging:
  path: /Users/zhangyunfei/Downloads
  ....
Copy the code

The following is the configuration of the online environment, which is configured in Linux under application-release.yml:

logging:
  path: /data/logs
  ....
Copy the code

2.3 Step 3: Configure the Logback configuration file

Open the logback configuration file logback-spring. XML and use springProperty to read the springBoot parameters, in which case the logging.path parameter is read.

Code:

<springProperty name="LOG_PATH" source="logging.path" defaultValue="./logs" />
    <property name="log.path" value="${LOG_PATH:-.}/spf-eureka"/>
Copy the code

2.4 Step 4: Configure the appender

<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/eureka_info.%d{yyyyMMdd}.log</fileNamePattern> <! </MaxHistory> </rollingPolicy> <encoder> >${pattern}</pattern> <charset>UTF-8</charset> </encoder> </appender>Copy the code

END