Logging and Determining a Logged Message

This section introduce you about Java logging and how to use the logger in Java program to log the error and messages into the log file.

Logging and Determining a Logged Message

This section introduce you about Java logging and how to use the logger in Java program to log the error and messages into the log file.

Logging and Determining a Logged Message

Logging and Determining a Logged Message

     

This section introduce you about Java logging and how to use the logger in Java program to log the error and messages into the log file. Explanation of java logging is explained in the given example program. This program will help you understand the java logging and its implementation in the java program.

Java provides logging APIs like: Logger, Level, Handler etc. for implementing logging features in your java application.  It is a part of J2SE (Java 2 Standard  Edition). To create java logging program you need a Logger object. The Logger object contains log messages. Logger has one or more handler that performs log records. Java logging provides a way to contain multiple types of message like: warning, info, severe etc. for an application. These information or messages can be used for many purposes but it is specially used for debugging, troubleshooting and auditing.

The first program simply create a log file and shows log message for the warning, info and servere and another checked the logging message. This program creates a log file that contains multiple information. 

Code Descriptions:

FileHandler(String file_name):
This is the constructor of FileHandler class. FileHandler class extends the StreamHandler. This is the class of java.uti.logging package. The above constructor constructs the FileHandler with given string as file name.

Logger:
This is the constructor of Logger class. The logger class extends the Object. IThis is the class of  java.util.logging package. Logger constructor used for constructing a logger. A logger object contains a log message to the system or application component. The logger object called by the getLogger method and returns a suitable maintain logger. 

getLogger(String log_name):
You can find or construct a logger with the help of above method. It takes string types value that can be used for a named subsystem.

addHandler:
It can be used for adding a log handler to get logging message which is maintaining in the warning, info and severe.

warning(String war_message):
This method used for showing the log WARNING message. The given message shows to all output handler objects when the logger is enabled at the time of warning message level. It takes string type value for the warning message.

info(String info_message):
It works same as warning method but it shows log INFO message.

servere(String ser_message):
It also works same as like: warning and info method but it shows log SERVERE message.

Here is the code of "QuintLog.java":

import java.io.*;
import java.util.logging.*;

public class QuintLog{
  public static void main(String[] args) {
  try{
  FileHandler hand = new FileHandler("vk.log");
  Logger log = Logger.getLogger("log_file");
  log.addHandler(hand);
  log.warning("Doing carefully!");
  log.info("Doing something ...");
  log.severe("Doing strictily ");
  System.out.println(log.getName());
  }
  catch(IOException e){}
  }
}

Download this example.

Program Descriptions:

This program check logger object. If given instruction is certify then shows finest message like: 'Display a finest message' otherwise no any message appear to here.

Code Descriptions:

isLoggable(Level level):
The above method is a Boolean type than returns true or false. It checks the given message is actually logged by this logger that means method is returned true.

finest(String fi_message):
The above method is used to represent the Log FINEST message. This message shows to all output handler objects when the logger is enabled at the time of finest message level . It takes string type value for message.

Here is the code of "CheckLogMessage.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");
  }
  }
}

Download this example.