The Quintessential Logging Program
To log a message, you first need to obtain a Logger object and then
use it to log the message. Loggers have names that resemble a
fully qualified class name. Typically, log messages generated from a
class would use a Logger with the same name as the class.
The logger contains one or more handlers that are responsible
for processing the log records. A handler could write to a file or
print to a console. Typically, the code that generates log records
should not concern itself with where those log records end up. It only
needs to decide the name of the logger to use.
This example logs a few messages to a logger.
For examples of controlling the destination of the log messages,
see Writing Log Records to a Log File and
Writing Log Records to Standard Error.
import java.io.*;
import java.util.logging.*;
package com.mycompany;
public class BasicLogging {
public static void main(String[] args) {
// Get a logger; the logger is automatically created if
// it doesn't already exist
Logger logger = Logger.getLogger("com.mycompany.BasicLogging");
// Log a few message at different severity levels
logger.severe("my severe message");
logger.warning("my warning message");
logger.info("my info message");
logger.config("my config message");
logger.fine("my fine message");
logger.finer("my finer message");
logger.finest("my finest message");
}
}
Post a comment