Log4J is a logging tool used in the production environment. It used to find success messages ,information , warnings, errors in application while using it.Log4J used in the production environment.
By default Messages/errors printed in the console ,these are temporary location.
To see all problems from beginning to till date use Log4J.
Log4J can write errors to File, Database, Email, Network etc.Log4J provides Error details like Exception type, class an method with line numbers it occurred, Date ,Time and other information as well.
The object must created inside of a class as a instance variable.It is used to enable Log4J service to the current class.If we are not creating Logger object we can apply the Log4J concept.
There are 5 priority methods
Order method Name
1 debug(obj) DEBUG
2 info(obj) INFO
3 warn(obj) WARN
4 error(obj) ERROR
5 fatal(obj) FATAL
This method used to print a final/success message.
Ex:ID reated successfully etc..
Print current state I the execution
Ex: If block executed,Mail sent etc..
Used to print warnings in application
Ex: local variables not used, resource not closed
Used to print Exceptions like NullPointer,ArrayIndex,SQLException etc..
This method used to print any high level exceptions
Appender provide details like, where to print message? Where to store log messages?
· Database Appender
store messages in log file.
store log messages in the db table
send messages from one server to another
send log messages using a mail
It provides format of the message to be printed.possible layouts are
· Simple Layout
· HTML Layout
· XML Layout
· Pattern Layout
In this Example
First I created logger object for the class.
Then created object for the Appender class. In the Appender class mention what type message to be printed. Here by using INFO simple message printing in the Console.So we have to use ConsoleAppender to see messages.To see this message creating PatternLayout with date and time format.