Logging with Log4j in JAVA

Why we use Log4j ?

Log4j  is used for logging. Logging is the process of writing log message in any file, db,console etc( any central place).

If we use SOP  (System.out.print() ) statement to print the log message then we can have some disadvantage :

1. We can print log message on console only. So when console is closed , we will lose all logs.

2.We can’t store log message at any permanent place. These message will print one by one on console because it is single threaded environment.

To overcome these problems Log4j framework came into the picture.Log4j is an open source framework provided by Apache for only java projects.

Log4j Components:

Log4J have three components mainly which are following :

  1. Logger
  2. Appender
  3. Layout
  • Logger

Logger  is a class in org.apache.log4j.*  package. we have to initialize one Logger object for each java class. We use Logger’s methods to generate log statements. Log4j provide the factory method to get Logger object.

Syntax to get Logger object :

Note: CurrentClass is a java class name for which we are getting logger object.

Example

Logger class has some method which are used to print the application status.

We have totally five methods in Logger class

  1. info()
  2. debug()
  3. warn()
  4. fatal()
  5. error()

It depends on us how to use and when to use these methods. Here methods name are different but working is same of all. All will print a message only.

Levels 

Level is a class in org.apache.log4j.* package. We can also make custom level by extending Level class. Each level has a different priority order like this :

debug < info < warn < error < fatal

It means fatal is the highest priority error like database closed.

 Appender

Appender is used to write the message into the file or db or smtp.

Log4j have different type of appender:

  1. SyslogAppendersends
  2. SMTPAppender
  3. JDBCAppender
  4. FileAppender
  5. SocketHubAppender
  6. SocketAppender
  7. TelnetAppender
  8. ConsoleAppender

 Layout

This is used to define the formatting in which logs will print in repository.

We have different types of layout:

  1. PatternLayout
  2. SimpleLayout
  3. XMLLayout
  4. HTMLLayout

log4j – Configuration

log4j.properties

 

Description of log4j.properties file :

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

It will define appender type that means it specify where we want to store application logs . RollingFileAppender will use to print all logs in file and ConsoleAppender will use to print all log in console

log4j.appender.file.File=D:\\log\\logging.log

It specify log file location.

log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L – %m%n

It specify the pattern in which log will print into log file

Example:

 

logging.log(log file)

 

It will not print debug level error logs because we defined root logger as INFO in log4j.properties file . That’s why, error message will print which have priority greater than INFO level.

Console logs

 

 

Leave a Reply