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 .:: | ||
Comments
16 Responses to “Log4j Hello World Program”
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.
@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 ]
in 13th line without declaration how did u initialize the object of SimpleLayout();
Layout l1 = new SimpleLayout();
@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
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
please update what kind of jar files required to develop a simple log4j application.
Thanks
Rajesh B
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.
Can you please share the jars which is required for log4J
Thanks for sharing Your great knowledge.
Please should be increase font size .
Thanku.
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.
Why it went into catch block? Which code leads to an exception?
Simplest example.. i have ever seen…!
addApender method is available in which Versioin of log4j
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.
Exception in thread "main" java.lang.NullPointerException
at org.apache.log4j.Category.error(Category.java:362)
at Email.Client.main(Client.java:31)
static Logger l1 = Logger.getLogger is not allowed why? I am using log4j2.8.2