In each and every applications we will be seen logger to record the different types of logs like error, information, debug etc., In this tutorial we will see how to use Logger log4j API in our application and its usage.
There are mainly 3 components like loggers, appenders and layouts. All these 3 components work together to enable developers to log messages according to message type and level and to control at run-time as how these messages are formatted and where they are stored. All these appender or logger settings will be made in the file called log4j.properties file.
The advantage of using logger over System.out.print is to disable certain log statements while allowing others to print unhindered. This capability assumes that the logging space, that is, the space of all possible logging statements, is categorized according to some developer-chosen criteria. Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule ie., "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." as example given below
log4j.rootCategory=DEBUG, CONSOLE, TESTING
log4j.logger.org.apache.commons.validator=DEBUG, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=WARN
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %-5p %m%n
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/user/local/logFile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%15.15t] %-5p %c %x - %m%n
log4j.appender.TESTING=org.apache.log4j.RollingFileAppender
log4j.appender.TESTING.File=/user/local/application.log
log4j.appender.TESTING.Append=true
log4j.appender.TESTING.MaxFileSize=1024KB
log4j.appender.TESTING.MaxBackupIndex=10
log4j.appender.TESTING.layout=org.apache.log4j.PatternLayout
log4j.appender.TESTING.layout.ConversionPattern=[%d{EEE MMM d HH:mm:ss yyyy}] [%-5p] %c{1}.java(): %m%n
Set of possible logger levels are:
TRACE,
DEBUG,
INFO,
WARN,
ERROR and
FATAL
For more appenders and how to inclide in our project we can get it from the following link - http://logging.apache.org/log4j/1.2/manual.html
Now lets see simple example for how to start with the log4j API in our application with simple example to redirect all logs to a specified log file. For that first we need log4j.jar file (can use any latest version). I have used log4j-1.2.13.jar and we need to create log4j.properties file under classes directory.
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\application.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
import java.util.Date;
import org.apache.log4j.Logger;
public class LogTest {
static Logger logger = Logger.getLogger(LogTest.class.getName());
public static void main(String[] args) {
logger.info("Application starts .....................");
long stime = new Date().getTime();
try{
logger.info("Process Started !!!");
Thread.sleep(5300);
logger.info("Process completed !!!");
}catch (Exception e) {
logger.error("Exception Occured : "+e.getMessage());
}
long etime = new Date().getTime();
float diff = (float)(etime - stime)/1000;
logger.info("Total time to complete ::: "+ diff +" second(s)");
logger.info("Application Ends .......................");
}
}
OUTPUT:
No comments:
Write comments