Get all updates of Java4s
Follow Us: Email Newsletter

What Are The Main Components Of Log4J

Log4j » On Feb 27, 2012 { 8 Comments } By Sivateja

We have mainly 3 components to work with Log4j

  • Logger
  • Appender
  • Layout

Logger

  • 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.

Example

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()
  • fatal()

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

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

Layout

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 ]

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

8 Responses to “What Are The Main Components Of Log4J”
  1. sarathchandra says:

    it is very useful for me good analyzing about the topic
    than q

  2. dinesh says:

    Good work ! Good Explanation and clear gun shot points….

  3. bhadram says:

    Hi Java4s,

    subject: Minor correction.

    Details

    In the list of methods for Logger class you guys mentioned "fetal" instead of "fatal".In normal people can understand the things but at the first glance they might get little bit confused.Ofcourse later the naming convention was good.Please update the literal.

    *Note:Consider this as intimation.

    I have gone through some the tutorial like servlets, this log4j and spring.The explanation regarding to the topics were clear and easy to understand the things.Good work buddy.Keep goging :)

    Thanks

    Bhadram Komali

  4. B.Manikanta says:

    It’s really simple and good Explanation,I easily understood all the concepts,Thank you very much……..

  5. chetan says:

    It’s really simple and i always found useful information, please provide SOAP WEBSERVICE and Junit tutorial asap.

  6. khadar says:

    Hai sir this is easily understand pls thanku so much…..

  7. Himabindu says:

    Easy to understand and very clear cut information.
    Thankyou:)

  8. Rajnish says:

    Very Impressive tutorials.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.