Newsletter

Example On Log4j.properties File With FileAppender & SimpleLayout

Log4j » on Feb 27, 2012 { 24 Comments } By Sivateja

Let us see how to use log4j.properties file

Files Required

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

Directory Structure

Client.java

import org.apache.log4j.Logger;

public class Client {

  static Logger l = Logger.getLogger(Client.class.getName());

  public static void main(String[] args) {      

      l.debug("This is debug message");
      l.info("This is info message");
      l.warn("This is warn message");
      l.fatal("This is fatal message");
      l.error("This is error message");

      System.out.println("Your logic executed successfully....");

  }
}

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

log4j.properties

log4j.rootLogger = DEBUG,abc
log4j.appender.abc = org.apache.log4j.FileAppender
log4j.appender.abc.file = my.txt
log4j.appender.abc.layout = org.apache.log4j.SimpleLayout

my.txt

DEBUG – This is debug message
INFO – This is info message
WARN – This is warn message
FATAL – This is fatal message
ERROR – This is error message

Execution Flow

  • Run Client.java
  • Log4j environment created, at line number 5
  • As our default properties file name is log4j.properties, we no need to import properties file explicitly into Client.java, by default our java class will verify for the properties file named log4j.properties.  If we give the name other than log4j to the properties we have to import manually into our java class [ will see this later, like how to manually ]
  • So once Logger object created at line number 5, our class will be able to know about the content in log4j.properties
  • In log4j.properties the content always will be in key,value pairs only

Explanation

  • If we use .properties file, we no need to import any related classes into our java class
  • log4j.rootLogger = DEBUG,abc  — > Here DEBUG means we are specifying the level from where log4j methods execution must start,  see my.txt file it showing all messages right.  But if we wrote log4j.rootLogger = WARN,abc then it will prints the messages in l.warn(), l.error(), l.fatal() and ignores l.debug(), l.info()
  • I have used FileAppender as my Appender, so if we want to change my appender to ConsoleAppender, i will open log4j.properties file and do the modifications,  so no need to touch our java classes, this is the main advantage of .properties file

So just change layout into HTMLLayout and check the output

​​

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

24 Responses to “Example On Log4j.properties File With FileAppender & SimpleLayout”
  1. Mohammed Vaseem says:

    Hello java4s team,
    Thank you for sharing rich content with all…

    I have one doubt. Please help me.

    log4j.properties file
    ———————-
    log4j.rootLogger = DEBUG,abc
    log4j.appender.abc = org.apache.log4j.FileAppender
    log4j.appender.abc.file = my.txt
    log4j.appender.abc.layout = org.apache.log4j.SimpleLayout

    My doubts:
    1. What is the purpose of root logger?
    2. What is the purpose of “abc” ?

    Thanks …

  2. Java4s says:

    @ Mohammed

    log4j.rootLogger is a predefined key given by log4j, actually here we used to specify the Logging level

    – And ‘abc‘ is one dummy name, just to maintain the connection [ not exactly ], but some thing like finally we used to add appender to our logger object right ( check log4j hello world program once ), What i mean is we are letting each component to know other, maintaining chain.

    Just i said it 🙂 , hope you are clear.

  3. rajkirpal says:

    Hi, java4s

    Subject : Minor Correction required. 😉

    I know it might be by mistake but i just read it so just intimating you buddy.

    Topic Name :
    ” Example On Log4j.properties File With FileAppender & SimpleLayout ”

    in this topic’s Explanation part in line no three by mistake you have written ” and ignores l.warn(), l.info() ”
    but i think it should be :” and ignores l.debug(), l.info() ”

    Thanks
    RajKirpal

  4. Java4s says:

    @Rajkirpal

    Yeah its happened in the flow by mistake, good observation.

    Thank you so much Raj, for your time 😉

  5. gangadhar says:

    hi,

    Ref:
    [If we give the name other than log4j to the properties we have to import manually into our java class [ will see this later, like how to manually ]

    please provide how to configure and utilize the “XXX.properties” that is other than log4j.properties.

  6. venkata says:

    @Gangadhar,

    You can configure your won properties file by using this statement
    PropertyConfigurator.configure(“venkata.properties”);” .Here the file “venkata.properties” should be under the project folder.

    Refer this link…
    http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html

    Thanks
    Venkata

  7. Gangadhar says:

    Thank you @venkat

  8. Kiran says:

    Dear all this is kiran i would like to thank to Java4s team and its Supporters. i have gone through all of your Log4j articles and gained something new.

    Thanks,
    Kiran.

  9. Dear all this is suresh arkati…i would like to thank to Java4s team and its Supporters. i have gone through all of your Log4j articles and gained something new.Once again thnq so much for providing these material..I guess every one can understand if thouroly go threw this material…

    Thanks& Regards,
    Suresh A…

  10. saibaba says:

    thanks java4s .most of my doubts were cleared,i am clear about rootlogger,not getting clarity over dummyname abc

  11. Yaser says:

    In this example where can I mention file appender third parameter(true/false) in properties file?

  12. sayani says:

    Thank u so much.. your content was really helpful. but i need a little more help. can i take the filename(my.txt) at runtime? if yes then how can i do that? thanx again …

  13. Hi This is shivaji sadineni
    Dear all this is suresh arkati…i would like to thank to Java4s team and its Supporters. i have gone through all of your Log4j articles and gained something new.Once again thnq so much for providing these material..I guess every one can understand if thouroly go threw this material…

    Thanks& Regards,
    Shivaji sadineni

  14. naveen says:

    how to keep the destination of log4j file i mean output log file to remote machine

  15. Anupama Dhuri says:

    how to create new log file daily??

  16. Sivateja, thanks for tutorial, but the is a question related to it.
    Whe I use log4j HTMLLayout it generetes html without closing tag. So next time logs are generating then html breaks as you understand.
    Why it happens and how to fix it?

  17. Vilkas.s says:

    Hi i have a doubt with this program,
    suppose when i have five java files how many .property file is needed. but one thing i want produce different output log in each class what is the solution please replay me.

  18. Tejashwini says:

    log4j.rootLogger=INFO,fa
    log4j.appender.fa=org.apache.log4j.FileAppender
    log4j.appender.fa.File=logs.txt
    log4j.appender.fa.layout=org.apache.log4j.PatternLayout
    log4j.appender.fa.layout.ConversionPattern=%r %d{MM:dd:yy hh-mm-ss} [%t] – %m %n
    Its my properties file
    when I Use ConsoleAppender Instead of FileAppender its wprking but FileAppender not working. Plz tell me the Prblm

  19. Rahul says:

    Hi,
    – And ‘abc‘ is one dummy name, just to maintain the connection [ not exactly ], but some thing like finally we used to add appender to our logger object right ( check log4j hello world program once ), What i mean is we are letting each component to know other, maintaining chain.

    That is not a dummy name. It is the name of the appender that you set to the rootLogger. The word “dummy name” is misleading. ‘abc’ is actually an appender that we have set to rootLogger.

  20. sindhu says:

    how to save log4j file inside my project directory in web application

  21. Brijesh says:

    Hi Sir,

    How i can add current date to the log file.

    like if log file name is my.txt then it will be save like my_08172015.txt

  22. shravan says:

    You are the best
    tooooo good.

  23. SABYASACHI MISHRA says:

    nice explanation.
    I have a doubt which i was faced in an interview that if we are using spring in our project then it has its own logging support then why we need log4j ?i.e he is asking the diffrnce betwn spring logging and log4j logging?

  24. Sakshi Mittal says:

    Thank you so much for such a good KT.

    But I have a problem–
    If I use ==>>> log4j.rootLogger = WARN,abc
    then also I got all the messages starting from DEBUG.
    Please clear it.

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.