JDK built-in log system and jdk built-in log

Source: Internet
Author: User

JDK built-in log system and jdk built-in log

1. Log functions in Java applications

========================

Generally, Java applications require logging. Currently, the mainstream log recording method is to introduce log4j in the application and use log4j to generate logs. In fact, JDK has its own log system, which is easy to use.

 

2. Use of Java built-in logs

========================

Classes related to the jdk built-in log system are included in the java. util. logging package. The following example illustrates how to use the JDK built-in log system from simple to complex.

To enable the application to record logs, you must first obtain a logger in the application. All the logs that the application wants to record are sent to this logger, logger will help applications record logs.

 

2.1 directly use the global logger in the log system to record logs

----------------------------------------------------

In the following example, the global logger in the log system is obtained to record logs in the Test application. This global logger is used to record several logs.

Import java. util. logging. logger; public class Test {public static void main (String [] args) {Logger logger = Logger. getGlobal (); // obtain the built-in Global logger in the log system // use the obtained global logger to record seven logger logs. severe ("level. severe "); logger. warning ("level. warning "); logger.info (" level.info "); logger. config ("level. config "); logger. fine ("level. fine "); logger. finer ("level. finer "); logger. finest ("level. finest ");}}

After compiling the Test application with the javac Test. java command, run the Test application with the java Test command. You can see the logs output by the global logger in the Test application, as shown below:

In the log output below, each two lines is a log record. The first line of each log record is the date and time of the log output, and the classes and methods for output. The following line shows the log level and specific log information of this log record ).

Since the Test application above outputs seven log records, 14 lines of log information are output after the Test application is run. Each two lines is a log record.

Zzl @ ZZL-PC/e/code/javacode/CoreJava $ javac Test. javazzl @ ZZL-PC/e/code/javacode/CoreJava $ java Test February 24, 2017 6:29:14 PM Test main severe: level. severe October 11, 2017 6:29:14 Test main warning: level. warning February 24, 2017 6:29:14 Test main information: level.info February 24, 2017 6:29:14 Test main configuration: level. config February 24 6:29:14 Test main details: level. fine February 24, 2017 6:29:14 in the afternoon Test main more detailed: level. finer February 24, 2017 6:29:14 Test main very detailed: level. finest

 

2.2 Use an application-specific logger to record logs

-----------------------------------------

In addition to using the Global logger in the log system, in actual applications, we should set a separate logger for each application.

You can use the following statement to create a logger named com. mycompany. myapp for the myapp:

Logger myLogger = Logger. getLogger ("com. mycompany. myapp ");

The logger name and package name are similar and hierarchical. However, it differs from the package name because, for a package, there is no semantic relationship between a package and its parent package, but the logger shares some attributes. For example, the name is com. the Logger of mycompany is named com. company. the father of the myapp logger, if the log recorder com. the log level (LogLevel) and logger com. mycompany. myapp inherits this log level.

Package com. mycompany; import java. util. logging. logger; import java. util. logging. level; public class MyApp {public static void main (String [] args) {// obtain the name com. mycompany. logger logger = Logger. getLogger ("com. mycompany. myapp "); // use the obtained logger to record seven logger logs. severe ("level. severe "); logger. warning ("level. warning "); logger.info (" level.info "); logger. config ("level. config "); logger. fine ("level. fine "); logger. finer ("level. finer "); logger. finest ("level. finest ");}}

Compile and run the above MyApp application. Seven log records are output.

Zzl @ ZZL-PC/e/code/javacode/CoreJava $ javac-d. com/mycompany/MyApp. javazzl @ ZZL-PC/e/code/javacode/CoreJava $ java com. mycompany. myApp February 24 8:04:36 com. mycompany. myApp main is serious: level. severe August 1, 2017 8:04:36 com. mycompany. myApp main warning: level. warning February 24 8:04:36 com. mycompany. myApp main information: level.info, January 1, 2017 8:04:36 com. mycompany. myApp main configuration: level. config February 24 8:04:36 com. mycompany. myApp main details: level. fine February 24 8:04:36 com. mycompany. myApp main: level. finer, October 11, 2017 8:04:36 com. mycompany. myApp main is very detailed: level. finest

The two examples above describe the basic usage of the logger.

 

2.3 Log Level)

--------------------------

The log level indicates the different severe programs in which the application wants to tell the outside world information, such as critical and not so important. Each logger in the built-in Logging System of JDK can have seven loglevels, from advanced to low. They are: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST. After a logger is created, the default log level is INFO, which indicates that the logger can output logs of INFO and above levels, that is, logs of the SEVER, WARNING, and INFO levels can be output, but logs of the CONFIG, FINE, FINER, and FINEST levels cannot be output, even if the program calls the corresponding output statement, the corresponding level of log is not output.

For details, see the example below.

Package com. mycompany; import java. util. logging. logger; import java. util. logging. level; public class MyApp {public static void main (String [] args) {// obtain the name com. mycompany. logger logger = Logger. getLogger ("com. mycompany. myapp "); // use the obtained Logger to record seven log records. // The default level of each Logger is INFO, therefore, the first three can output // The last four cannot output logger. severe ("level. severe "); logger. warning ("level. warning "); logger.info (" level.info "); logger. config ("level. config "); logger. fine ("level. fine "); logger. finer ("level. finer "); logger. finest ("level. finest ");}}

After compilation and running, you can see the following output:

Zzl @ ZZL-PC/e/code/javacode/CoreJava $ javac-d. com/mycompany/MyApp. javazzl @ ZZL-PC/e/code/javacode/CoreJava $ java com. mycompany. myApp February 24 8:18:50 com. mycompany. myApp main is serious: level. severe August 1, 2017 8:18:50 com. mycompany. myApp main warning: level. warning February 24 8:18:50 com. mycompany. myApp main information: level. infozzl @ ZZL-PC/e/code/javacode/CoreJava $

It can be seen that no log records are output at the INFO level, and only the first three levels of log records are output.

Of course, you can set the log level for the logger in the log system configuration file. mycompany. the myapp logger can be found in jre/lib/logging. in properties, set the log level. Add the following sentence at the end of the configuration file:

Com. mycompany. myapp. level = FINE

In addition, in addition to the log level of the logger, the processor also has its own log level, and the log level of the processor is higher than the Log Level of the logger, for details, see the "log processor" section below ".

 

2.4 log processor (Handler)

---------------------------

A log processor is the destination where logs are actually sent. It can be a console, a file, or a network server. The three commonly used built-in log processors are: ConsoleHandler representing the console, FileHandler representing the file, and SocketHandler representing the network server.

After a logger is created, if a processor is not explicitly specified for it, it will send the log records to ConsoleHandler by default and output the logs to System. err. Note that the logger not only sends the log records to its processor, but also sends the log records to the processor of its parent logger.

The log processor has its own log level like the log recorder. The default logging level of eaglehandler is

Java. util. logging. lelehandler. level = INFO

In your own program, you can specify your own log processor for the custom logger. See the following example:

Package com. mycompany; import java. util. logging. logger; import java. util. logging. level; import java. util. logging. handler; import java. util. logging. consoleHandler; import java. util. logging. fileHandler; import java. io. *; public class MyApp {public static void main (String [] args) {// get the name com. mycompany. logger logger = Logger. getLogger ("com. mycompany. myapp "); logger. setLevel (Level. ALL); // set the log manager's log level logger. setUseParentHandlers (false); // com. mycompany. myapp this logger sets a lelehandler processor Handler handler = new ConsoleHandler (); handler. setLevel (Level. ALL); // set the Log Level of the processor. addHandler (handler); // use it again for com. mycompany. myapp this logger sets a FileHandler processor FileHandler handler2 = null; try {handler2 = new FileHandler (); logger. addHandler (handler2);} catch (IOException e) {e. printStackTrace ();} logger. severe ("level. severe "); logger. warning ("level. warning "); logger.info (" level.info "); logger. config ("level. config "); logger. fine ("level. fine "); logger. finer ("level. finer "); logger. finest ("level. finest ");}}

After the above program is compiled and run, it will send logs to both the Console and File. For FileHandler, by default, it stores the log records to a file named javan. log (n is a number. When you run the same application multiple times, n will be different each time, so that logs will be recorded in different files each time) in the xml file, the file is located in the user by default. the logs in the home attribute. The log file generated in this example is c: \ users \ user1 \ java0.log.

These are the basic knowledge of the log processor. Of course, for more details about the log processor, please refer to the detailed API instructions in the java. util. logging package.

 

3. Log System Configuration File

====================

Various attributes of the log system, such as the log level, can be modified through the configuration file of the log system. By default, the configuration file of the log system is JAVA_HOME/jre/lib/logging. properties. If you want to set a unique configuration file for the application, you can use the java. util. logging. config. file Attribute when enabling the application, for example, as shown below:

Java-Djava. util. logging. config. file = configFile MyApp

Of course, if you want to modify the default configuration of the log system, you can directly edit the jre/lib/logging. properties file.

Compile your own log configuration file. Refer to the jre/lib/logging. properties file.

 

4. Other problems

========

There are still many details about the log system that comes with JDK. Here is a basic knowledge. You can use it if you understand this. For more details, refer to java. util. logging package documentation. In addition, it is worth noting that the basic concepts should be clarified during the learning process. Be sure to clearly identify the default configuration files of the log recorder, log record, log processor, log level, and log system.

Related Article

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.