The Java.util.logging package provides powerful log output, and it requires some skill to correctly and effectively use these features: Understand different level, level.fine and level. Finer,level. Finest is used to debug and track programs, such as printing some debugging information, exceptions, and so on. Level.info will print the information to the console. Using the Logger.isloggable (level) method to detect the current level, so as to determine whether the log output, the log should also follow the need to output the principle, to avoid generating a large number of debugging information strings, occupy system memory, affect performance. Turn on and off the log programmatically when necessary, such as opening most logs when developing a program, shutting down most logs in order to check for errors and debugging programs, and to avoid impacting customers and affecting program performance. When necessary, you can use a unique logger for a module, that is, the name of the incoming child module when the logger is created, and all classes use the same logger to avoid generating too many logger instances. Below is a section of code that uses the log tool:
Package javatest;
Import java.io.IOException;
Import Java.util.logging.ConsoleHandler;
Import Java.util.logging.FileHandler;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import Java.util.logging.SimpleFormatter;
/**
*/
public class Loggertest {
Building Logger
private static Logger log = Logger.getlogger (Loggertest. class. GetName ());
/** creates a new instance of Loggertest * *
Public Loggertest () {
Use this method to avoid generating strings that require output
if (log.isloggable (Level.fine)) {
Log.log (Level.fine, "start constructing ...) " );
}
}
/**
* Sets the log output level for the specified package.
*
* @param packagename The specified package
* @param fileName Specifies the output file for the log
* @param level log output levels
*/
private static void Setloglevel (String packagename, String fileName, level level) {
try {
Filehandler Filehandler = new Filehandler ("%h/" + fileName + "%u.log");
Filehandler.setformatter (New Simpleformatter ());
Consolehandler Consolehandler = new Consolehandler ();
Consolehandler.setlevel (level);
Logger.getlogger (PackageName). AddHandler (Filehandler);
Logger.getlogger (PackageName). AddHandler (Consolehandler);
Logger.getlogger (PackageName). Setlevel (level);
catch (SecurityException ex) {
Ex.printstacktrace ();
catch (IOException ex) {
Ex.printstacktrace ();
}
}
public static void Main (string[] args) {
Enable all log output for the current package
String PackageName = "Javatest";
Setloglevel (PackageName, PackageName, Level.all);
Loggertest test = new Loggertest ();
}
}