Log4j (Logging For Java):
jdbc Appender
SMTC Appender
Console Appender
File Appender
Rolling File Appender
Daily Rolling File Appender and etc.
Rolling File Appender:
Creates new back up log files based on the max size of log file
Daily Rolling File Appender:
It creates 1 log file per day
(3) Layout object (Formatter)
- Keeping tracking of application flow of execution is called logging and the related messages that are generated are called log messages, there log messages are useful to analyse bugs to fix bugs that are generated in testing phase, MAINTENANCE PHASE of project.
- Using System.out.println() to generate log messages System.out.println() is not recommended to use.
Draw Backs of System.out.println() are:
- Log messages written to console may disappear when screen scrollup.
- It is a single threaded process so can not write multiple log messages at a time.
- Does not allow to categorize log messages.
- Does not allow to Filter log messages while retrieving them.
- Can not write log message file, Database Software log messages and etc.
- Do not deliver the project to the client without log4j
- to overcome above problems we have to use 3rd party Logging API.
T he following Logging API'S
- Assertions (Sun Microsystems)
- Java Logging API (Sun M/s)
- Apache Commons Logging API (Apache)
- Log4j
- JBoss Logging( JBoss)
- SL4j (Simple Facade Logging for Java )(Apache)
Log4j Features:
- Allows to write log message to different destinations (like files, console, Database software etc..)
- Allows to format log messages
- Log4j is industry standard
- Allows to categorize log messages: Total 5 categories.
- Allows to filter log messages while retrieving them.
The 3 Important objects of Log4j:
- Logger object
- Appender object (Handler)
- Layout object (Formatter)
Logger Object:
- Enables logging in the Application on per class basis
- Important object to generate different categories of log messages:
syntax:
Logger logger=Logger.getLogger(com.rajendra.SelectTest.class);
logger.debug("....");
logger.info("..");
logger.warn("...");
logger.error("...");
logger.fatal("...);
- Use Debug level for normal debug messages
- Use "INFO" LEVEL when certain important operations are completed.
- Use "WARN" level when deprecated api code, poor api, code is executed.
- Use "ERROR" level in the catch block of Unchecked Exceptions
- Use "FATAL" LEVEL in the catch blocks Checked Exceptions
.(2) Appender object:
- Specify the destination to record log messages like file, console and etc.
- Log4j api gives built-in Appenders.
Example:
jdbc Appender
SMTC Appender
Console Appender
File Appender
Rolling File Appender
Daily Rolling File Appender and etc.
Rolling File Appender:
Creates new back up log files based on the max size of log file
Daily Rolling File Appender:
It creates 1 log file per day
(3) Layout object (Formatter)
- Allows/Specifies the format and layout of log messages
- Built-in layout classes.
Example:
- HTML Layout
- XML Layout
- PATTERN Layout
- Simple Layout
Putting all Together:
//enables logging
Logger logger=Logger.getLogger(com.ds.SelectTest.class); //here "ds" gives object of java.lang.class //pointing to SelectTest
( Logger class is a Singleton
java class)
//prepare layout.
SimpleLayout layout=new FileAppender("log.txt", true,layout);
//Add appender object to Logger object
logger.addAppender(appender);
//write log messages
logger.debug("....");
logger.info("..");
logger.warn("...");
logger.warn("...");
logger.error("...");
logger.fatal("...);
- to use Log4j api in our applications to generate log messages we need to add log4j<version>/jar file classpath or buildpath. You can download this jar file from www.logging.apache.org (or) from Weblogic installations.
- Log4j support to a project is added during the development of project but the generated log messages are useful to fix the bugs that are generated during project testing and maintanance.
No comments:
Post a Comment