SpringBoot's logging is based on the application multi-environment configuration file configuration log output to a file (not spring-logback.xml)

General situation: Log files are divided into folders according to yyyy-MM-dd, %i cuts files according to the size of a single log file and names them with an index.

  1. Development environment (dev): only output to the console, not to the file, and the log level is INFO.
  2. Test environment (test): output to the console and output to a file, the log level is INFO, and the log content can be cleared when restarting (convenient for debugging).
  3. Production environment (prod): do not output to the console, only output to the file, the log level is ERROE (only error logs are recorded), and it is forbidden to clear the log when restarting (to prevent other people's error logs from being cleared, causing others to be unable to locate the problem) .

pom.xml adds environment configuration

	<!-- 环境配置 -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <!--  设为默认开启 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

application.yml

spring:
  application:
    name: '@artifactId@'
  profiles:
    #环境(idea右边栏的maven可以选配置文件的环境)
    active: '@spring.profiles.active@'

application-dev.yml

Development environment configuration file

#按需配置

application-test.yml

Test environment configuration file

logging:
  level:
    #spring根日志级别
    root: error
    #spring web日志级别
    web: error
    #项目日志级别
    com.fu.easycode: info
    #SQL日志打印
    com.fu.easycode.mapper: debug
  file:
    #日志存放路径
    path: ./log/${
    
    spring.application.name}/${
    
    spring.profiles.active}
    #日志名称
    name: '${logging.file.path}.log'
  charset:
    #控制台编码
    console: UTF-8
    #输出到文件编码
    file: UTF-8
  pattern:
    #控制台日志输出样式
    console: '%clr(%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}'
    #输出到文件的样式
    file: '%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}'
  logback:
    rollingpolicy:
      #重启是否清除日志(推荐测试环境使用)
      clean-history-on-start: true
      #切割日志格式
      file-name-pattern: ${
    
    logging.file.path}.%d{
    
    yyyy-MM-dd}.%i.log
      #单个日志文件最大大小
      max-file-size: 10MB
      #全部日志文件最大大小
      total-size-cap: 50GB
      #日志存放天数
      max-history: 15

application-prod.yml

Production environment configuration file

logging:
  level:
    #spring根日志级别
    root: error
    #spring web日志级别
    web: error
    #项目日志级别
    com.fu.easycode: error
    #SQL日志打印
    com.fu.easycode.mapper: debug
  file:
    #日志存放路径
    path: ./log/${
    
    spring.application.name}/${
    
    spring.profiles.active}
    #日志名称
    name: '${logging.file.path}.log'
  charset:
    #输出到文件编码
    file: UTF-8
  pattern:
    #输出到文件的样式
    file: '%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss}} ${LOG_LEVEL_PATTERN:-%5p} ${PID: } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}'
  logback:
    rollingpolicy:
      #重启是否清除日志,默认:否
      clean-history-on-start: false
      #切割日志格式
      file-name-pattern: ${
    
    logging.file.path}.%d{
    
    yyyy-MM-dd}.%i.log
      #单个日志文件最大大小
      max-file-size: 10MB
      #全部日志文件最大大小
      total-size-cap: 50GB
      #日志存放天数
      max-history: 15

Guess you like

Origin blog.csdn.net/weixin_43933728/article/details/132040005