Principal components of Log4j

There are three main components:

loggers: It is responsible for capturing the logging information.
appenders: It is responsible for publishing logging information to various preferred destinations.
layouts: It is responsible for formatting logging information in different styles.

  1. Logggers
  2. Apppenders
  3. Layouts

1. Logggers :

Logger class is a Java-based utility that has got all the generic methods already implemented to use log4j. Loggers is responsible for logging information.

Below are the steps through which we can implement loggers into our project:

  • Create an instance for logger class.
  • Define the Log4j level: There are seven kinds of log levels:

All – This level of logging will log everything.
DEBUG – It prints the debugging information and is helpful in development stage.
INFO – It prints the informational message that highlights the progress of the application
WARN – It prints the information regarding faulty and unexpected system behavior.
ERROR – It prints the error message that might allow system to continue.
FATAL – It prints the system critical information which are causing the application to crash.
OFF – It means no logging.

2. Appenders

It is used to deliver Log Events into their destination logs file. It decides what will happen with log information.

Following are few types of Appenders

  • Console Appender logs to standard output.
  • File appender prints logs to some file.
  • Rolling file appender to a file with maximum size.

3. Layout
It is responsible for formatting logging information in different styles. It provides two static methods for obtaining a Logger Object.

  • Public static Logger getRootLogger()
  • Public static Logger getLogger(String name)