javapandit.net
  • Home
  • Quick Java
    • Exception Handling
    • Collections Framework
  • Java Best Practices
  • Web Services
    • Web Service Basics
    • Ten Basic webservice concepts
    • XML
    • Apache Axis
    • Restful Web Services
  • JMS Concepts
    • JMS- MySQL
  • Hadoop
    • NoSQL DATABASEs
    • Apache Sqoop
    • Hadoop Interview Questions
  • Java 5
  • Java 8
    • Java 8 : Lambda Expressions
  • JDBC
  • Java Architect
    • Enterprise application re-platforming strategies
    • Java Memory Management
  • Java Programs
  • Technical Tips
    • How to set JAVA_HOME environment variable
    • How to create an auto increment field in Oracle?
    • Linux Commands
  • Best Java Interview Questions
    • Java Interview Questions- YouTube
  • Interview Questions
    • Java Tech interview Questions
    • Core Java Interview Questions >
      • core tech questions1
      • Java Collection interview questions
      • Java Concurrency
    • Servlets Interview Questions
    • JSP Interview Questions
    • Java Web Services Interview Questions
    • EJB Interview Questions
    • XML Interview Questions
    • JMS Interview Questions
  • Struts Interview Questions
    • Struts 2 Interview Questions
  • Java EE Architects Interview Questions
    • Java Architect Interview Questions
    • Top 10 reasons for Java Enterprise Application Performance Problems
    • Web Application Scalability Questions for IT Architect
  • JavaPandit's Blog
  • Web Services Interview Questions
  • Servlets And JSP
  • Oracle SOA Interview Questions
    • Open ESB /JBI
    • BPEL Language
  • Log4J
  • Ant
  • Maven
  • JMeter
  • JUnit
  • Apache POI Framework
  • ORCALE SERVICE BUS (OSB) Interview Questions
  • J2EE Patterns
    • Model-View-Controller (MVC)
    • Front Controller
    • DAO
    • Business Delegate
    • Session Facade
    • Service Locator
    • Transfer Object
    • Design Patterns >
      • Creational Patterns >
        • Singleton
      • Behavioural Patterns
      • Structural Patterns
    • Intercepting Filter
  • SQL Interview Questions/Lab
  • Best Wall Papers
    • Devotional Songs
  • Java Community
  • HIBERNATE
  • ORACLE CC&B
    • Oracle CC&B Interview Questions
  • Docker
  • Little Princess
    • Sai Tanvi Naming Ceremony Celebrations
    • Rice Feeding Ceremony
    • Sai Tanvi Gallery
  • APPSC Career Guidance
    • AP History
    • Indian Polity
    • Indian Economy
    • Science & Technology
    • Mental Ability and Reasoning
    • Disaster Management
    • Current Affairs and Events
    • General Sciences >
      • Biology
      • Physics
      • Chemistry
    • Previous Question Papers
  • About Us
  • Contact US

Java Logging with log4J  Framework

_

IInstallation : Download Log4J from the Jakarta site. The URL is http://jakarta.apache.org/log4j/docs/download.html. Now, unzip the file. The file of our interest is dist\lib\log4j-1.2.7.jar. Place this file in the classpath.

About Log4J : Inserting log statements into code is a low-tech method for debugging it. Logging does have its drawbacks. It can slow down an application. To alleviate these concerns, log4j is designed to be reliable, fast and extensible. The first and foremost advantage of any logging API over plain System.out.println resides in its ability to disable certain log statements while allowing others to print unhindered.

Logger : Log4j uses a class "Logger". Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule. A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger. For example, the logger named "com.foo" is a parent of the logger named "com.foo.Bar". Similarly, "java" is a parent of "java.util" and an ancestor of "java.util.Vector".
The root logger resides at the top of the logger hierarchy. It is exceptional in two ways:
- it always exists,
- it cannot be retrieved by name.
Invoking the static Logger.getRootLogger() method retrieves the root logger. All other loggers are instantiated and retrieved with the static Logger.getLogger() method. This method takes the name of the desired logger as a parameter. The methods of the Logger class that are used to obtain a logger are - * public static Logger getLogger(String name)
Retrieve a logger by name.
* public static Logger getLogger(Class clazz)
Same as calling getLogger(clazz.getName()).


Levels : Loggers may be assigned levels. There are some built-in levels defined in the org.apache.log4j.Level class. They are DEBUG : designates fine-grained informational events that are most
useful to debug an application.
INFO : designates informational messages that highlight the
progress of the application at coarse-grained level.
WARN : designates potentially harmful situations.
ERROR : designates error events that might still allow the
application to continue running.
FATAL : designates very severe error events that will presumably
lead the application to abort.


We may define our own levels by sub-classing the Level class. If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level.

Logging : Logging requests are made by invoking one of the printing methods of a logger instance. These printing methods are defined in the class org.apache.log4j.Logger. The methods are -
  • public void debug(Object message) : Logs a message object with the DEBUG level.
  • public void info(Object message) : Logs a message object with the INFO Level.
  • public void warn(Object message) : Logs a message object with the WARN Level.
  • public void error(Object message) : Logs a message object with the ERROR Level.
  • public void fatal(Object message) : Logs a message object with the FATAL Level.
To write the log for a method, that logging request should be enabled. A logging request is said to be enabled if its level is higher than or equal to the level of its logger. Otherwise, the request is said to be disabled. For the standard levels, the order is -
    DEBUG < INFO < WARN < ERROR < FATAL

Appenders : An output destination is called an appender (where the log is sent). Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously. More than one appender can be attached to a logger.
We can add an appender to a logger by using the method
    public void addAppender(Appender newAppender)
Just like levels, appenders are also inherited by a child from it's parent. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.


Prev 1 2 3 4 Next
Powered by Create your own unique website with customizable templates.