日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Springboot 切換Log4j2日志

作者:jasperLiu17 更新時(shí)間: 2024-04-02 編程語(yǔ)言

?SpringBoot的日志框架使用、日志框架切換【log4j2】

SpringBoot 整合 log4j2 使用yml的配置

目錄

排除logging starter

配置log4j2.yaml

排除logging starter

從上圖可以看到,需要從spring-boot-starter-web里面排除logging starter,并加入log4j2 starter

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

springboot會(huì)讀取到log4j2-spring.xml的配置文件(spring推薦),如果是log4j2.yaml的配置文件,那么需要加入依賴(lài)

<!-- 加上這個(gè)才能辨認(rèn)到log4j2.yml文件 -->
<dependency>
? ? <groupId>com.fasterxml.jackson.dataformat</groupId>
? ? <artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

配置log4j2.yaml

 # 共有8個(gè)級(jí)別,按照從低到高為:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
  status: warn
  monitorInterval: 30

  Properties: # 定義全局變量
    Property: # 缺省配置(用于開(kāi)發(fā)環(huán)境)。其他環(huán)境需要在VM參數(shù)中指定,如下:
      #測(cè)試:-Dlog.level.console=warn -Dlog.level.xjj=trace
      #生產(chǎn):-Dlog.level.console=warn -Dlog.level.xjj=info
      - name: log.level.console
        value: debug
      - name: log.sql.level
        value: trace
      - name: log.path
        value: ${user.home}/logs
  Appenders:
    Console:  #輸出到控制臺(tái)
      name: CONSOLE
      target: SYSTEM_OUT
      ThresholdFilter:
        level: ${sys:log.level.console} # “sys:”表示:如果VM參數(shù)中沒(méi)指定這個(gè)變量值,則使用本文件中定義的缺省全局變量值
        onMatch: ACCEPT
        onMismatch: DENY
      PatternLayout:
        pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
    RollingFile: # 輸出到文件,超過(guò)128MB歸檔
      - name: info
        ignoreExceptions: false
        fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/info.log
        filePattern: "${sys:log.path}/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
        ThresholdFilter:
          level: info
          onMatch: ACCEPT
          onMismatch: DENY
        PatternLayout:
          pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: "128 MB"
        DefaultRolloverStrategy:
          max: 1000
      - name: debug
        ignoreExceptions: false
        fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/debug.log
        filePattern: "${sys:log.path}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
        ThresholdFilter:
          level: debug
          onMatch: ACCEPT
          onMismatch: DENY
        PatternLayout:
          pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: "128 MB"
        DefaultRolloverStrategy:
          max: 1000
      - name: error
        ignoreExceptions: false
        fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/error.log
        filePattern: "${sys:log.path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
        ThresholdFilter:
          level: error
          onMatch: ACCEPT
          onMismatch: DENY
        PatternLayout:
          pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: "128 MB"
        DefaultRolloverStrategy:
          max: 1000
  Loggers:
    Root:
      level: info
      AppenderRef:
        - ref: CONSOLE
        - ref: info
        - ref: debug
        - ref: error
#    Logger: # 為com.xjj包配置特殊的Log級(jí)別,方便調(diào)試
#      - name: cnki.bdms.module.search.dal
#        additivity: true
#        level: ${sys:log.sql.level}
#        AppenderRef:
#          - ref: info
#          - ref: debug
#          - ref: error
#          - ref: CONSOLE

application.yaml指明log4j2.yaml位置

logging:
  config: classpath:log4j2.yaml

?

原文鏈接:https://blog.csdn.net/weixin_39314736/article/details/137035751

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類(lèi)
最近更新