Spring Boot集成log4j2

作者: 183207efd207 | 来源:发表于2016-11-14 18:07 被阅读1283次

    log4j2优点:http://logging.apache.org/log4j/2.x/
    最喜欢的一点:日志不同级别可以用颜色区分,定位问题非常快捷。

    pom文件的依赖配置中,去掉spring boot默认的log配置,引入log4j2依赖包:

      <dependency> <!-- exclude掉spring-boot的默认log配置 -->  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter</artifactId>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-starter-logging</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <dependency> <!-- 引入log4j2依赖 -->  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-log4j2</artifactId>  
        </dependency>  
        <dependency>  <!-- 加上这个才能辨认到log4j2.yml文件 -->  
            <groupId>com.fasterxml.jackson.dataformat</groupId>  
            <artifactId>jackson-dataformat-yaml</artifactId>  
        </dependency> 
    

    log4j2.yaml配置

    Configuration:
      status: debug
      Appenders:
        Console:#输出到控制台
          name: Console
          PatternLayout:
            Pattern: '%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.}:%L - %msg%n}{%throwable{short.fileName}}{FATAL=white,ERROR=red,
              WARN=blue, INFO=black, DEBUG=green, TRACE=blue}'
          target: SYSTEM_OUT
        RollingFile:# 输出到文件,超过2048MB归档 
        - name: RollingFile_Appender
          fileName: /logs/events-csg-adapter/app.log
          filePattern: "/logs/events-csg-adapter/event-csg-adapter-%i.log.%d{yyyy-MM-dd}"
          PatternLayout:#设置日志级别的颜色
            pattern: '%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.}:%L - %msg%n}{%throwable{short.fileName}}{FATAL=white,ERROR=red,
                               WARN=blue, INFO=black, DEBUG=green, TRACE=blue}'
          Policies:
            SizeBasedTriggeringPolicy:
              size: 2048 M
            DefaultRollOverStrategy:
              max: 10
      Loggers:
        Root:
          AppenderRef:
          - ref: Console
          - ref: RollingFile_Appender
        logger:
        - name: org.springframework
          level: debug
        - name: com.apricotforest.events
          level: debug
    

    输出日志:

    Paste_Image.png

    相关文章

      网友评论

        本文标题:Spring Boot集成log4j2

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