"Java" Java comes with the Java.util.logging.Logger log feature

Source: Internet
Author: User
Tags log log string format

Occasionally read to an article, notice that Java comes with the logger log function, specially to take a look carefully, record a bit.

1.Java with log function, default configuration

Default configuration of the ①logger, located in the Logging.properties in Lib in the JRE installation directory

The contents of the ②logging.properties log file are as follows:

############################################################# Default Logging Configuration file## You can use a diff  Erent file by specifying a filename# and the Java.util.logging.config.file system property. # for example java-djava.util.logging.config.file=myfile######################################################### ################################################################ Global properties##############################  ############################### "Handlers" specifies a comma separated list of log Handler # classes. These handlers'll be installed during VMS startup.# Note that these classes must is on the system classpath.# by default We only configure a consolehandler, which'll only# show messages at the INFO and above levels.handlers= Java.util.loggin g.consolehandler# to also add the Filehandler with the following line instead. #handlers = Java.util.logging.FileHandler, ja va.util.logging.consolehandler# Default Global Logging level.# This specifies whichKinds of events is logged across# all loggers.  For any given facility this global level# can is overriden by a facility specific level# Note that the Consolehandler also Have a separate level# setting to limit messages printed to the console. level= info############################################################# Handler specific properties.# Describes Specific configuration info for handlers.############################################################# default file Output is in user ' s home directory.java.util.logging.FileHandler.pattern =%h/java% U.logjava.util.logging.filehandler.limit = 50000java.util.logging.filehandler.count = 1java.util.logging.filehandler.formatter = java.util.logging.xmlformatter# Limit The message that is printed on the cons Ole to INFO and above.java.util.logging.ConsoleHandler.level = INFOjava.util.logging.ConsoleHandler.formatter = java.util.logging.simpleformatter# Example to customize the Simpleformatter output format # To print one-line log message LikE this:#      < Level>:<Logmessage>[<Date/time>]## java.util.logging.simpleformatter.format=%4$s:%5$s [%1$tc]%n############################################ ################# Facility specific properties.# provides extra control for each logger.############################### ############################## For example, set the Com.xyz.foo logger to only log severe# Messages:com.xyz.foo.level = SE VERE
View Code

The 1th concern in the log file is:

  

The 2nd to be concerned is:

The 3rd to be concerned is:

The 4th to be concerned is:

2. TheJava.util.logging.Level class sets the log level class setting

Can be seen from the API, out of the following level levels, as well as off and all two levels.

If set to Level.info level, the log will only display info and the above levels.

 

Simple use of 3.Logger

First, make it clear that the Java.util.logging.Logger initialization method

  

Static

Name represents your logger name, and if you specify Getlogeger the same name, only one object is created

Static

Name represents your logger, Resourcebundlename represents the localized logger name, which is the log file to the local disk, and the name of each logger

Next, look at the effect:

  

 PackageCom.sxd.util;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;Importorg.junit.Test;/*** Test the log log function with Java *@authorSxd **/ Public classTestlog {@Test Public voidTest () {Logger log1= Logger.getlogger ("Log-test");        Log1.setlevel (Level.info); Logger log2= Logger.getlogger ("Log-test"); System.out.println ("Log1 and log2 are equal:" + (Log1 = = log2));//trueLogger Log3 = Logger.getlogger ("Log-test"); System.out.println ("Log1 and Log3 are equal:" + (Log1 = = Log3));//falseLog3.setlevel (level.warning); Log1.info ("Info level printing: Log information at info level"); Log3.info ("Warning Level Printing: Log information at info level");//can't print.Log3.severe ("Warning level printing: Log information at severe level"); }}
View Code

From this, it can be proved that the log level set by ① can only print to this level and high level of log information; ② logger with the same name will only create one.

4. Set Filehandler, set for log localization

 PackageCom.sxd.util;Importjava.io.IOException;ImportJava.util.logging.ConsoleHandler;ImportJava.util.logging.FileHandler;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;Importorg.junit.Test;/*** Test the log log function with Java *@authorSxd **/ Public classTestlog {@Test Public voidTest ()throwsSecurityException, ioexception{Logger log1= Logger.getlogger ("Log-test"); Consolehandler Consolehandler=NewConsolehandler ();        Consolehandler.setlevel (Level.all);                Log1.addhandler (Consolehandler); Filehandler Filehandler=NewFilehandler ("D:/testlog%g.log");        Filehandler.setlevel (level.warning);                Log1.addhandler (Filehandler); Log1.info ("All Level Printing: Log information at info level"); Log1.severe ("Warning Level Printing: Severe level log Information"); }}
View Code

Console:

"Note" Here the console prints the same log two times because the Java default has a Consolehandler set, but this level is the info level.

and the program has reset a new Consolehandler, this level is all, two different, so all performed the print, so printed two times exactly the same.

Local log file:

Note: Only one log is printed, which can be understood according to the code log level.

The key log file is in XML format because it explains the relevant settings for Filehandler, which is the default XML format

"NOTE 2" Filehandler specifies the log file name, which has the following specifications:

5. Setting a custom log format for localized log files

Java.util.logging.LogRecord;

Java.util.logging.Formatter;

 PackageCom.sxd.util;Importjava.io.IOException;ImportJava.util.logging.ConsoleHandler;ImportJava.util.logging.FileHandler;ImportJava.util.logging.Formatter;ImportJava.util.logging.Level;ImportJava.util.logging.LogRecord;ImportJava.util.logging.Logger;Importorg.junit.Test;/*** Test the log log function with Java *@authorSxd **/ Public classTestlog {@Test Public voidTest ()throwsSecurityException, ioexception{Logger log1= Logger.getlogger ("Log-test"); Filehandler Filehandler=NewFilehandler ("D:/testlog%g.log");        Filehandler.setlevel (level.warning); Filehandler.setformatter (NewMyFormatter ());                Log1.addhandler (Filehandler); Log1.severe ("Warning Level Printing: Severe level log Information"); }        classMyFormatterextendsformatter{@Override PublicString format (LogRecord record) {returnRecord.getloggername ()+ ">>" +Record.getlevel ()+ ">>" +Record.getmessage (); }            }    }
View Code

======================================= here, =========================================.

"Java" Java comes with the Java.util.logging.Logger log feature

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.