1. 程式人生 > >Log4j和Log4j2的區別(SpringBoot中配置使用)

Log4j和Log4j2的區別(SpringBoot中配置使用)

前言:

日誌的級別:

預設的幾個級別:trace<debug<info<warn<error<fatal

說明:

   1.級別之間是包含關係,意思是如果你設定日誌級別是trace,則大於等於這個級別的日誌都會輸出。

   2.trace:追蹤,就是程式推進一下,你就可以寫個trace輸出,所以trace應該特別多,不過沒關係,我們可以設定最低日誌級別不讓他輸出。

   3.debug:除錯,一般設定這個為最低級別。

   4.info:輸出你感興趣或者重要的資訊,最經常使用。

   5.warn:youxie資訊不是錯誤資訊但是也要給出提示。

   6.error:錯誤資訊。

   7.fatal:重大錯誤。

正文:

一、配置檔案型別(IntelliJ IDEA的SpringBoot的main/resources/下)

log4j是通過一個log4j.properties的檔案作為主配置檔案的,而現在的log4j2則已棄用,採用的是log4j2.xml或log4j2.yml或者log4j2.json

均在main/resources/下

log4j.properties

#log4j.rootLogger=CONSOLE,info,error,DEBUG
log4j.rootLogger=info,error,CONSOLE,DEBUG
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout     
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = info   
log4j.appender.info.append=true   
#log4j.appender.info.File=/home/admin/pms-api-services/logs/info/api_services_info
log4j.appender.info.File=/Users/dddd/Documents/testspace/pms-api-services/logs/info/api_services_info
log4j.logger.error=error  
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout     
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = error   
log4j.appender.error.append=true   
#log4j.appender.error.File=/home/admin/pms-api-services/logs/error/api_services_error
log4j.appender.error.File=/Users/dddd/Documents/testspace/pms-api-services/logs/error/api_services_error
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG   
log4j.appender.DEBUG.append=true   
#log4j.appender.DEBUG.File=/home/admin/pms-api-services/logs/debug/api_services_debug
log4j.appender.DEBUG.File=/Users/dddd/Documents/testspace/pms-api-services/logs/debug/api_services_debug


log4j2.yml

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


二、核心jar包
log4j在springboot中預設整合

log4j2需要在pom.xml中新增以下依賴

  <!--去除exclude預設logging依賴-->
        <dependency>
            <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>
        <!--新增log4j2依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!--新增能夠使用log4j2.yml配置的依賴-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>


三、呼叫

log4j

import org.apache.log4j.Logger;
private static Logger log = Logger.getLogger(Hello.class);
log.info("test log4j");

log4j2

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
private static Logger logger = LogManager.getLogger(HelloController.class);
logger.info("test log4j2");