您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在springboot項目中配置log4j.yml日志文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1, 去掉默認日志,以便切換到log4j2的日志依賴
2, 然后添加如下兩個日志依賴
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>
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
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日志文件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。