3 components to work with Log4j


  1. Logger
  2. Appender
  3. Layout

1) Logger:
Logger methods are used to generate log statements in a java class instead of sopln.
We must create Logger object right after our class name.
While creating a Logger object we need to pass current class object as a parameter for which we are going to use Log4j.
Eg.
1
2
3
4
5
6
7
8
 public class Client {

   static Logger l = Logger.getLogger(Client.class.getName());

   public static void main(String[] args) {
     // Our logic will goes here
   }
}



Logger object has some methods, We use these methods to print the status of our application.

We have 5 methods in Logger class starting from highest priority
  1.     debug()
  2.     info()
  3.     warn()
  4.     error()
  5.     fetal()

Note: The purpose of all these methods is same ie. to print logs in a file. Only names are different.
 If you mention warn in log4j.properties file then methods from and below warn() will be executed.


2) Appender:
Appender job is to write the messages into the external file or database or smtp
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
In FileAppender we have 2 more
1) DailyRollingFileAppender - Rotates based on dateFormat
2) RollingFileAppender - Rotates based on a maximum file size.


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

Note: The PatternLayout defaults to %m%n which means print your-supplied message in a newline. This is exactly the same as printed out by Java's System.out.println(...) method                                     

                                                                          || NEXT TOPIC ||

Related Article:


No comments :

Post a Comment