2. java. util. logging. Logger usage details,
1. Introduction to java. util. logging. Logger
Java. util. logging. Logger is nothing new, and 1.4 is available. But because of the existence of log4j, this logger has been silenced,
In fact, the built-in logger of jdk is more convenient than log4j in some tested code.
2. Logger level
More detailed than the log4j Level, all defined in java. util. logging. Level.
The levels are listed in descending order as follows:
· SEVERE (maximum value)
· WARNING
· INFO
· CONFIG
· FINE
· FINER
· FINEST (minimum value)
In addition, there is a level OFF that can be used to Disable Logging and enable logging for ALL messages with level ALL.
The default logger level is INFO. Logs lower than INFO are not displayed.
The default Logger level is defined under the lib of the jre installation directory.
# Limit the message that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = INFO
Iii. simple instance code
package com.my.utils;import java.io.IOException;import java.util.logging.ConsoleHandler;import java.util.logging.FileHandler;import java.util.logging.Level;import java.util.logging.Logger;public class LoggerLog { public static void main(String[] args) { //create logger Logger logger = Logger.getLogger("LoggerLog"); logger.setLevel(Level.INFO); //create console handler ConsoleHandler handler = new ConsoleHandler(); handler.setLevel(Level.ALL); //add console handler to logger logger.addHandler(handler); try { //add fileHandler FileHandler fileHandler = new FileHandler("D:\\java\\test\\test.txt"); fileHandler.setLevel(Level.ALL); //add fileHandler to logger logger.addHandler(fileHandler); } catch (SecurityException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.info("Start"); logger.info("Running"); logger.info("End"); } }
At this time, you will find that "Start" "Running" "End" is printed twice on the console, because the log is output once and ConsoleHandler outputs it once.
The Handler object obtains the log information from the Logger and exports the information. For example, it can write the information to the console or file, send the information to the Network Log service, or forward it to the operating system log.
You can disable Handler by executing setLevel (Level. OFF) and re-enable it by executing the appropriate setLevel.
The Handler class usually uses the LogManager attribute to set the default values of the Filter, Formatter, and Level of the Handler.
Check the output log of disk D as follows:
<?xml version="1.0" encoding="GBK" standalone="no"?><!DOCTYPE log SYSTEM "logger.dtd"><log><record> <date>2018-03-25T20:35:42</date> <millis>1521981342189</millis> <sequence>0</sequence> <logger>LoggerLog</logger> <level>INFO</level> <class>com.my.utils.LoggerLog</class> <method>main</method> <thread>1</thread> <message>Start</message></record><record> <date>2018-03-25T20:35:42</date> <millis>1521981342292</millis> <sequence>1</sequence> <logger>LoggerLog</logger> <level>INFO</level> <class>com.my.utils.LoggerLog</class> <method>main</method> <thread>1</thread> <message>Running</message></record><record> <date>2018-03-25T20:35:42</date> <millis>1521981342298</millis> <sequence>2</sequence> <logger>LoggerLog</logger> <level>INFO</level> <class>com.my.utils.LoggerLog</class> <method>main</method> <thread>1</thread> <message>End</message></record></log>
It can be seen that the default log format is xml. We use logs to clearly view operation-related information. This format is a bit messy, so we need to customize the logger format. It must be defined by Formatter.
4. Define the Formatter of the output log
package com.my.utils;import java.text.SimpleDateFormat;import java.util.logging.Formatter;import java.util.logging.LogRecord;public class MyFormat extends Formatter { @Override public String format(LogRecord log) { // TODO Auto-generated method stub SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:MM:ss S"); return log.getLevel() + ": " + format.format(log.getMillis())+" " + log.getMessage() +"\n"; }}
Then add the Format to fileHandler.
fileHandler.setFormatter(new MyFormat());
At this time, the output log file is clear. You can define the appropriate Format according to the actual needs of the project.
INFO: 21:03:49 791 Start
INFO: 21:03:49 909 Running
INFO: 21:03:49 913 End
Formatter supports formatting LogRecords.
Generally, each logging Handler has an associated Formatter. Formatter accepts LogRecord and converts it to a string.
Some formatter (such as XMLFormatter) needs to wrap the header and tail strings around a set of formatting records. You can use the getHeader and getTail methods to obtain these strings.
The LogRecord object is used to transmit log requests between the log framework and a single log Handler.
LogRecord (Level level, String msg) constructs LogRecord with the given Level and message value.