Start Logging
The logging functionality is defined in the java.util.logging package. The Logger class provides a set of methods that are used for generating log messages. You can use a Logger object with the name of the class or the package of the application. This Logger object is used to generate the log messages. Each log message is associated with a corresponding log Level. The Level provides guidance as to the relative importance of the message. The Level class specifies seven standard log levels, ranging from SEVERE (the highest priority) to FINEST (the lowest priority). The Logger object provides the ability to log messages at any of the seven severity levels that are defined in the Level class.
To create a Logger, you merely use the following static method of the Logger:
Static Logger getLogger(String Name);
To ensure that the names are globally unique, start names with package names that belong to your application. The general convention is that Loggers have names that resemble a fully qualified package or class name. In most cases, the package provides a convenient boundary. However, you may need to log the messages of a particular class separately.
In the simplest case to log a message, you can call one of the methods of the Logger class called log() with two parameters: a priority level and some String data . Alternatively, you can call a method named for a well-known priority, such as severe(), and pass in a parameter of String data to be logged.
Here's a simple example of how the logging API works:
package com.informit; import java.io.*; import java.util.logging.*; public class SimpleLogging { public static void main(String[] args) { // Get a logger // To create a Logger, you call the static getLogger() method, // passing in a name of a class or a package Logger logger = Logger.getLogger("com.informit.SimpleLogging"); // Alternatively, you could define the logger for the package // Logger logger = Logger.getLogger("com.informit"); // Log a few messages at different severity levels // There are seven different levels for logging; // you can choose a method specific to the logging // level or use the generic log method as shown logger.severe("Something really bad happened "); logger.warning("This is a warning"); logger.log(Level.WARNING,"This is also a warning"); logger.info("Just for your info"); logger.config("A configuration message"); logger.fine("A fine message"); logger.finer("A finer message"); logger.finest("A finest message"); logger.log(Level.FINEST,"This is also a finest message"); } }
Most often a String will be sufficient to log to your log file. However, the Logger object provides additional logging methods that take a parameter that is an array of Object. This allows developers to add their custom Java objects to the log. The method that can be used is as follows:
void log( Level level, String msg, Object[] params);
Running the above SimpleLogging class generates a log on the screen such as the following:
Jun 12, 2002 5:18:04 PM SimpleLogging main SEVERE: Something really bad happened Jun 12, 2002 5:18:05 PM SimpleLogging main WARNING: This is a warning Jun 12, 2002 5:18:05 PM SimpleLogging main WARNING: This is also a warning Jun 12, 2002 5:18:05 PM SimpleLogging main INFO: Just for your info
Notice that all the calls to log messages other than the SEVERE, WARNING, and INFO were ignored at runtime. This is because, by default, the Logger is configured to ignore records lower than Level.INFO. Hence, all messages logged for Levels CONFIG, FINE, FINER, and FINEST were ignored by the logger. However, you don't need to recompile your application to allow or prevent low-priority messages.
We'll discuss the messages logged to the file later. In the simplest form, the log methods don't take an explicit class or method name as parameters. These are appropriate when you don't require detailed information. The Logging framework makes a guess to determine the class and method that's logging the message and adds this information into the LogRecord. However, this information is at best approximate and is largely dependent on the implementation of the JVM on your machine. In order to have a guaranteed location specified in the logs, you can use an explicit method similar to the following calls:
void warning( String sourceClass, String sourceMethod, String msg); public void logp( Level level, String sourceCls, String sourceMethod, String msg); public void logp( Level level, String sourceCls, String sourceMethod, String msg, Object [] data);