Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Apache Log4j >>> Log4j Example Configuration >>> Applying Levels Logger
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
How to use Log4j
Description : PatternLayout
More...


Written By : Amit
Title :
Using NDC and Log4j
Description : NDC Push with Log4j
More...


Written By : Amit
Title :
Log4j Example
Description : Log4j Tutorial
More...


Written By : guddu
Title :
Apache Commons Logging
Description : with Log4j Example
More...


Written By : ISHTEK
Title :
Log4j Example Configuration
Description : Applying Levels Logger
More...

Tags/Keywords : Log4j Example,Log4j Consoleppender, Log4j article, Example, Log4j, Code
Author : ISHTEK
Date (Year/Month/Date): 2010-07-07 Using Log4j Example : A case study discussed

Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways  for doing some sort of example
and may not be fit for any other purpose.

In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.


Log4j Example : Using various levels of logging using Apache Log4j Suppose there is a log file to be analyzed for certain issues while tracing activity is performed. This activity can be real difficult if one has to find/look for a pin from a box full of sand, type of situation. This may happen if number of tracing / logging lines of statements ranges from few hundred to thousand lines for a real big application with many classes being executed with an operation. So what do you think can be possible way to simplify this particular problem?? In my thinking if there could be some way to filter only those log statements, those are related to one particular class or component in hand. Yes, You are right!!! Apache Log4j has a provision for defining which class to be considered for logging and leaving rest of the classes not eligible for logging purpose. In this example I shall try to demonstrate this objective of configuring log4j.properties file in such a way so as to provide a filter kind of functionality and this should be done without any code change as far as the Java class files are concerned. So this is pretty configuration time setting up activity. In order to show a method execution flow, I have defined three dummy class files, such as ExampleController, ExampleService, ExampleDAO with some dummy kind of methods too. demo.example.ExampleController.java
package demo.example;

import org.apache.log4j.Logger;

public class ExampleController {
  private static Logger logger = Logger.getLogger(ExampleController.class);
  public void delegateCall() {
	logger.info("Entering delegateMethod");
	new ExampleService().doBusiness();  
	logger.info("Leaving delegateMethod");
  }
}
demo.example.ExampleService.java
package demo.example;

import org.apache.log4j.Logger;

public class ExampleService {
  private static Logger logger = Logger.getLogger(ExampleService.class);	
  public void doBusiness() {
    logger.info("Entering doBusiness");
    new ExampleDAO().doStore();  
    logger.info("Leaving doBusiness");

  }
}
demo.example.ExampleDAO.java
package demo.example;

import org.apache.log4j.Logger;

public class ExampleDAO {
   private static Logger logger = Logger.getLogger(ExampleDAO.class);	
   public void doStore() {
    logger.info("Entering doStore");
    logger.debug("TODO database call");  
    logger.info("Leaving doStore");
   }
}
As these files shows that ExampleController.delegateCall method is calling ExampleService.doBusiness and this method is again calling ExampleDAO.doStore, so there is a chain of method execution with some logging statements being coded. Now a very common (I think so) log4j properties file for this example as follows:
log4j.rootLogger=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This log4j.properties file prints out all the logging statements that Log4j encounters while these method execution, as shown below:
[main] INFO  demo.example.ExampleController - Entering delegateMethod
[main] INFO  demo.example.ExampleService - Entering doBusiness
[main] INFO  demo.example.ExampleDAO - Entering doStore
[main] INFO  demo.example.ExampleDAO - Leaving doStore
[main] INFO  demo.example.ExampleService - Leaving doBusiness
[main] INFO  demo.example.ExampleController - Leaving delegateMethod
Now I have modified log4j.properties file and added the line marked as bold RED in color
log4j.rootLogger=FATAL
log4j.logger.demo.example=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes no difference so far as the number of logging statements on console, for this example is concerned : But the real difference appeared when I added the class file name within the second line of log4j properties file as follows:
log4j.rootLogger=FATAL
log4j.logger.demo.example.ExampleController=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes the log statement being printed on console, restricted only to the demo.example.ExampleController class file, as show below:
[main] INFO  demo.example.ExampleController - Entering delegateMethod
[main] INFO  demo.example.ExampleController - Leaving delegateMethod
By this way one can apply some filter/restrictions (Just of understanding) and log only those statements those are real important to track.
Author of this article/writeup has expressed his/her willingness
to help or guide users with any technical difficulties he/she faces while working with the example code environment setting up, running and resolving any such exception raised during compile or at runtime. You may ask for any technical doubt or seek technical help related to this article by using following form to reach for technical help from the Author for FREE. This article's Author shall be reading your request and responding within reasonable time (no resolution timeframe defined as such).


	
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
14,5
Enter bigger number from above :*
Home >>> Apache Log4j >>> Log4j Example Configuration >>> Applying Levels Logger
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
19,12
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER
 

Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners

This web site's Terms of Use and Disclaimer :

This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.

By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.

All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.