在 Java 中如何使用 Log4j 創建自己的日誌記錄級別(配置 Log4j 2)
已發表: 2017-07-26
如果您需要在 Log4j 中添加自己的日誌記錄級別,則可以按如下方式進行。 您將必須創建自己的類,該類將從Level
擴展,使用 Apache Log4j 2 Custom Log Levels
。
Log4j
是一個簡單靈活的logging framework
。 日誌記錄為開發人員提供了應用程序故障的詳細上下文。 使用 log4j 可以在運行時啟用日誌記錄,而無需修改應用程序二進製文件。
log4j 包的設計使這些語句可以保留在交付的代碼中,而不會產生沉重的性能成本。

Log4j 允許記錄請求打印到多個目的地。 在 log4j 中,輸出目的地稱為appender
。 它們從控制台、文件、GUI 組件、遠程套接字服務器到 JMS 各不相同。
您需要的 Jar 文件。
這是一個 Maven 依賴項:
1 2 3 4 5 |
< dependency > < groupId > log4j < / groupId > < artifactId > log4j < / artifactId > < version > 2.16.0 < / version > < / dependency > |
將 Log4j 更新到最新版本
CVE-2021-44228:Apache Log4j2 <=2.14.1 配置、日誌消息和參數中使用的 JNDI 功能不能防止攻擊者控制的 LDAP 和其他 JNDI 相關端點。
從 log4j 2.16.0 開始,默認情況下已禁用此行為。
您必須將log4j.xml
文件放在/resources
文件夾下:

這是一個示例 Java 代碼:
- 用於 Java 中自定義日誌級別的 Log4j 日誌記錄
- 在 log4j 中創建自己的日誌記錄級別
- log4j 自定義記錄器示例
CrunchifyLog4jLevel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com . crunchify . tutorials ; import org . apache . log4j . Level ; /** * @author Crunchify.com * */ @SuppressWarnings ( "serial" ) public class CrunchifyLog4jLevel extends Level { /** * Value of CrunchifyLog4jLevel level. This value is lesser than DEBUG_INT and higher * than TRACE_INT} */ public static final int CRUNCHIFY_INT = DEBUG_INT - 10 ; /** * Level representing my log level */ public static final Level CRUNCHIFY = new CrunchifyLog4jLevel ( CRUNCHIFY_INT , "CRUNCHIFY" , 10 ) ; /** * Constructor */ protected CrunchifyLog4jLevel ( int arg0 , String arg1 , int arg2 ) { super ( arg0 , arg1 , arg2 ) ; } /** * Checks whether logArgument is "CRUNCHIFY" level. If yes then returns * CRUNCHIFY}, else calls CrunchifyLog4jLevel#toLevel(String, Level) passing * it Level#DEBUG as the defaultLevel. */ public static Level toLevel ( String logArgument ) { if ( logArgument ! = null && logArgument.toUpperCase().equals("CRUNCHIFY")) { return CRUNCHIFY; } return ( Level ) toLevel ( logArgument , Level . DEBUG ) ; } /** * Checks whether val is CrunchifyLog4jLevel#CRUNCHIFY_INT. If yes then * returns CrunchifyLog4jLevel#CRUNCHIFY, else calls * CrunchifyLog4jLevel#toLevel(int, Level) passing it Level#DEBUG as the * defaultLevel * */ public static Level toLevel ( int val ) { if ( val == CRUNCHIFY_INT ) { return CRUNCHIFY ; } return ( Level ) toLevel ( val , Level . DEBUG ) ; } /** * Checks whether val is CrunchifyLog4jLevel#CRUNCHIFY_INT. If yes * then returns CrunchifyLog4jLevel#CRUNCHIFY, else calls Level#toLevel(int, org.apache.log4j.Level) * */ public static Level toLevel ( int val , Level defaultLevel ) { if ( val == CRUNCHIFY_INT ) { return CRUNCHIFY ; } return Level . toLevel ( val , defaultLevel ) ; } /** * Checks whether logArgument is "CRUNCHIFY" level. If yes then returns * CrunchifyLog4jLevel#CRUNCHIFY, else calls * Level#toLevel(java.lang.String, org.apache.log4j.Level) * */ public static Level toLevel ( String logArgument , Level defaultLevel ) { if ( logArgument ! = null && logArgument.toUpperCase().equals("CRUNCHIFY")) { return CRUNCHIFY; } return Level . toLevel ( logArgument , defaultLevel ) ; } } |
另一個必須閱讀:

- 如何通過命令行啟動停止 Apache Tomcat 服務器? (設置為 Windows 服務)
- 在 Eclipse 中創建和部署簡單的 Web 服務和 Web 服務客戶端
這是 log4j.xml 文件的內容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<? xml version = "1.0" encoding = "UTF-8" ?> < ! DOCTYPE log4j : configuration SYSTEM "log4j.dtd" > < log4j : configuration xmlns : log4j = "http://jakarta.apache.org/log4j/" debug = "false" > < ! -- FILE Appender -- > < appender name = "FILE" class = "org.apache.log4j.FileAppender" > < param name = "File" value = "c:/crunchify.log" / > < param name = "Append" value = "false" / > < layout class = "org.apache.log4j.PatternLayout" > < param name = "ConversionPattern" value = "%t %-5p %c - %m%n" / > < / layout > < / appender > < ! -- CONSOLE Appender -- > < appender name = "CONSOLE" class = "org.apache.log4j.ConsoleAppender" > < layout class = "org.apache.log4j.PatternLayout" > < param name = "ConversionPattern" value = "%d{ISO8601} %-5p [%c{1}] %m%n" / > < / layout > < / appender > < ! -- Limit Category and Specify Priority -- > < category name = "com.crunchify" > < priority value = "CRUNCHIFY" class = "com.crunchify.tutorials.CrunchifyLog4jLevel" / > < appender - ref ref = "CONSOLE" / > < / category > < ! -- Setup the Root category -- > < root > < appender - ref ref = "CONSOLE" / > < / root > < / log4j : configuration > |
CrunchifyLog4jLevelTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com . crunchify . tutorials ; import org . apache . log4j . Level ; import org . apache . log4j . Logger ; import com . crunchify . tutorials . CrunchifyLog4jLevel ; /** * Tests whether the new log level * com.crunchify.tutorials.CrunchifyLog4jLevel#CRUNCHIFY} is working * * @author Crunchify.com * */ public class CrunchifyLog4jLevelTest { public static void main ( String [ ] args ) { Logger logger = Logger . getLogger ( CrunchifyLog4jLevelTest . class ) ; logger . log ( CrunchifyLog4jLevel . CRUNCHIFY , "I am CrunchifyLog4jLevelTest log" ) ; logger . log ( Level . DEBUG , "I am a DEBUG message" ) ; } } |
運行測試程序,您應該會在Eclipse's Console
中看到以下類型的結果。
1 2 |
2013 - 08 - 01 15 : 22 : 36 , 758 CRUNCHIFY [ CrunchifyLog4jLevelTest ] I am CrunchifyLog4jLevelTest log 2013 - 08 - 01 15 : 22 : 36 , 758 DEBUG [ CrunchifyLog4jLevelTest ] I am a DEBUG message |
您為日誌級別指定的 int 值很重要。 這裡我定義“ CRUNCHIFY
”日誌級別要高於DEBUG
級別但低於 log4j 提供的TRACE
級別。
因此,只要您在類別上(在log4j.xml
文件中)將優先級設置為 DEBUG, CRUNCHIFY
級別的日誌就不會進入日誌文件。