Log4j Hello World Program
|
Log4j »
On Feb 27, 2012 | { 6 Comments }
|
Tweet
|
Let us see one simple program in Log4j
For working with log4j, we must set log4j.jar in our class path
Files Required
- Client.java
- my.txt [ We will let the appender to write into this file ]
Directory Structure
![]() |
![]() |
Client.java
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
public class Client {
static Logger l = Logger.getLogger(Client.class.getName());
public static void main(String[] args) {
Layout l1 = new SimpleLayout();
Appender a;
//Appender a = new ConsoleAppender(l1);
try
{
a = new FileAppender(l1,"my.txt", false);
// 3rd parameter is true by default
// true = Appends the data into my.txt
// false = delete previous data and re-write
l.addAppender(a);
}
catch(Exception e) {}
l.fatal("This is the error message..");
System.out.println("Your logic executed successfully....");
}
}
Once we run this client program, my.txt will contains….
my.txt
FATAL – This is the error message..
Explanation
- First step is to create one Logger class object [ see line number 9 ]
- Second step is to create Layout object [ see line number 13 ]
- Once Layout is ready, our next step is to create Appender [ see line number 18 ]
- In appender i have passed 3 parameters like.. first parameter is object of layout because, appender will write the error messages based on the layout we selected, then 2nd parameter is file name with extension [ in this file only appender will writes the messages ], then 3rd parameter is by default true, means appender will appends the error messages, if we give false then appender will clears the previous data in my.txt file and write newly
Hey see, i have used FileAppender, but if i would like to change my appender choice to ConsoleAppender, then again we must open this java file then modifications and recompile bla bla…, so to avoid this we can use one .properties file, will see this in the next session.
|
What you are thinkig....
6 Responses to “Log4j Hello World Program”
If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum





Hello Java4sTeam,
I have one doubt, please help.
In line 18, the third parameter we are using to append/not append the file. For that we writing as true/false. This we are doing in java file.
If I want to specify this(specifying about true/false) in .properties file, then how can i specify?
Thank you.
@Vaseem
Yeah you can specify the same in .properties file like……
Compare with this
log4j.appender.LOGFILE.Append = true
Rmemeber: must write Append unlike append [ its case sensitive ]
in 13th line without declaration how did u initialize the object of SimpleLayout();
Layout l1 = new SimpleLayout();
@srinivas
SimpleLayout() is subclass of Layout
Note: We can take subclass object into its super class reference.
Hope its clear.
More information:
https://logging.apache.org/log4j/1.2/apidocs/index.html
thank u sir ,ya ur right actually i have seen Layoutl1 = new SimpleLayout(); insted of “Layout l1 = new SimpleLayout();” .I thought Layoutl1 was Layout class’s Object variable .now its ok
please update what kind of jar files required to develop a simple log4j application.
Thanks
Rajesh B