如何在log4j中添加自定义日志记录级别 [英] how to add custom logging level in log4j

查看:99
本文介绍了如何在log4j中添加自定义日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的log4j.xml文件添加自定义日志记录级别安全性,并且此日志级别应高于debug且低于级别trace.How如何在log4j.xml文件中配置自定义日志记录级别以及何时使用级别debug在log4j.xml文件的根元素中,不应将日志级别的安全性打印在日志文件中,而如果我在log4j.xml文件的根元素中使用自定义级别的安全性,则应打印所有级别的安全性,但不能跟踪.

I want to add custom logging level security to my log4j.xml file and this log level should be above debug and less than level trace.How to configure the custom logging level in log4j.xml file and when i use the level debug in root element of log4j.xml file the log level security should not be printed in log file wwhereas if i use custom level security in root element of log4j.xml file all the levels up security should be printed but not trace.

推荐答案

Log4J 2支持自定义日志级别.可以通过代码或配置来定义自定义日志级别.

Log4J 2 supports custom log levels. Custom log levels can be defined in code or in configuration.

要在配置(log4j.xml)中创建自定义日志级别,请使用以下示例代码段

To create custom log levels in configuration (log4j.xml), use the following example snippet,

<CustomLevels>
      <CustomLevel name="CUSTOM" intLevel="350" />
</CustomLevels>

intLevel参数(在上面的示例中为350)确定自定义级别相对于Log4J 2内置的标准级别的位置.

The intLevel parameter (350 in the example above) determines where the custom level exists in relation to the standard levels built-in to Log4J 2.

就您而言,由于您需要高于DEBUG且低于TRACE的日志级别,因此请在log4j.xml中使用以下代码段

In your case, since you need a log level above DEBUG and less than TRACE, use the following code snippet in log4j.xml

<CustomLevels>
      <CustomLevel name="CUSTOM" intLevel="550" />
</CustomLevels>

由于 DEBUG 的 intLevel 为 500,TRACE 为 600(根据 Log4J 2),我们将自定义日志级别的 intLevel 设置为 550.

Since the intLevel of DEBUG is 500 and TRACE is 600 (according to Log4J 2), we have set the intLevel for the custom log level as 550.

有关更多信息,请参见以下链接,

For more information, refer the following link,

https://logging.apache.org/log4j/2.x/manual/customloglevels.html

这篇关于如何在log4j中添加自定义日志记录级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆