Newsletter

Log4j Hello World Program

Log4j » on Feb 27, 2012 { 16 Comments } 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

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.

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

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

  8. Rini says:

    Can you please share the jars which is required for log4J

  9. Tiru says:

    Thanks for sharing Your great knowledge.
    Please should be increase font size .
    Thanku.

  10. Pallavi says:

    Hi Sir,

    I am not able to run the simple program in Log4j. Can you please tell me what are the jar files we have to use to get successful output without any CTE.

  11. Siddharth says:

    Why it went into catch block? Which code leads to an exception?

  12. Bharat says:

    Simplest example.. i have ever seen…!

  13. raviteja says:

    addApender method is available in which Versioin of log4j

  14. deep says:

    while running above example i am getting below error:-

    Exception in thread "main" java.lang.NullPointerException
    at org.apache.log4j.Category.error(Category.java:362)
    at Email.Client.main(Client.java:31)

    Please help me to resolve this.

  15. deep says:

    Exception in thread "main" java.lang.NullPointerException
    at org.apache.log4j.Category.error(Category.java:362)
    at Email.Client.main(Client.java:31)

  16. Daniel says:

    static Logger l1 = Logger.getLogger is not allowed why? I am using log4j2.8.2

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.