美文网首页
SpringBoot 2.x Druid中整合上Log4j2

SpringBoot 2.x Druid中整合上Log4j2

作者: 12313凯皇 | 来源:发表于2019-04-10 16:59 被阅读0次

    本文主要是在整合了druid的情况下整合log4j2,如果还没有整合druid的可以先参考我的另一篇文章:SpringBoot2.x 整合druid,并将数据库密码加密

    下面开始整合log4j2

    步骤一

    pom.xml中添加以下依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <!--移除默认的logback-->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!--log4j2-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
    <!-- 加上这个才能辨认到log4j2.yml文件 -->
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>
    
    步骤二

    resources目录下新建一个log4j2.yml文件:

    #log4j2.yml
    Configuration:
      status: OFF
      name: YAMLConfigTest
      properties:
        property:
          name: filename
          value: target/test-yaml.log
      thresholdFilter:
        level: debug
      appenders:
        #只接受程序中DEBUG级别的日志进行处理
        Console:
          name: Console
          target: SYSTEM_OUT
          ThresholdFilter:
            level: DEBUG
            onMatch: ACCEPT
            onMismatch: DENY
          PatternLayout:
            Pattern: "[%d{HH:mm:ss.SSS}] %-5level %class{36} %L %M - %msg%xEx%n"
        #处理DEBUG级别的日志,并把该日志放到logs/debug.log文件中
        RollingFile:
        #打印出DEBUG级别日志,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档
        - name: RollingFileDebug
          fileName: ./logs/bulb/debug.log
          filePattern: "logs/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
          PatternLayout:
            Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
          Filters:
            ThresholdFilter:
            - level: DEBUG
            - level: INFO
              onMatch: DENY
              onMismatch: NEUTRAL
          Policies:
            SizeBasedTriggeringPolicy:
              size: 512MB
        #处理INFO级别的日志,并把该日志放到tomcat根目录/bin/logs/info.log文件中
        - name: RollingFileInfo
          fileName: ./logs/bulb/info.log
          filePattern: "logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
          PatternLayout:
            Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
          Filters:
            #只接受INFO级别的日志,其余的全部拒绝处理
            ThresholdFilter:
            - level: INFO
            - level: WARN
              onMatch: DENY
              onMismatch: NEUTRAL
          Policies:
            SizeBasedTriggeringPolicy:
              size: 512MB
        #处理WARN级别的日志,并把该日志放到logs/warn.log文件中
        - name: RollingFileWarn
          fileName: ./logs/bulb/warn.log
          filePattern: "logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"
          PatternLayout:
            Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
          Filters:
            ThresholdFilter:
            - level: WARN
            - level: ERROR
              onMatch: DENY
              onMismatch: NEUTRAL
          Policies:
            SizeBasedTriggeringPolicy:
              size: 512MB
        #处理error级别的日志,并把该日志放到logs/error.log文件中
        - name: RollingFileError
          fileName: ./logs/bulb/error.log
          filePattern: "logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
          PatternLayout:
            Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
          ThresholdFilter:
            level: ERROR
          Policies:
            SizeBasedTriggeringPolicy:
              size: 512MB
        #druid的日志记录追加器
        - name: druidSqlRollingFile
          fileName: ./logs/bulb/druid-sql.log
          filePattern: "logs/$${date:yyyy-MM}/api-%d{yyyy-MM-dd}-%i.log.gz"
          PatternLayout:
            Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %L %M - %msg%xEx%n"
          Policies:
            SizeBasedTriggeringPolicy:
              size: 512MB
      loggers:
        logger:
          #记录druid-sql的记录
          name: druid.sql.Statement
          level: debug
          additivity: false
          appender-ref:
            ref: druidSqlRollingFile
        #log4j2 自带过滤日志
        Logger:
        - name: org.apache.catalina.startup.DigesterFactory
          level: error
        - name: org.apache.catalina.util.LifecycleBase
          level: error
        - name: org.apache.coyote.http11.Http11NioProtocol
          level: warn
        - name: org.apache.sshd.common.util.SecurityUtils
          level: warn
        - name: org.apache.tomcat.util.net.NioSelectorPool
          level: warn
        - name: org.crsh.plugin
          level: warn
        - name: org.crsh.ssh
          level: warn
        - name: org.eclipse.jetty.util.component.AbstractLifeCycle
          level: error
        - name: org.hibernate.validator.internal.util.Version
          level: warn
        - name: org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration
          level: warn
        - name: org.springframework.boot.actuate.endpoint.jmx
          level: warn
        - name: org.thymeleaf
          level: warn
        root:
          level: DEBUG
          appender-ref:
          - ref: Console
          - ref: RollingFileInfo
          - ref: RollingFileWarn
          - ref: RollingFileError
          - ref: RollingFileDebug
    
    
    步骤三

    application.yml中添加:

    spring:
        druid:
          #开启log4j2
          log4j2:
            enabled: true
    logging:
      config: classpath:log4j2.yml
    

    这样就整合完成了,按照我的这个yml文件运行后log文件在tomcat根目录/bin/logs/bulb目录下。

    相关文章

      网友评论

          本文标题:SpringBoot 2.x Druid中整合上Log4j2

          本文链接:https://www.haomeiwen.com/subject/rzloiqtx.html