Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

like the code is provided for Struts and other applications, step by step code for JSTL custom Tags creation and their usage in a JSP page should be provided
in the same way for log files also
like a log file should be there and when we give userid and password for an application(simple JSP application) that details should be stored in the log file which can be viewed by opening that log file

Posted by Srikanth on Thursday, 09.6.07 @ 23:16pm | #25321

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.