Log4j - priority value and param name concept expl

2020-01-31 03:00发布

问题:

My 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>

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

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".

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

Cases :

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

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

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

回答1:

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

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.

A configuration like this:

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

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

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".

A configuration like:

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

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

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

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

As for your two examples:

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

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

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 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>

If you run code like:

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");

you get this in sysAdminLogs.log:

some error statement
some fatal statement

and this in 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 logging