本文介绍了Log4j - 优先级值和参数名概念解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的log4j.xml:

 <appender name="B2BAPP" class="org.apache.log4j.RollingFileAppender">
     <param name="File" value="/LOGS/SAM/B2B_VJ.log"/>
     <param name="Threshold" value="ERROR"/>
     <param name="MaxFileSize" value="10000KB"/>
     <param name="MaxBackupIndex" value="10"/>
     <param name="Append" value="false"/>
     <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss,SSS} %5p [%c:%L] %m%n"/>
     </layout>
</appender>

<logger name="com.sas">
   <priority value="DEBUG"/>
   <appender-ref ref="B2BAPP"/>
</logger>

我想了解 priority value="DEBUG"param name="Threshold" value="DEBUG" 的行为.

I would like to understand the behaviour of priority value="DEBUG" and param name="Threshold" value="DEBUG".

在我的记录器 (com.sas) 中,我设置了优先级值DEBUG",该记录器的 appender 是B2BAPP",在B2BAPP"中,我将阈值"定义为错误"".

In my logger (com.sas) I have set the priority value "DEBUG" and appender of this logger is "B2BAPP" and in "B2BAPP" I have defined "Threshold" as "ERROR".

那么com.sas"的日志级别会设置为DEBUG"还是ERROR"?

So log level for "com.sas" would be set to "DEBUG" or "ERROR"?

案例:

priority value="DEBUG" and param name="Threshold" value="ERROR"

priority value="ERROR" and param name="Threshold" value="DEBUG"

priority value="ERROR" and param name="Threshold" value="DEBUG"

以上案例的输出是什么?它是如何工作的?

What would be the output of the above cases? How does it work?

推荐答案

Logger 组件接受日志记录指令 (logger.debug(), logger.error() 等调用)并将它们发送到适当的目的地到 Appenders.

The Logger component accepts logging instructions (logger.debug(), logger.error() etc calls) and sends them to appropriate destinations to the Appenders.

您可以在 Logger 上设置一个优先级",并指示它只接受某个级别的日志记录指令.级别是(按重要性升序排列):TRACE、DEBUG、INFO、WARN、ERROR 和 FATAL.

You can set a "priority" on the Logger and instruct it to accept only logging instructions of a certain level. The levels are (in ascending importance order): TRACE, DEBUG, INFO, WARN, ERROR and FATAL.

这样的配置:

<logger name="com.sas">
    <priority value="WARN" />
    ....
</logger>

指示 com.sas 记录器只接受重要性级别为 WARN 或更高(即 WARN、ERROR 和 FATAL)的级别.

instructs the com.sas logger to only accept levels with a level of importance of WARN or higher (i.e. WARN, ERROR and FATAL).

然后将日志语句发送到 Appenders.附加程序也可以配置为仅接受特定重要性级别的语句,高于特定阈值".

The logging statements are then sent to Appenders. The appenders can also be configured to accept only statements of a certain importance level, one above a certain "threshold".

配置如下:

<appender name="B2BAPP" class="org.apache.log4j.RollingFileAppender">
   <param name="Threshold" value="ERROR"/>
   ....
</appender>

告诉 appender 只接受 ERROR 重要性或更高级别的语句(即 ERROR 和 FATAL).

tells the appender to only accept statements of ERROR importance or above (i.e. ERROR and FATAL).

那么com.sas"的日志级别会设置为DEBUG"还是ERROR"?

在您的示例中,日志级别设置为调试.附加程序编写的内容与问题正交.

In your example the log level is set to DEBUG. What gets written by the appenders is orthogonal to the issue.

至于你的两个例子:

priority value="DEBUG" and param name="Threshold" value="ERROR"

priority value="ERROR" and param name="Threshold" value="DEBUG"

priority value="ERROR" and param name="Threshold" value="DEBUG"

1. Logger 优先级设置为 DEBUG,appender 阈值设置为 ERROR 意味着记录器传递 DEBUG、INFO、WARN、ERROR 和 FATAL,但 appender 只接受 ERROR 和 FATAL,所以你只得到 ERROR和 FATAL 写入您的日志.

1. Logger priority set to DEBUG and appender threshold set to ERROR means that the logger passes along DEBUG, INFO, WARN, ERROR and FATAL but appender only accepts ERROR and FATAL so you get only ERROR and FATAL into your log.

2. Logger 优先级设置为 ERROR,appender 阈值设置为 DEBUG 意味着 logger 只传递 ERROR 和 FATAL,而 appender 接受 DEBUG、INFO、WARN、ERROR 和 FATAL.您的日志中再次仅显示 ERROR 和 FATAL.

2. Logger priority set to ERROR and appender threshold set to DEBUG means that the logger passes along only ERROR and FATAL while the appender accepts DEBUG, INFO, WARN, ERROR and FATAL. You again get only ERROR and FATAL into your log.

但这只是一个不幸的案例.混合优先级和阈值可以为您提供一些不错的功能.例如...

But that's just an unfortunate case. Mixing the priority and the threshold can get you some nice functionality. For example...

... 假设您刚刚将应用程序置于暂存状态,并且您需要对其进行一段时间的监视,直到将其移至生产环境.您有一名开发人员和一名系统管理员进行监控.开发者想要所有的日志,而系统管理员很忙,只想看到错误.

... assume you just placed an application in staging and you need to monitor it for a bit until you move it to production. You have a developer and a system administrator doing the monitoring. While the developer want all the logs, the system administrator is busy and only wants to see the errors.

你是如何配置的?这样的事情怎么样:

How do you configure that? How about something like this:

<appender name="developerLogs" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="/LOGS/SAM/developerLogs.log" />
    <param name="Threshold" value="DEBUG" />
    <param name="Append" value="false" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
</appender>

<appender name="sysAdminLogs" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="/LOGS/SAM/sysAdminLogs.log" />
    <param name="Threshold" value="ERROR" />
    <param name="Append" value="false" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
</appender>

<logger name="com.test">
    <priority value="DEBUG" />
    <appender-ref ref="developerLogs" />
    <appender-ref ref="sysAdminLogs" />
</logger>

如果您运行如下代码:

Logger logger = Logger.getLogger("com.test");

logger.debug("some debug statement");
logger.info("some info statement");
logger.warn("some warn statement");
logger.error("some error statement");
logger.fatal("some fatal statement");

你在 sysAdminLogs.log 中得到这个:

some error statement
some fatal statement

developerLogs.log 中:

some debug statement
some info statement
some warn statement
some error statement
some fatal statement

希望这个解释能更好地描述概念.

Hope this explanation better describes the concepts.

这篇关于Log4j - 优先级值和参数名概念解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:50