Main Components of Log4j

What are different appenders that can configure in log4j?
There are 3 main components that are used to log messages based upon type and level.
These components help to log messages according to message type and priority. Also provide the formatting option.
These components are:
– loggers
– appenders
– layouts
Logger Object is responsible for storing the information which need to be logged
We need to create Logger Object in a class (one per class).
Example:      static Logger log = Logger.getLogger(“your_class_name”);
Appender Objectis used to define the destination like file system, console, database, mail server etc. where user wants to save the log messages.
There are various appenders –
FileAppender –> used to write into a file. Files Appender is used to log the information into our custom name files. when we are configuring this appender, we can specify the file name
ConsoleAppender –> used to write into console. if we use this as appenders in your application, log4j logs the information in the console or command prompt window that is started with startup script.
JDBCAppender –> used to write in database
SMTPAppender –> used to send the log over mail
Again, in FileAppender we have 2 more
RollingFileAppenderwhich Rotates based on a maximum file size.
RollingFileAppender extends FileAppender to backup the log files when they reach a certain size. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.
It has two important menthods –
setMaximumFileSize – Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
setMaxBackupIndex – Set the maximum number of backup files to keep around.
DailyRollingFileAppender which Rotates based on dateFormat.
DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency. DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss.
Layout Object is responsible for layout and formating of the logging information to the desired format.
We have different type of layout classes in log4j
    SimpleLayout
    PatternLayout
    HTMLLayout
    XMLLayout

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories