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

Example On Log4j.properties File With FileAppender & SimpleLayout

Log4j » On Feb 27, 2012 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

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






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


Comments

14 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

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
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


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