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 :

static Logger logger = Logger.getLogger(CurrentClass.class.getName()).

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

Example

public class Student{

 private static final Logger LOGGER = Logger.getLogger(Student.class);
   public void getStudentRecord() {
  }
}

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

# Root logger option
log4j.rootLogger=INFO, file, stdout

# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\log\\logging.log
log4j.appender.file.MaxFileSize=12MB
log4j.appender.file.MaxBackupIndex=10
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

# configuration to print on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

 

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:

import org.apache.log4j.Logger;

public class Student {
  static Logger logger = Logger.getLogger(Student.class);

  public static void main(String[] args) {        
     logger.debug("This is debug message");
     logger.info("This is info message");
     logger.warn("This is warn message");
     logger.fatal("This is fatal message");
     logger.error("This is error message");
  
     System.out.println("Logic executed successfully....");

    }

}

 

logging.log(log file)

2018-05-02 16:01:45 INFO  Student:12 - This is info message
2018-05-02 16:01:45 WARN  Student:13 - This is warn message
2018-05-02 16:01:45 FATAL Student:14 - This is fatal message
2018-05-02 16:01:45 ERROR Student:15 - This is error message

 

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

16:01:45,511 &nbsp;INFO Student:12 - This is info message
16:01:45,517 &nbsp;WARN Student:13 - This is warn message
16:01:45,517 FATAL Student:14 - This is fatal message
16:01:45,518 ERROR Student:15 - This is error message

program executed successfully....

 

 

Leave a Reply