在 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
级别的日志就不会进入日志文件。