Logger in Java

In this section we will learn how to use the Logger in Java program. Logger in Java is part of java.util.logging are used to log the error and messages into the log file. The name of Logger are dot-separated and should be the package name or class name.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

In this section we will learn how to use the Logger in Java program. Logger in Java is part of java.util.logging are used to log the error and messages into the log file. The name of Logger are dot-separated and should be the package name or class name. getLogger factory method provides the object of Logger class. Logger object contains log messages. If the reference is not strong of Logger it can be Garbage collected at any time.

This program will help you understand the java logging and its implementation in the java program. Java has logging APIs that implements logging features in Java application. Few of the logging APIs are: FileHandler, Handler, Level, Logger, LogManager, LogRecord, StreamHandler, XML Handler, etc.

Properties of Logger:

  1. Every Logger keeps track of parent logger.
  2. Every Logger has a Level. If a Logger level is null, it acquires the level of its parent class. Logger level can be set by Logger.setLevel method.
  3. Every Logger has ResourceBundle name that is used to localize logging message.

Operations performed by Logger:

  • Logger performs cheap test on each logging.
  • Then Logger allocates LogRecord that describes Logging message.
  • Then Logger publishes LogRecord to its output Handler
  • Logger objects are required to create java logging program. Logger has one or more handler that performs log records. Log messages or information like: warning, info, server, etc are used for debugging, troubleshooting and auditing, etc.

    Constructor: Logger(String name, String resourceBundleName)

    Few of the Methods of Logger are:

    1. addhandler(Handler handler)
    2. entering(String msg)
    3. fine(string msg)
    4. getAnnonymous Logger()
    5. getFilter()
    6. getHandlers()
    7. getLevel()
    8. getName()
    9. getParent()
    10. info(String msg)
    11. removeHandler(Handler handler)
    12. warning(String msg)

    Example of Logger in Java:

    import java.io.*;
    import java.util.logging.*;
    
    public class CheckLogMessage{
      public static void main(String[] args) {
      Logger log = Logger.getLogger("log_file");
      if(log.isLoggable(Level.OFF)){
      log.finest("Display a finest message");
      }
      }
    }
    

    Output:

    Advertisements

    Share on Google+Share on Google+

    Logger in Java

    Posted on: May 11, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

    Advertisements

     

    Discuss: Logger in Java  

    Post your Comment


    Your Name (*) :
    Your Email :
    Subject (*):
    Your Comment (*):
      Reload Image
     
     
    Comments:0

    Ads

     

    Ads