Proper use of java.util.logging packages

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.