Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j - priority value and param name concept explanation

Tags:

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?

like image 721
VJS Avatar asked Dec 28 '11 08:12

VJS


People also ask

What is priority in log4j?

Log4j Level Order/PriorityTrace is of the lowest priority and Fatal is having highest priority. Below is the log4j logging level order. Trace < Debug < Info < Warn < Error < Fatal. When we define logger level, anything having higher priority logs are also getting printed.

What are the three most important components of log4j?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.

What is logger name in log4j XML?

In log4j a logger is associated with a package or sometimes with a particular class. Package/class of a logger is defined by the attribute "name". A logger logs messages in its package and also in all the child packages and their classes.


1 Answers

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.

like image 125
Bogdan Avatar answered Dec 13 '22 15:12

Bogdan