Java logging api was officially released by Sun in May 2002. It is a new application interface that has been provided since the j2se 1.4 version. It is easy to control and output log information to files, consoles or other user-defined places such as databases, emails, etc. So it is a tool for end users, system administrators, software service engineers, and developers to capture security vulnerabilities, check configuration correctness, track system bottlenecks, and investigate system runs incorrectly. JAVA&NBSP;LOGGING&NBSP;API's design goal is to:1. the ability to turn logging on or off at run time; 2. very precise control of the log, that is, the ability to turn on or off a particular function of the log; 3. can register the log service at runtime, and, of course, register a new Third-party Log service; 4. can provide connectivity services to enable java logging APIs can be connected to existing log systems (such as the operating system log function); 5. can support the display of high-priority information to end users. Output log as Java logging api provides a very convenient interface, you can see how convenient it is to use in the following example. Example 1: import java.util.logging.logger; // <= (1) public class HelloLogWorld { private static String Name = hellologworld.class.getname (); // <= (2) private static logger log = logger.getlogger (name); public void sub() { log.info ("Hello logging world"); // <= (3) } public static void main (String[] args) { hellologworld logworld = new hellologworld (); logworld.sub (); } &NBSP;&NBSP} indicates that: (1) logger is a class in Java logging api. Before you do this, you must make sure that the j2se you are using is 1.4 or higher. (2) The logger.getlogger method creates a Logger instance. Each logger instance must have a name, and the usual practice is to define the logger instance with the class name. The reasons for doing so are also mentioned later. The (3) log.info method is used to output log information. Executive Example 1 you can see that the console outputs the date time, class name, method name, and Hello logging world information. The type of output JAVA&NBSP;LOGGING&NBSP;API provides seven levels to control the output. These seven grades are:severe (highest grade) WARNING INFO CONFIG FINE FINER FINEST (lowest grade) Accordingly, the output method of these seven grades is also prepared for you in the logger class. The following is an example of using these methods to output log information. Case 2:IMPORT&NBSP;JAVA.UTIL.LOGGING.LOGGER;PUBLIC&NBSP;CLASS&NBsp leveltest { private static string name = HelloLogWorld.class.getName (); private static logger log = Logger.getlogger (name) public void sub () { log.severe ("Severe level"); // <= (1) log.warning ("Warning level"); // <= (2) log.info ("info Level "); // <= (3) log.config ("Config level"); // <= (4) log.fine ("Fine level"); // <= (5) Log.finer ("Finer level"); // <= (6) log.finest ("Finest level"); // <= (7) } Public static void main (String[] args) { leveltest test = new leveltest (); Test.sub ();  } 2 You may see that the console only outputs (1)-(3) of the information, (4)-(7) that the information is not being exported as expected. It doesn't matter, keep reading the following and you'll know why. Control output the level information below info in Example 2 above is not output because the default log output level is set to info, which means that only info or its rank is output, and the following levels are not output. You can change the output level by changing settings. There are two ways to change the output level, one is to use an external configuration file and the other is to use program encoding. The first method is usually used. The default external configuration file is the Lib/logging.properties file in the JRE. You can open this file and modify the following two behaviors:. LEVEL=&NBSP;ALLJAVA.UTIL.LOGGING.CONSOLEHANDLER.LEVEL&NBSp;= all after the modification, run again for example 2, this (1)-(7) content is output. You can modify the output level to any of the following values and try to see what the output changes. offsevere warning info config fine finer finest All custom Profiles If you do not want to use the default profile Lib/logging.properties provided by the system, you can also use your own defined configuration file. There are a number of ways to use a custom profile, and it is convenient to set up a custom profile by defining the system property value Java.util.logging.config.file. For example, at the execution Example 2 o'clock, use the following command:java -djava.util.logging.config.file=mylogging.properties Leveltest where mylogging.properties is your custom profile. If you want, you can also add the path name of the file. More output control In addition to the seven output levels mentioned earlier, the Logger class also provides the following three output information methods for programmers to debug: enteringexitingthrowing They are the same level of output as finer, respectively, for the program entrance, Exports and exceptions when the output. In addition, there are some ways to output information in the Logger class, which are left to detail later. Localization of output content Java logging api has taken into account the internationalization of the corresponding issue. You can use ResourceBundle to provide localized output information on the output. Here is an example to illustrate. Case 3:import java.util.logging.logger;public class localtest { private static string name = hellologworld.class.getname (); private static Logger log = Logger.getLogger (name, "local"); <= (1) public void sub () { log.severe ("Level.severe"); <= (2) log.warning (" Level.warning "); <= (3) log.info ("Level.info"); <= (4) log.config ("Level.config"); <= (5) log.fine ("Level.fine"); <= (6) log.finer ("Level.finer"); <= (7) &nbsP; log.finest ("Level.finest"); <= (8) } public static void main (String[] args) { LocalTest test = new Localtest (); test.sub ();  }} You can compare cases 2 and 3. You can see that an argument is added to the logger.getlogger method invocation of Example 3, and the output in (2)-(8) also changes. (1) The added string argument is the property file name when using the ResourceBundle configuration. The following is required to create this property file Local.properties as follows: Level.severe=severe levellevel.warning=warning levellevel.info=info levellevel.config=config levellevel.fine=fine levellevel.finer=finer levellevel.finest= Finest level now run example 3 to see that the output is not what is defined in (2)-(8). You might have thought, (2)-(8) The property key names in the Local.properties file are actually defined, and the output is the key value of the property key names. Well, maybe you should ask. If the attribute key name does not exist in the property file, the answer is simple, if it is not the attribute key name must be the output of the content, so it is output as it is.
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