参考

springboot log4j2配置,使用gradle构建

在 gradle 中引入 log4j2,并移除 spring boot 默认 logger。

引入不提。

移除:

configurations {
   providedRuntime
   // remove default logger
   all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

二 yml配置

#log4j2
Configuration:
  status: warn

  Properties: # 定义变量
    Property:
      - name: log.path
        value: d:\\tmp\\logs
      - name: project.name
        value: spring-boot-log

  Appenders:
    Console:  #输出到控制台
      name: CONSOLE
      target: SYSTEM_OUT
      ThresholdFilter:
        level: trace
        onMatch: ACCEPT
        onMismatch: DENY
      PatternLayout:
        pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"

    # 输出到文件,超过128MB归档
    RollingFile:
      - name: ROLLING_FILE
        ignoreExceptions: false
        fileName: ${log.path}/${project.name}.log
        filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
        PatternLayout:
          pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: "128 MB"
        DefaultRolloverStrategy:
          max: 1000

  Loggers:
    Root:
      level: info
      AppenderRef:
        - ref: CONSOLE
        - ref: ROLLING_FILE
    Logger: # 为com.xjj包配置特殊的Log级别,方便调试
      - name: com.test.dao
        additivity: false
        level: debug
        AppenderRef:
          - ref: CONSOLE
          - ref: ROLLING_FILE