You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Log4j Hello World Program

Log4j » On Feb 27, 2012 By Sivateja

Let us see one simple program in Log4j

For working with log4j, we must set log4j.jar in our class path

Files Required

  • my.txt   [ We will let the appender to write into this file ]

Directory Structure

Once we run this client program, my.txt will contains….


FATAL – This is the error message..


  • 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.

By ( Java/J2EE Developer & Web Designer )
Founder of You can contact me at sivateja [at]


7 Responses to “Log4j Hello World Program”
  1. Mohammed Vaseem says:

    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.

  2. Java4s says:

    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 ]

  3. srinivas says:

    in 13th line without declaration how did u initialize the object of SimpleLayout();

    Layout l1 = new SimpleLayout();

  4. Java4s says:


    SimpleLayout() is subclass of Layout :-)

    Note: We can take subclass object into its super class reference.
    Hope its clear.

    More information:

  5. srinivas says:

    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

  6. RAJESH says:

    please update what kind of jar files required to develop a simple log4j application.

    Rajesh B

  7. Thanks a lot java4s team…..

    You are helping a lot.The tutorials are really good for freshers like me… I am able to run the examples fine.


By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services

  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2015 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.