Logging Introduction

Log4J Introduction

In Real time applications development, we may get number of problems or bugs or exceptions while executing or testing the applications, To identify the problems and their locations then we have to trace the applications flow of execution.

To trace applications flow of execution we will use “System.out.println(–)” at basic level.

public class Transaction {
   void deposit() {
      System.out.println("logic for deposit");
   }
   void withdraw() {
      System.out.println("logic for withdraw");
   }
   void transfer() {
      System.out.println("logic for transfer");
   }
}
public class Test {
   Transaction tx = new Transaction();
   System.out.println("Before deposit() method call");
   tx.deposit();
   System.out.println("After deposit() method call");
   -----
   System.out.println("Before withdraw() method call");
   tx.withdraw();
   System.out.println("After withdraw method call");
   ------
   System.out.println("Before transfer() method call");
   tx.transfer();
   System.out.println("After transfer() method call");
}

If we execute the above application then we are able to get the following output on console or command prompt.

  • Before deposit() method call
  • logic for deposit
  • After deposit() method call
  • Before withdraw() method call
  • logic for withdraw
  • After withdraw() method call
  • Before transfer() method call
  • logic for transfer
  • After transfer() method call

If we use System.out.println() method in applications to trace flow of execution then we are able to get the following problems.

  1. System.out.println(–) will be used for only console display, not for sending data to any other output systems like file systems [ html files, xml files, text files…], databases, network
  2. System.out.println() method contains synchronized block to display data, it is heavy weight, expensive and time consuming.
  3. In Applications, it is not suggestible to write too many no of System.out.println() methods in applications, because, it will reduce application performance.
  4. System.out.println() method is useful in Development environment only, It is not suitable in production environment , because, if we use System.out.println() method in server side applications then it will display messages in servers console only, it will not be available to users.
  5. System.out.println() will display the messages on console or on command prompt, it will not show differences in Error messages, warning messages, normal information etc
  6. System.out.println() is suitable for simple standalone applications, not for complex enterprise applications.

To overcome all the problems while tracing applications we have to use Logging.

Logging

It is the process of writing log messages during the execution of a program to a central place. This logging allows you to report and persist error and warning messages as well as info messages so that the messages can later be retrieved and analyzed.

In general, in java applications, Logging is required

  1. To understand flow of execution in applications
  2. To manage exception messages when Exceptions are occurred in java applications
  3. To manage the event – notification messages in file systems
Logging Framework

It is a set of classes and interfaces or a product to perform Logging in Java applications.

E.g

  • Java provided java.util Logging
  • Log4j
  • LogBack
  • Commons Logging
  • ObjectGuy
  • TinyLog
Logging Introduction
Scroll to top