spring log4j2整合的maven配置

1. 在spring里用log4j2要做一下准备

    1. 解决spring 日志依赖冲突
    2. 补充yaml解析依赖,让项目可以使用log4j2.yml配置,如果格式是log4j2.properties,是不需要的(我喜欢yml的格式)
    3. 补充log4j2y依赖
    4. 其他框架,如mybatis,定义日志实现类使用log4j2

2.maven pom.xml 做以下配置

排除 spring-boot-starter-logging

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-kafka</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
        
        
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.12.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.12.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

3.日志文件的配置

    1. 定义appenders
    2. 配置appenders的参数
    3. 设置日志输出名称分类和日志级别
    4. 继承appenders
log4j2.yml

Configuration:
  status: warn
  Properties:
    property:
      - name: "dir"
        value: "logs"

  appenders:
    Console:
      name: Console
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"

    RollingFile:
      - name: RollingFile
        fileName: "${sys:dir}/app.log"
        filePattern: "${sys:dir}/app-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          TimeBasedTriggeringPolicy:
            interval: 1
            modulate: true
          SizeBasedTriggeringPolicy:
            size: 100 MB
        DefaultRolloverStrategy:
          max: 10
          Delete:
            basePath: ${sys:dir}
            maxDepth: 2
            IfFileName:
              glob: "*/app*log.gz"
            IfLastModified:
              age: 30d
      - name: SqlRollingFile
        fileName: "${sys:dir}/sql.log"
        filePattern: "${sys:dir}/sql-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          TimeBasedTriggeringPolicy:
            interval: 1
            modulate: true
          SizeBasedTriggeringPolicy:
            size: 100 MB
        DefaultRolloverStrategy:
          max: 10
          Delete:
            basePath: ${sys:dir}
            maxDepth: 2
            IfFileName:
              glob: "*/sql*log.gz"
            IfLastModified:
              age: 30d

  Loggers:
    Root:
      level: error
      AppenderRef:
        ref: Console
    Logger:
      - name: org.cyt
        additivity: false
        level: info
        AppenderRef:
          - ref: Console
      - name: org.springframework
        additivity: false
        level: error
        AppenderRef:
          - ref: Console
      - name: org.cyt.tidb.mapper
        level: error
        additivity: false
        AppenderRef:
          - ref: SqlRollingFile
      - name: org.cyt.doris.mapper
        level: error
        additivity: false
        AppenderRef:
          - ref: SqlRollingFile
      - name: org.springframework.cloud.task
        additivity: false
        level: debug
        AppenderRef:
          - ref: Console

4.spring application.yml 修改

mybatis:
  configuration:
    default-statement-timeout: 30
    log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
Share