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 .:: | ||
Comments
24 Responses to “Example On Log4j.properties File With FileAppender & SimpleLayout”
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 …
@ 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.
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
@Rajkirpal
Yeah its happened in the flow by mistake, good observation.
Thank you so much Raj, for your time 😉
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.
@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
Thank you @venkat
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.
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…
thanks java4s .most of my doubts were cleared,i am clear about rootlogger,not getting clarity over dummyname abc
In this example where can I mention file appender third parameter(true/false) in properties file?
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 …
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
how to keep the destination of log4j file i mean output log file to remote machine
how to create new log file daily??
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?
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.
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
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.
how to save log4j file inside my project directory in web application
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
You are the best
tooooo good.
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?
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.