Java Logging
Used to log4j and other log tools, unexpectedly do not know Java has also brought a log tool. If you are free today, let me know.
Let's look at a simple example:
Public classSystemtest {Private FinalLogger Logger = Logger.getlogger ("Alias"); @Test Public voidShowsystemproperty () {Properties props=system.getproperties (); Enumeration<Object> Keysiter =Props.keys (); System.out.println (Logger.getname ()); while(Keysiter.hasmoreelements ()) {Object key=keysiter.nextelement (); Logger.info (Key+ "\t\t:" +Props.getproperty (key.tostring ())); } }}
This example can be said to be the simplest use of logging. This looks no different from the log4j use.
In addition, when using log4j, you need to configure a log4j.properties, specify Adpater, specify the pattern of the log, then logging there is no similar function? Log4j supports namespace inheritance, does logging support it?
1, the basic knowledge of logging
A lot of basic knowledge online:
1) Official JDK documentation
2) http://www.vogella.com/tutorials/Logging/article.html
3) Http://en.wikipedia.org/wiki/Java_logging_framework
2. Log Environment initialization
The so-called log environment initialization, in fact, is Logmanager initialization. Through source reading, the process of Logmanager initialization is:
1.1 load Logmanager through the class loader
Logmanager can be customized, and custom Logmanager to inherit Java.util.logging.LogManager. If you do not have a custom Logmanager class, the default Logmanager is loaded.
You can configure system properties by Java.util.manager
1.2 creating Logmanager by reflection mechanism Object
1.3 Create Rootlogger , and read the properties of the log from the configuration file
Rootlogger is actually a logger subclass. Its name is "" and the default level is info.
and also to read the configuration file, the configuration file can be a configuration class, or it can be a property file.
The following is a configuration file that attributes files.
The location of the property file is specified by System properties, and its system property name is: Java.util.logging.config.file
If you do not have a custom profile, read the Logging.properties file from the Java_home\lib directory.
Below you can see the default configuration:
############################################################# Default Logging Configuration file## You can use a Different 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.logging.consolehandler# to also add the Filehandler,Use the following line instead. #handlers= Java.util.logging.FileHandler,java.util.logging.consolehandler# Default Global logging level.# This specifies which kinds of events is logged Acro ss# 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 console to INFO and above.java.util.logging . Consolehandler.level=INFOjava.util.logging.ConsoleHandler.formatter=java.util.logging.simpleformatter############################################################# 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= SEVERE
3. Add Logger Object
You can use the logger constructor to create an object, or you can use Logger.getlogger (name) to create an object.
When you add a logger object, the log level is set according to the configuration file, related to handler.
4. Process of recording logs
1) Create a LogRecord object based on the message content.
2) filter using logger filter filters.
3) log processing with all relevant handler of the Logger object and write to the appropriate location.
This note is written in favor of summary, these are I through the source view learned, do not want to war source space. There are examples of the links given above.
Java se:logging Framework Description