201711671224 "Java Programming" Chapter 15 learning summary

Learning content summary

Logs API

The starting point is to use the log Logger class, create LOgger examples are many elements to be processed, the constructor LOgger class identifies the class is not the same java.util.logging package can not create new direct, to get Logger instance is protected,, must use the static method getlogger Logger ().

Call getLogger (), you must specify Logger instance belongs namespace name space. "" As a distinguished level, the same level of the same name space Logger parent Logger configuration.

Logger is usually made in that class, the namespace will be named the full name of that class. Often, it will be made by way Logger.

import java.util.logging.*;

public class LoggerDemo {
public static void main(String[] args) {
    Logger logger = Logger.getLogger(LoggerDemo.class.getName());
    logger.log(Level.WARNING, "WARNING 訊息");
    logger.log(Level.INFO, "INFO 訊息");
    logger.log(Level.CONFIG, "CONFIG 訊息");
    logger.log(Level.FINE, "FINE 訊息");
}
}

Results are as follows:

If the client calls the log Logger instance () method, first will be based on Level filter information, and then look at whether the instance of Logger set the Filter interface, if there is one and it isLoggable () returns true, calls will publish handle instances () method, the handle may be set Filter own instance, if the isLoggable and which () returns true, calls the format formatter instance () method of formatting information, and finally the object call message formatted output.

Handle与Formatter

Handle divided MemoryHandle and StreamHandle, which is divided into StreamHandle ConsoleHandle, FileHandle and SockeHandle.

Logger can AddHandle () Handle new instance, using removeHandle () Handle removed. For example look at an example:


public class HandlerDemo {
public static void main(String[] args) throws IOException {
    Logger logger = Logger.getLogger(HandlerDemo.class.getName());
    logger.setLevel(Level.CONFIG);
    FileHandler handler = new FileHandler("%h/config.log");
    handler.setLevel(Level.CONFIG);
    logger.addHandler(handler);
    logger.config("Logger 組態完成");
}
}

FileHandle designation mode when creating a string, "% h" to represent the user's root directory, the system can also be achieved using a temporary directory "& t", or by using "% g" document number automatically.

The default is the XMLFormatter Formatter FileHandle, seen in front SimpleFormatter ConsoleHandle use the default, which is a subclass of Formatter two, () method Handle set by the Formatter setFormatter.

Pattern and Matcher

  • Expressions used in the program, you must first do the analysis, validation rules for the operation of expression, regular expression syntax to determine the correct string comparison.
  • Examples are java.util.regex.Pattern regular expression representing an object in the JVM, Pattern constructor is marked as private, must be acquired by the static method compile Pattern ().

  • After obtaining Pattern example, use split () method represented by the specified string cutting rule, equal to the effect of using String split () method.

JDK8 API Enhancements

String, join (): String new join () static method to specify a comma-separated connection between each string.

Arrays:

  • New parallelPrefix (), parallelSetAll () on Arrays and parallelSort () method.
  • parallelPrefix () method can be specified xxxBinaryOperator instance, a similar the Stream reduce () procedure.
  • parallelSetAll () method, which the array is initialized or reset its index of each element.
  • parallelSort () method, the specified array can be divided into sub-arrays of parallel manner and are sorted, and then merge sort.

International basis

Depending on the application users in the region, presents a different language, date format called localization, if the application is designed, you can love without modifying the application cases, depending on the user directly in different languages ​​such as date format, this design consider becoming an international, referred to as i18n.

Three important concepts of international regional information resource bundle with base name. Area information corresponding to the class Locale, ResourceBundle object is a resource package on behalf of the JVM object. Specified time () represent the same set of information for each resource pack but different regions share the same base name, use ResourceHandle of getBundle name is in the specified base name.

Use ReasourceBUndle is how to obtain information corresponding to the document based on the basis of name:

  1. Using the specified Locale object to obtain information about the document.
  2. Use Locale.getDefault () object is made to obtain information about the document.
  3. Use the base name to obtain information document

Date can be used to get the full date and time, you can simply use the toString () Date of obtaining the written description, or use the DateFormat format dates. If the view Date of API documentation, you will find many ways are no longer recommended, it recommended the use of relevant methods Calendar replaced.

Log API Introduction

java.util.logging package provides a log function related classes and interfaces. No extra configuration logging component, you can use the standard Java platform is its advantage, the starting point is to use the log Logger class Logger class constructor marked as protected, the class is not the same java.util.logging package can not create new direct, to obtain Logger instance, you must use the static method getLogger Logger (). When you call getLogger (), you must specify the Logger instance belongs namespace. Name space. "" As the level of distinction. The same namespace hierarchy Logger, the same as its parent Logger configuration. Logger is usually made in which class, the namespace which will be named the full name of the class. In simple terms, Logger is the starting point for recording the information, the information to be output must be filtered through the Filter Logger Level, and then filtered through Level and Filter Handler, actions, formatted information to action Formatter, output actually Handler is responsible.

Specify log level

Logger and Handler will default to Level filter information based on, if the parent does not make any changes, made Logger instance of Logger configuration is Logger.GLOBAL LOGGER configuration Logger instance NAME namespace configuration settings of this example is INFO , by Logger example getParent () to obtain the parent Logger instance, can be obtained by setting Level examples getLevel (). Logger information processing Logger will spread to the Father, that is to say, in any case did not do under the configuration settings, default instance of Logger achieved, the hierarchy must be greater than or equal to Logger.GLOBAL LOGGER NAME namespace Logger instance set Level .INFO, can be output information.

 

Use Handler and Formatter

MemoryHandler not be formatted log information, the information will be temporarily stored in memory buffer than the buffer size until only the information output to the specified target Handler. StreamHandler OutputStream instance may specify their own use when outputting information, it will be specified subclasses Formatter formatting information, when ConsoleHandler created automatically designated as OutputStream System.err, so the log information is displayed on the console. FileOutputStream used when creates a log output is created FileHandler, file locations and names can use pattern (Pattern) specified string.

Custom Handler, Formatter, Filter

If the results of Handler java.util.logging package provided do not meet the requirements, can be inherited Handler class, operating abstract method publish (), flush (), close () to customize Handler, it recommended to consider the information filtering and formatting operation.

 

Code debugging and problem solving in the process

No

 

Other (perception, thinking, etc.)

no

Reference material

  • "Java Programming"

Guess you like

Origin blog.csdn.net/nemeziz/article/details/85042795