Home Java Example Java Util Logger in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Logger in Java

Advertisement
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.

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:

    Advertisement

    Liked it!  Share this Tutorial


    Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

    Posted on: May 11, 2013

    Ask Questions?    Discuss: Logger in Java  

    Post your Comment


    Your Name (*) :
    Your Email :
    Subject (*):
    Your Comment (*):
      Reload Image
     
     
    Comments
    DMCA.com