Java program log: java. util. logging. Logger class,

Source: Internet
Author: User

Java program log: java. util. logging. Logger class,
1. 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 will not be displayed (but will also be written to files ).

Note:
High-level Logger does not display (or write) low-level records.
Low-level Logger displays (or writes) high-level records.


2. Create a Logger object

Static Logger getLogger (String name)
Searches for or creates a logger for a specified subsystem.
Static Logger getLogger (String name, String resourceBundleName)
Searches for or creates a logger for a specified subsystem.

Note: name is the name of Logger. Generally, a hierarchical namespace separated by dots is used to name Logger.
When the name is the same, only one Logger with the same name is created (different references point to the same logger, which means that different references can operate on the same Logger ).


3. simple instance-print Logger on the console
Public class TestLogger {public static void main (String [] args) {// create a Logger severeLogger = Logger at the SEVERE level. getLogger ("logger. severe "); // The name is logger. severe severeLogger. setLevel (Level. SEVERE); // set the logger level // create a WARNING level Logger warninglogger = Logger. getLogger ("logger. warning "); warninglogger. setLevel (Level. WARNING); // create the INFO-level Logger infoLogger = Logger. getLogger ("logger.info "); InfoLogger. setLevel (Level. INFO); // create a CONFIG-level Logger configLogger = Logger. getLogger ("logger. config "); // logs with a lower level than INFO do not display configLogger. setLevel (Level. CONFIG);/* ------------------------------------ Record Test ------------------------------ */infoLogger.info ("This Is An infoLogger record"); // record an INFO-level message infoLogger. warning ("Can low-level display advanced information? "); // The result shows that the low-level logger can display/record the High-level information warninglogger. warning ("this is the record of warninglogger"); warninglogger. severe ("Can low-level display advanced information? "); Warninglogger.info (" Can Advanced display low-level information? "); // The result shows that high-level logger cannot display/record low-level information severeLogger. severe ("this is the record of severeLogger"); severeLogger. warning ("Can Advanced display low-level information? "); ConfigLogger. config ("this is the record of configLogger"); // Logger below INFO level will not display the same level/lower level message Logger testLogger1 = Logger. getLogger ("logger.info"); // Logger with the same name only creates one System. out. println ("are logger of the same name equal:" + (infoLogger = testLogger1); testLogger1.setLevel (Level. SEVERE); // set it to a high-level infoLogger.info ("this is the record of infoLogger"); infoLogger. setLevel (Level. INFO); // set it to infoLogger.info ("this is the record of infoLogger ");}}

 

Output result:

4. Handler class of Logger

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.
Java. util. logging. Handler
Java. util. logging. MemoryHandler
Java. util. logging. StreamHandler
Java. util. logging. lelehandler
Java. util. logging. FileHandler
Java. util. logging. SocketHandler

 

V. simple instance-write a Logger File
Public class TestLogger {public static void main (String [] args) throws IOException {// create a Logger severeLogger = Logger at the SEVERE level. getLogger ("logger. severe "); // The name is logger. severe severeLogger. setLevel (Level. SEVERE); // set the logger level // create a WARNING level Logger warninglogger = Logger. getLogger ("logger. warning "); warninglogger. setLevel (Level. WARNING); // create the INFO-level Logger infoLogger = Logger. getLogg Er ("logger.info"); infoLogger. setLevel (Level. INFO); // create a CONFIG-level Logger configLogger = Logger. getLogger ("logger. config "); // logs with a lower level than INFO do not display configLogger. setLevel (Level. CONFIG); File logDir = new File ("f:/logTest", "logDir"); // create a File object parameter: parent directory, subdirectory logDir. mkdirs (); // create a multi-level directory File logFile = new File (logDir. getAbsolutePath (), "logTest. log "); // create the file object FileHandler fileHandler = new FileHandler (logFile. ge TAbsolutePath (), 10240, 1, true); // write the log file, parameter: path, maximum number of bytes, number of files to be used, and whether to append the file. (When the maximum number of bytes is reached, all the original records will be deleted and the record will be restarted) fileHandler. setLevel (Level. ALL); // Similarly, high-level FileHandler does not record low-level messages, and ALL-level FileHandler records ALL levels of messages. // fileHandler. setFormatter (new MyLogFormatter (); // set the custom log record format (the default format is XML), which will be mentioned later. InfoLogger. addHandler (fileHandler); // Add the Handler object infoLogger.info ("infoLogger's info record"); // write the record to the log file infoLogger. config ("config record of infoLogger"); // records lower than the logger level will not be written into the log file configLogger. addHandler (fileHandler); configLogger.info ("configLogger info record"); configLogger. config ("configLogger config record"); // If the FileHandler level is ALL or lower than INFO, the Logger of config level will also be written to the log file, but it will not be printed in the console }}

 

Output result: (the written log file can be found in drive F)


Similarly, the Logger log () method can also write files.

Open the file and you will find that the default log format is xml.
You can also customize the logger format.
It must be defined by Formatter.

6. Logger Formatter

Formatter supports formatting LogRecords.
Generally, each logging Handler has an associated Formatter.
Formatter accepts LogRecord and converts it to a string.
The LogRecord object is used to transmit log requests between the log framework and a single log Handler.

Several Formatter types:
Java. util. logging. Formatter
Java. util. logging. SimpleFormatter
Java. util. logging. XMLFormatter

Note:
Java. util. logging. Formatter, which must be separated from java. util. Formatter


7. simple instance --- custom Formatter
/** Custom formatting class */class MyLogFormatter extends Formatter {@ Override public String format (LogRecord record) {return record. getSourceClassName () + ":" + record. getSourceMethodName () + ":" + record. getLevel () + ":" + record. getMessage () + "\ n ";}}

 

For other formatting settings, refer to the java. util. Formatter class in the API.

Output result:

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.