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.


 

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

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.

Advertisements

Ads
Share on Google+Share on Google+

Logging and Determining a Logged Message

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Logging and Determining a Logged Message   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
venkat
April 15, 2012
log

yes this is best example for logging ....................
JAva@
April 9, 2013
java details

good
Rakesh
April 26, 2013
Please Add New Security(java 7) Topic

Hi, Please Add New Security(java 7) Topic with very good EXAMPLE.
Sasha
August 21, 2014
Java

Super post !!!!
Sasha
August 21, 2014
Java

Super post!

Ads

 

Ads