Java Reference
In-Depth Information
log. Typically, this should be static and final, indicating that one single logger will be used for all
instances of the class and it will remain unchanged. The logger will automatically include a default
console handler that will print messages to the console. You can create additional handlers yourself.
If you want your log files to be saved to a text file somewhere, you can create your own file handler.
Once your logger and handlers are created, you should create a special method that will essentially set
them up for use. In the example, this is called method logIt() , but you can change the name. Inside
this method, you should instantiate the file handlers with the path and name of the file where you
would like to save the log messages. Because IOExceptions are checked exceptions, you need to put
these in a try/catch block to handle the error. Next, you can set the formatter and level for each han-
dler, attach the handlers to the logger, and set the level of the logger. Both the logger and the handlers
can be independently set to their own level. However, if the logger is higher than the handler level, the
handler level will effectively be at the logger's level, because no lower level messages will be generated
by the logger. In the example, you see two handlers for the same logger. This allows more messages to
be output to one file while fewer (more important) messages are output to another file.
Now you are ready to use the logger in your main and other methods. You can call methods from
the Logger class to log the information you're interested in. The entire list of methods can be found
in the Logger class documentation, but some of the methods you will see are listed here. Each
method also has several parameter choices you can make that impact the results of the log.
entering() : Log a method entry.
exiting() : Log a method return.
log() : Log a custom message.
The example is based on the retirement fund program you've been working with. There are now
log messages indicating when the recalculate method is entered and exited, as well as the value
of the fundAmount variable each time it is calculated. Finally, a log message is created if the value
is extremely low, indicating something is wrong with the calculation. And at the end of the main
method, several messages are logged at each level to demonstrate which ones are written by each
handler.
import java.io.IOException;
import java.util.logging.*;
public class LoggerExample {
// create a Logger instance
private final static Logger logger =
Logger.getLogger(LoggerExample.class.getName());
// create a file handler for fine messages and above
private static FileHandler finerhandler = null;
// create a file handler only for config messages and above
private static FileHandler warninghandler = null;
public static void logIt() {
try {
finerhandler = new FileHandler("src/loggerExample_finer.log", false);
warninghandler = new FileHandler("src/loggerExample_config.log",false);
} catch (SecurityException | IOException e) {
Search WWH ::




Custom Search