Google

Friday, February 11, 2005

Java Logging API Basics

Java Logging API Basics
-----------------------------------------


Java Logging API is part of Java 2 platform. It provides java.util.logging package which provides you with a logging facility with the following features-

1. Embed messages inside your code
2. configure at runtime whether messages should be displayed or not.
3. If you decide to display the messages, you can configure what severity of messages should be displayed(by default it will display all the kind of messages)

We will use the class java.util.logging.Logger to log the messages in our code.

Steps to be followed in the code:-

1. Create an instance of Logger class and pass as a parameter a package.

Example,

Logger logger = Logger.getLogger("deepak.learn.elevel_feb_five");

The reason we pass a package as parameter is that we can enable or disable logging for a particular package.

2. Log the message using the log method of Logger class.

Example,

logger.log(Level.SEVERE, "Severe problem!!!!!");

Following the sample class I have written to explain the flow.

package deepak.learn.eleven_feb_five;

/**
* @author Deepak.Saini *
*/

import java.util.logging.*;

public class LogExample {

private static Logger logger = Logger.getLogger("deepak.learn.elevel_feb_five");

public static void main(String args[]){
logger.log(Level.SEVERE, "Severe problem!!!!!");
logger.log(Level.WARNING, "Warning!!!!!");
}
}


Run the program: java deepak.learn.eleven_feb_five.LogExample

Output:
Feb 11, 2005 9:46:22 PM deepak.learn.eleven_feb_five.LogExample main
SEVERE: Severe problem!!!!!
Feb 11, 2005 9:46:22 PM deepak.learn.eleven_feb_five.LogExample main
WARNING: Warning!!!!!


Great!!! Now that we have the program running, let us dig deeper.

As you see in the statements below that we have mentioned Level.WARNING and Level.SEVERE.

logger.log(Level.SEVERE, "Severe problem!!!!!");
logger.log(Level.WARNING, "Warning!!!!!");

What does that mean? Ofcourse it gives you a hint of the severity of the error that has been reported.

The java.util.logging.Level class defines a set of standard logging levels that can be used to control logging output.

The levels in descending order are:

SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)

In addition there is a level OFF that can be used to turn off logging, and a level ALL that can be used to enable logging of all messages.

Ok. So we have learnt a little more about API now. Lets make some more changes in our class deepak.learn.eleven_feb_five.LogExample

import java.util.logging.*;

public class LogExample {

private static Logger logger = Logger.getLogger("deepak.learn.elevel_feb_five");

public static void main(String args[]){
logger.setLevel(Level.SEVERE);
logger.log(Level.SEVERE, "Severe problem!!!!!");
logger.log(Level.WARNING, "Warning!!!!!");
}
}

Run the program: java deepak.learn.eleven_feb_five.LogExample

Output:
Feb 11, 2005 10:01:18 PM deepak.learn.eleven_feb_five.LogExample main
SEVERE: Severe problem!!!!!!

What did we do?

We set the log level specifying which message levels will be logged by the logger. In our program, we set the level at SEVERE, so when we run the program, we will only see the messages with level SEVERE.

Ofcourse, you can now do a lot of things with the little information we gained today.

Recommened programming excercise.

Rewrite the above class so that you set the logging level as well as the package for which logging must be done at run time.

Happy learning
Deepak Saini

0 Comments:

Post a Comment

<< Home