Tuesday, March 10, 2009

Log4J configuration – controlling logging to multiple loggers

I recently happened to read a blog about Apache Lo44J java logging API where the blogger mentioned that there is no clean good document/tutorial about using log4j, considering the fact that it is one of the most widely used open source java API. I felt that it is true to some extent, when I had to look for some help regarding a logging issue I encountered. Especially, I could not find one authentic manual/tutorial explaining the log4j configuration.


My requirement was to control the logging of messages to multiple appenders (targets) with different log priority. For example, I wanted to a log message to write log only to file if priority is debug but wanted to write to both console and log file if priority is set to info or above. With the help of some blogs and log4j javadocs, I found ways to do this. In this blog, I am explaining what I understood, assuming that it might help others looking for this info.


I used XML configuration for configuring log4j in my project, so I will use same in this article. I assume that you (reader of this blog) have basic understanding of Log4J configuration, if not please read this short manual at apache. I expect you to understand Log4J terms like Logger, Log Level and the XML tags like <category>, <appender>, <layout> etc., those appear in log4j configuration.


Note: wherever I mention “logger” (starting with lower case l), I referelement <category> in log4j configuration xml. I mean <root> wherever I mention root logger and tag <appender> wherever I mention “appender”.


As I mentioned above, my requirement was to log messages to multiple appenders (targets), console and a file, But I wanted to all messages (any level – level DEBUG or above) logged to file and only messages with log level ERROR or higher to the console. Following is my log4j.xml


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="TestLogFile.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>

<param name="Append" value="true"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<category name="com.ibswings">

<priority value="debug"/>

<appender-ref ref="FILE"/>

</category>


<root>

<priority value="error"/>

<appender-ref ref="CONSOLE"/>

</root>

</log4j:configuration>


A simple class I used to test this:


package com.ibswings.loggertest;


import org.apache.log4j.Logger;


public class Test {


public static void main(String[] args) {

new Test();

}


public Test() {

Logger logger = Logger.getLogger(getClass());

logger.debug("Debug Message");

logger.info("Info Message");

logger.error("Error Message");

}

}


I was expecting this to print all three messages from code to be printed to the log TestLogFile.log and only error message to be printed to console, since I have set ‘error’ priority in <root>. However I noticed that all messages are printed to both log file and console like:


2009-03-07 19:32:00,893 DEBUG Test.java:14 - Debug Message

2009-03-07 19:32:00,893 INFO Test.java:15 - Info Message

2009-03-07 19:32:00,893 ERROR Test.java:16 - Error Message


Let’s look into this in detail. By default, when a message is logged, the message will go to the first logger () whose names closely matches the name of the Logger instance you created in Java code. From there, it is directed to the next matching logger up in the logger hierarchy until root logger. In each of these loggers, output is sent to all appenders indentified by definitions. In the above example, I had created logger in my code like:

Logger logger = Logger.getLogger(getClass());


Actually, this is equivalent of

Logger logger = Logger.getLogger(“com.ibswings.loggertest.Test”);


When following line is executed,

logger.debug("Debug Message");


The closest matching logger defined in config is <category name="com.ibswings". This logs output to TestLogFile.log through appender named “FILE” (Note that if I had a logger defined in log4j.xml with name "com.ibswings.loggertest", that would have been the first logger to get the message). Message directed to root logger from here, since there is no other logger matching the name in between. And, message is printed out to standard out by CONSOLE appender. Also, when message is logged by root, it is logged with priority of message it received it from previous logger. Hence all messages are printed to console in this case, even though we have set error priority in root. The priority set in root has two uses – first, it is the default priority of all other loggers without priorities set. Second, all messages which are only received by root logger (i.e. messages with no matching loggers in log4j.xml) will have this default priority enabled. In following section, let’s see how we can avoid duplicating messages in both log file and console.


How to log only my messages to log file and others to console?


As I mentioned above, output from each logger is directed to next logger above in the logger hierarchy, till root logger. To set behavior off, we need use “addictivity” attribute in . If addictivity is set to “false” in a particular logger, output will not be sent to next logger up in the hierarchy. Or, output is sent from logger to logger, until it reaches a logger (of course, before root) in which addictivity is set to false. By default (when not specified) addivity is set to true. That’s how in the previous case messages are printed to both file and console. Let’s use this in out log4j.xml to stop messages from printing in console.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="TestLogFile.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>

<param name="Append" value="true"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<category name="com.ibswings" additivity="false">

<priority value="debug"/>

<appender-ref ref="FILE"/>

</category>


<root>

<priority value="error"/>

<appender-ref ref="CONSOLE"/>

</root>

</log4j:configuration>


With this log4j.xml, all messages from the code are logged to file only. Since we set additivity="false" in the first logger, output is not directed to root logger. In this case, other Logger instances with name not matching “com.ibswings” and any System.out.printxx() calls go to console. In the following section, let’s see how to send all messages from our java code to file and some selected messages (based on priority) to console.


How to log all my messages to log file and selected (based on priority) my messages to console?


Coming to my original requirement, I want to send all my messages (printed from my java code) to log file. Also, I want to send all error messages from my code to console also. At the same time, I want to see all other messages (from other Logger instances and SOP calls) in console.


To achieve this, let’s assume we add appender-ref to console in our first logger. Now question is how to send only error messages, since whatever priority we set to in logger is applicable to all appenders. The solution is to use “Threshold” parameter in appender definition. If we specify a particular priority in appender using threshold, only messages with priority equal of higher to priority specified in ‘threshold’ are printed to the target. However, in this case, we cannot set ‘threshold’ to error in CONSOLE appender, since we need to print all messages (any level) not covered by our logger to console, in root logger. Let’s create a new appender CONSOLE_1 to log messages to console. The configuration is given below.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<appender name="CONSOLE_1" class="org.apache.log4j.ConsoleAppender">

<param name="Threshold" value="ERROR"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="TestLogFile.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>

<param name="Append" value="true"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<category name="com.ibswings" additivity="false">

<priority value="debug"/>

<appender-ref ref="FILE"/>

<appender-ref ref="CONSOLE_1"/>

</category>


<root>

<priority value="error"/>

<appender-ref ref="CONSOLE"/>

</root>

</log4j:configuration>


When I ran my test, I see all message logged to file and error message printed in console. Also, all other messages with any level and SOPs printed to console, since we have appender CONSOLE referenced in root logger. In following section, let’s see how to all my messages and all other error messages, only to log file. Following is the log4j.xml we can use to achieve this.


How to log all my messages and all other error messages, only to log file?


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="TestLogFile.log"/>

<param name="DatePattern" value="'.'yyyy-MM-dd"/>

<param name="Append" value="true"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>

</layout>

</appender>


<category name="com.ibswings" additivity="false">

<priority value="debug"/>

<appender-ref ref="FILE"/>

</category>


<root>

<priority value="error"/>

<appender-ref ref="FILE"/>

<!--

<appender-ref ref="CONSOLE"/>

-->

</root>

</log4j:configuration>


Note that I have commented appender-ref CONSOLE in root logger. With this log4j.xml, all messages logged from my java code are printed to log file. At the same time any other message with priority error is also printed to log file. All these will be logged only to the log file, not to console. However, we will still see SOPs in the logger (if we use this in an Application server, we will see lot of such messages in console/standard out or error log files).


45 comments:

YO said...

Thanks buddy..its really very helpful..

Bharath
hyderabad
abbhooshan@gmail.com

larin said...

Thanks just what I was looking for.

Santosh said...

This was useful..Thanks

Unknown said...

Your blog is excellent and its pretty useful. Please keep updating the site.
Thanks for sharing the information.

Yashu said...

It was really good and concise explanation of log4j

Anonymous said...

Thanks a lot. This clarified a lot about the log4j config (which is not very intuitive at all, for example why are loggers called categories instead of...uhm, let's see....loggers?). Anyways, your investigation saved me a lot of time.

Senthil Kumar Sekar said...

Thanks a lot for a valuable information on Log4j.

This would make anyone to understand Log4j - If they hadnt had a clear picture on what it is.....

Anonymous said...

Really Cool. Thank You

Sir Ravindra Jadeja said...

Not Good from a project perspective.

Sir Ravindra Jadeja
Bowls Wicket to Wicket

Roger said...

Thanks for the valuable information.

I have a requirement where in i have 10 log statements in a java class file. I need to log first 5 log messages into a log file and the other 5 log messages into another log file. Is it possible to do this using log4j?? Can you give me an example to do this if it is possible.

hallsarah204@gmail.com said...

I didn't know a lot about how to log only my messages to log file and others to console... Thank you for sharing! This is a very brief yet detailed entry! Hopefully, paper-writing.services helped you to create an article!

ganga said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.Block Chain Training in bangalore

ganga sri said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Block Chain Training in Bangalore

Unknown said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.


rpa training in Chennai | rpa training in velachery

rpa training in tambaram | rpa training in sholinganallur

chitra pragya said...

After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.

angularjs Training in chennai
angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

Unknown said...

Pleasant Tips..Thanks for Sharing….We keep up hands-on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
Data Science training in rajaji nagar | Data Science with Python training in chenni
Data Science training in electronic city | Data Science training in USA
Data science training in pune | Data science training in kalyan nagar

Mounika said...

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
python training in tambaram
python training in annanagar
python training in jayanagar

sai said...

I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
Python training in marathahalli | Python training institute in pune

Unknown said...

Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
Java training in Marathahalli | Java training in Btm layout

Java training in Jaya nagar | Java training in Electronic city

shalinipriya said...

Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in

Data Science training in kalyan nagar | Data Science training in OMR

Data Science training in chennai | Data science training in velachery

Data science training in tambaram | Data science training in jaya nagar

SAM THOMAS CHRIST said...

Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.



AWS Training in Bangalore | Amazon Web Services Training in Bangalore

AWS Interview Questions And Answers

Learn Amazon Web Services Tutorial |AWS Tutorials For Beginners

Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai


AWS Training in Chennai |Best Amazon Web Services Training in Chennai


AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli


AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

AWS Training in Chennai |Best Amazon Web Services Training in Chennai

rohini said...

I greatly appreciate your posting.
apple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai

Rajan said...

Hand towels Rolls
Professional Cleaning Chemical
Buy bulk tissues
Nitrile gloves bulk in birmingham

Rajesh said...

Thanks for sharing informatic article
informatica Training in BTM
Blue Prism Training in BTM
MERN Stack Training in BTM
MEAN Stack Training in BTM
RPA Training in BTM
Qlikview Training in BTM
Qlik Sense Training in BTM
Machine Learning training in btm

Rajesh said...

thanks for sharing this information
data science training in bangalore
data science classroom training in bangalore
best training institute for data science in bangalore
best data science training institute in bangalore
data science with python training in bangalore
best data science training in bangalore
tableau training in bangalore
best tableau training institutes in bangalore
tableau classroom training in bangalore

Anonymous said...

For Blockchain training in Bangalore, Visit:
Blockchain training in Bangalore

Rajesh Anbu said...

Thank you for sharing information. Wonderful blog & good post.
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Anonymous said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post
360digitmg provides Tableau Certification Program provides an in-depth study of Tableau architecture and the Tableau Product Suite. The multifarious facets of Tableau Desktop like charts, tables, maps, functions, filters, hierarchies, joins, unions, groups, parameters, calculated fields, and Tooltips are explained vividly with relevant examples.
360digitmg provides Tableau Certification Program

sherlie said...

Nice blog..! I really loved reading through this article. Thanks for sharing.You done a great job. Interesting.
Web Designing Training Course in Chennai | Certification | Online Training Course | Web Designing Training Course in Bangalore | Certification | Online Training Course | Web Designing Training Course in Hyderabad | Certification | Online Training Course | Web Designing Training Course in Coimbatore | Certification | Online Training Course | Web Designing Training Course in Online | Certification | Online Training Course

Sukrita Singh said...
This comment has been removed by the author.
ram said...

It was wonderfull reading your article. Great writing styleIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

Thakur98 said...

We are used to the fact that we know only religious and public holidays and celebrate only them.Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder

Himachali Khabarnama said...

Annabelle loves to write and has been doing so for many years.Backlink Indexer My GPL Store Teckum-All about Knowledge

Tec said...

NDUW is a labor registration portal created by the Labor Department, Government of india. The registration of the unorganized workers (working class) of the state takes place on this portal.

subham said...

All type services start in Bihar Govt go to see this services and help for filling form RTPS Bihar

Himachal News Network said...

Welcome to CapturedCurrentNews – Latest & Breaking India News 2021
Hello Friends My Name Anthony Morris.latest and breaking news drupepower.com

Back linker said...

Serial key for Microsoft Office 2019 Professional Plus can be found and viewed here. We have the largest serial numbers data base. Office 2019 Professional Plus Crack

Inventateq said...


Very informative Blog by you for the people who are new to this industry. Your detailed blog solves all the queries with good explanation. Keep up the good work. Thanks for sharing! Please check out our courses of hadoop by inventateq.

digital marketing course in bangalore

digital marketing training in bangalore

Harshita Creation said...

Very informative Blog by you for the people who are new to this industry. Your detailed blog solves all the queries with good explanation. Keep up the good work. Thanks for sharing! We have a website too. Feel free to visit anytime.

western dresses online

stylish party wear western dresses

Krishna Packer Movers said...

Very informative Blog by you for the people who are new to this industry. Your detailed blog solves all the queries with good explanation. Keep up the good work. Thanks for sharing! We have a website too. Feel free to visit anytime.

packers and movers in Ghansoli

packers and movers in Malad

Invite Mart said...

I am exteremly impressed by your blog, because its very powerful for the new readers and have lot of information with proper explanation. Keep up the good work. Thanks for sharing this wonderful blog! We also have a website. Please check out whenever and wherever you see this comment.

Ring ceremony invitation card

wedding card design online

Quick Convert said...

Your blog has a lot of material with clear explanations and is really effective for new readers, therefore I'm very impressed. Keep up the excellent work. Thank you for distributing this fantastic blog! Moreover, we have a website. When and whenever you see this comment, please check it out.

MP4 File Converter

Quick Convert

Ava Volkov said...

Your ability to give a balanced viewpoint that takes into account other viewpoints and addresses potential problems stands out above all else. Splunk Course

Ava Volkov said...

You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. Splunk Training

Ava Volkov said...

You have recently made some very outstanding contributions. Your breadth of knowledge, astute analysis, and lucidity of expression are admirable. Your knowledge enriches our conversations and offers priceless direction. Splunk Certification