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

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

Directory Structure

Client.java

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.







By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

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:

    @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 ]

  3. srinivas says:

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

    Layout l1 = new SimpleLayout();

  4. Java4s says:

    @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

  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.

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.