springboot+log4j.yml配置日志文件的方法

一,Maven 依賴 pom.xml配置

1, 去掉默認日志,以便切換到log4j2的日志依賴

2, 然後添加如下兩個日志依賴

二,在工程根目錄下添加 lo4g2.yml 配置文件

2, 配置文件內容

<!-- 配置 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>

三,在工程根目錄下添加 lo4g2.yml 配置文件

1, 文件存放位置

2, 配置文件內容

# 共有8個級別,按照從低到高為:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
 status: warn
 monitorInterval: 30
 Properties: # 定義全局變量
 Property: # 缺省配置(用於開發環境)。其他環境需要在VM參數中指定,如下:
 #測試:-Dlog.level.console=warn -Dlog.level.xjj=trace
 #生產:-Dlog.level.console=warn -Dlog.level.xjj=info
 - name: log.level.console
 value: info
 - name: log.path
 value: log
 - name: project.name
 value: opendoc
 - name: log.pattern
 value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"
 Appenders:
 Console: #輸出到控制臺
 name: CONSOLE
 target: SYSTEM_OUT
 PatternLayout:
 pattern: ${log.pattern}
# 啟動日志
 RollingFile:
 - name: ROLLING_FILE
 fileName: ${log.path}/${project.name}.log
 filePattern: "${log.path}/historyRunLog/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
 PatternLayout:
  pattern: ${log.pattern}
 Filters:
# 一定要先去除不接受的日志級別,然後獲取需要接受的日志級別
  ThresholdFilter:
  - level: error
  onMatch: DENY
  onMismatch: NEUTRAL
  - level: info
  onMatch: ACCEPT
  onMismatch: DENY
 Policies:
  TimeBasedTriggeringPolicy: # 按天分類
  modulate: true
  interval: 1
 DefaultRolloverStrategy: # 文件最多100個
  max: 100
# 平臺日志
 - name: PLATFORM_ROLLING_FILE
 ignoreExceptions: false
 fileName: ${log.path}/platform/${project.name}_platform.log
 filePattern: "${log.path}/platform/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
 PatternLayout:
  pattern: ${log.pattern}
 Policies:
  TimeBasedTriggeringPolicy: # 按天分類
  modulate: true
  interval: 1
 DefaultRolloverStrategy: # 文件最多100個
  max: 100
# 業務日志
 - name: BUSSINESS_ROLLING_FILE
 ignoreExceptions: false
 fileName: ${log.path}/bussiness/${project.name}_bussiness.log
 filePattern: "${log.path}/bussiness/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
 PatternLayout:
  pattern: ${log.pattern}
 Policies:
  TimeBasedTriggeringPolicy: # 按天分類
  modulate: true
  interval: 1
 DefaultRolloverStrategy: # 文件最多100個
  max: 100
# 錯誤日志
 - name: EXCEPTION_ROLLING_FILE
 ignoreExceptions: false
 fileName: ${log.path}/exception/${project.name}_exception.log
 filePattern: "${log.path}/exception/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
 ThresholdFilter:
  level: error
  onMatch: ACCEPT
  onMismatch: DENY
 PatternLayout:
  pattern: ${log.pattern}
 Policies:
  TimeBasedTriggeringPolicy: # 按天分類
  modulate: true
  interval: 1
 DefaultRolloverStrategy: # 文件最多100個
  max: 100
# DB 日志
 - name: DB_ROLLING_FILE
 ignoreExceptions: false
 fileName: ${log.path}/db/${project.name}_db.log
 filePattern: "${log.path}/db/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
 PatternLayout:
  pattern: ${log.pattern}
 Policies:
  TimeBasedTriggeringPolicy: # 按天分類
  modulate: true
  interval: 1
 DefaultRolloverStrategy: # 文件最多100個
  max: 100
 Loggers:
 Root:
 level: info
 AppenderRef:
 - ref: CONSOLE
 - ref: ROLLING_FILE
 - ref: EXCEPTION_ROLLING_FILE
 Logger:
 - name: platform
 level: info
 additivity: false
 AppenderRef:
  - ref: CONSOLE
  - ref: PLATFORM_ROLLING_FILE
 - name: bussiness
 level: info
 additivity: false
 AppenderRef:
  - ref: BUSSINESS_ROLLING_FILE
 - name: exception
 level: debug
 additivity: true
 AppenderRef:
  - ref: EXCEPTION_ROLLING_FILE
 - name: db
 level: info
 additivity: false
 AppenderRef:
  - ref: DB_ROLLING_FILE
# 監聽具體包下面的日志
# Logger: # 為com.xjj包配置特殊的Log級別,方便調試
# - name: com.xjj
# additivity: false
# level: ${sys:log.level.xjj}
# AppenderRef:
#  - ref: CONSOLE
#  - ref: ROLLING_FILE

3, 在application.yml引入log4j2.yml

四, 不同日志枚舉類

五, 不同日志工具類util編輯

package com.tcs.irecr.util;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* 本地日志參考類 *

@author Administrator *

*/

public class LogUtils {
//* 獲取業務日志logger

}

public static Logger getBussinessLogger() {
return LoggerFactory.getLogger(LogEnum.BUSSINESS.getCategory());

}

//獲取平臺日志logger

public static Logger getPlatformLogger() {
return LoggerFactory.getLogger(LogEnum.PLATFORM.getCategory());

}

//獲取數據庫日志logger

public static Logger getDBLogger() {
return LoggerFactory.getLogger(LogEnum.DB.getCategory());

}

//獲取異常日志logger

public static Logger getExceptionLogger() {
return LoggerFactory.getLogger(LogEnum.EXCEPTION.getCategory());

}

六, 運行時自動在工程目錄下生產日志目錄和日志文件

到此這篇關於springboot+log4j.yml配置日志文件的方法的文章就介紹到這瞭,更多相關springboot log4j.yml配置日志內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: