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

What Are The Main Components Of Log4J

Log4j » On Feb 27, 2012 By Sivateja

We have mainly 3 components to work with Log4j

  • Logger
  • Appender
  • Layout


  • Logger is a class, in org.apache.log4j.*
  • We need to create Logger object one per java class
  • This component enables Log4j in our java class
  • Logger methods are used to generate log statements in a java class instead of sopls
  • So in order to get an object of Logger class, we need to call a static factory method [ factory method will gives an object as return type ]
  • We must create Logger object right after our class name, i will show you

Getting Logger Object

static Logger log = Logger.getLogger(YourClassName.class.getName())

Note:  while creating a Logger object we need to pass either fully qualified class name or class object as a parameter, class means current class for which we are going to use Log4j.


Logger object has some methods, actually we used to print the status of our application by using these methods only

We have totally 5 methods in Logger class

  • debug()
  • info()
  • warn()
  • error()
  • fetal()

As a programmer its our responsibility to know where we need to use what method, did you observe there ? method names are different right, but all are same :-)

Priority Order

debug < info < warn < error < fatal

I mean, fatal is the highest error like some database down/closed

Remember:  Friends don’t confuse by seeing all these 5 methods all are same, for example if our application is about 100 lines and we have JDBC related code in some 45th line or some where there we used to write fatal() method.  All it could be is just for human identification purpose names are different, else these 5 methods will print one text message only ;)

You will get more clarity once you saw the first program on log4j.


Appender job is to write the messages into the external file or database or smtp

  • Logger classes generates some statements under different levels right, this Appender takes these log statements and stores in some files or database
  • Appender is an interface

In log4j we have different Appender  implementation classes

  • FileAppender [ writing into a file ]
  • ConsoleAppender [ Writing into console ]
  • JDBCAppender [ For Databases ]
  • SMTPAppender [ Mails ]
  • SocketAppender [ For remote storage ]
  • SocketHubAppender
  • SyslogAppendersends
  • TelnetAppender

Again in FileAppender we have 2 more

  • RollingFileAppender
  • DailyRollingFileAppender

For now just remember, i will explain while executing the program


This component specifies the format in which the log statements are written into the destination repository by the appender

We have different type of layout classes in log4j

  • SimpleLayout
  • PatternLayout
  • HTMLLayout
  • XMLLayout

So will see one simple example on Log4j… [ In The Next Session ]

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



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
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets

UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.

© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.