Consider the following scenarios:
(1). Depending on the program output, it is good to help the programmer debug the program. When writing a small program, you can constantly add the System.out.print () statement to the program to see the state of the program, in large systems, it is obvious that this is a very wrong approach, you can consider the debug information through the Log processing tool, output to a text file, Then view the contents of the file.
(2). After the system began to run, the System Manager needs to know the operation of the system, including but not limited to the system start run time, system shutdown time, the system is currently processing tasks, etc., these states need to output to a formatted log file, for managers to view;
(3). In the writing system, even the system is running, errors can occur, causing the system to crash-regardless of whether the system crashes itself can not be accepted, the system crashes after the failure to find the cause of the crash is absolutely unacceptable. Therefore, the Log management tool will need state information when the system has a critical error, such as what is being performed when an error occurs, what data is processed, and put into the log.
(4) .....
The above situation involves two requirements: #1. Record information; #2. Classification record information. Well, it's actually a requirement, and we can think of it as two.
Logging information is the output of the information string to an output stream, the output stream may flow to the destination, whether it is a long-term storage of files, a longer-lasting database or a temporary view of the console is required by the programmer, and the classification of information work, when the output, using the keyword info (general information), Debug (Debugging information), error (Errors), and fatal (Critical error) are distinguished. Another benefit of differentiating is that you can configure the system to output only error information and automatically block other information so that debug output statements throughout the program are not deleted in one sentence.
The Java.util.logging Log toolkit that comes with the JDK, third-party log4j, and others follow this line of thinking to deal with the log, just to make it easier for programmers to handle logs differently, and some other subtle distinctions.
1. The java.util.logging package that comes with the JDK.
Java.util.logging is a log processing package in the JDK standard library, JDK1.4 and later available. The toolkit that comes with JDK, of course, is obvious in ease of use ... When separating the log output information, it is also very benchmarking, see the table below
| level |
SEVERE |
warning |
INFO |
config |
fine& nbsp; |
Finer |
FINEST |
| Call method |
Severe () |
Warning () |
info () |
Config () |
Fine () |
Finer () |
Finest () |
| meaning |
Critical |
warning |
Info |
Configuration |
Good |
Good |
Best |
Different levels of output, called different methods can be. Levels also set different priorities, from left to right priority descending, in the configuration output category, specify a keyword, by the keyword and the priority above the keyword represents the information will be output to the log output stream, low priority will be automatically blocked. For example, if the Set keyword is WARNING, then only the output of the severe () method and the WARNING () method will be written to the output stream, and info () will be masked.
As mentioned above, when using the log Processing tool, to configure the content as needed, such as which level of information should be output, the output destination is a file or console, and even the format of the output information is plain text or XML format text, the output should be 24 hours or 12 hours to indicate ... As a matter of course, you specify the above in a configuration file.
Let's take a look at the example (run it over and try it):
ImportJava.util.logging.Logger; Public classLogtest {StaticLogger Logger = Logger.getlogger (logtest.class. GetName ()); Public Static DoubleDivision (intValue1,intvalue2) { Doubleresult = 0; Try{result= value1/value2; } Catch(ArithmeticException e) {logger.warning ("Divide by 0." Error "); E.printstacktrace (); } returnresult; } Public Static voidMain (string[] args) {System.out.println (Division (5, 0)); } } View Code
In this simple example, first get a Java.util.logging.Logger object Logger, specify Logger name as the class name, or another string, and then where you want to output the log, Just use the Logger object to invoke the appropriate method, such as Logger.info (), logger.warning (). This example uses the default configuration, the log output stream destination is the console, and the log output level is info.
The next example is to configure the log output stream destination and the log output level using a custom configuration file.
First look at the source code, as follows
ImportJava.io.FileInputStream;ImportJava.io.InputStream;Importjava.io.IOException;ImportJava.util.logging.Logger;ImportJava.util.logging.FileHandler;ImportJava.util.logging.Handler;ImportJava.util.logging.Level;ImportJava.util.logging.LogManager;ImportJava.util.logging.LogRecord;ImportJava.util.logging.SimpleFormatter; Public classlogtest{StaticLogger Logger = Logger.getlogger (logtest.class. GetName ()); StaticLogmanager Logmanager =Logmanager.getlogmanager (); Static{InputStream InputStream=NULL; FileInputStream FIS=NULL; Try{FIS=NewFileInputStream ("Log.properties"); Logmanager.readconfiguration (FIS); Logmanager.addlogger (logger); }Catch(IOException e) {e.printstacktrace (); }finally{fis.close (); } } Public Static DoubleDivision (intValue1,intvalue2) { Doubleresult = 0; Try{result= value1/value2; }Catch(ArithmeticException ex) {Logger.severe ("Divided by 0 error"); Logger.warning ("Divided by 0 error"); Logger.info ("Divided by 0 error"); Logger.config ("Divided by 0 error"); Logger.fine ("Divided by 0 error"); Logger.finer ("Divided by 0 error"); Logger.finest ("Divided by 0 error"); System.out.println (Ex.getmessage ()); } returnresult; } Public Static voidMain (string[] args) {System.out.println (Division (5,0)); }}View Code
In this code, Logmanager reads the log.properties configuration file under the current directory by calling the Readconfiguration () method and sets the configuration of the current logger instance logger through the Addlogger () method. When using the log processing tool, it is still a simple logger.info () way of invocation.
Next look at the configuration in the Log.properties file, or refer to the JDK default configuration file, located in%java_home%\jre\lib\logging.properties.
Log.properties
# "Handlers" specifies a comma separated list of log Handler #handlers = java.util.logging.consolehandlerhandlers= Java.uti L.logging.filehandler, # Default Logging level: level= CONFIG # Default file output is in "D:\project_java\java\log" Directory.java.util.logging.FileHandler.pattern = D: /project_java/java/log/log%g.xmljava.util.logging.filehandler.limit = 50000java.util.logging.filehandler.count = 3java.util.logging.filehandler.formatter = java.util.logging.SimpleFormatter # The message that is printed on the C Onsole to CONFIG and above.java.util.logging.ConsoleHandler.level = CONFIGjava.util.logging.ConsoleHandler.formatter = Java.util.logging.XMLFormatter
Handlers specify output to console or file, specified with full class name, console is Java.util.logging.ConsoleHandler, file is Java.util.logging.FileHandler
If handlers specifies a file, specify the value of Java.util.logging.FileHandler.Pattern, determine the output address of the log file, and the naming format. The value of the%g in the format is described in JDK: Level=config indicates that the log output level is config, when there is no prefix, whether it is a console or a file, the output level is a config, individually specified can be used Java.util.logging.consolehandler=info.
2. log4j API
With the log processing tool that comes with the JDK, why use the log4j API? This article is very detailed and concludes that Log4j has more features than the JDK's own API, which is easy to implement using the JDK's own API, but if you already have a ready-made toolkit, why reinvent the wiper-after all, it takes a lot of effort to reinvent things, It's as important as the wheel!
In fact, the JDK comes with an API that references the implementation of log4j. Many of the concepts are the same, except in defining the output level, and not as rigorous as the former, see the following table:
FATAL (critical), error (wrong), WARN (warning), info (info), debug (Debug).
| FATAL |
ERROR |
WARN |
INFO |
DEBUG |
| Critical Error |
Error |
Warning |
Information |
Debugging |
log4j configuration file, can be a properties file, can also be an XML file, to the properties file as an example of the introduction, first look at the source code, as follows.
ImportOrg.apache.log4j.Logger;ImportOrg.apache.log4j.ConsoleAppender; Public classTestlog {/** * @paramargs*/ Private StaticLogger Logger=logger.getlogger (Testlog.class); Public Static voidMain (string[] args) {System.out.println ("This is testLog111222"); Logger.debug ("This is debug message1112222"); Logger.info ("This is info message"); Logger.error ("This is error message"); }}View Code
Log4j reads the configuration file for output, requires only one line of code, gets an instance of the Org.apache.log4j.Logger class Logger, specifies the Logger name, and calls the Logger.debug () method. Logger.getlog () will automatically load the properties file during execution.
The properties file is as follows.
log4j.rootlogger=debug,testloglog4j.appender.testlog=org.apache.log4j.fileappender# log4j.appender.testlog.layout=org.apache.log4j.patternlayoutlog4j.appender.testlog.file= Testlog.loglog4j.appender.testlog.layout=org.apache.log4j.ttcclayout
log4j.properties
Reference article Address http://blog.csdn.net/luoweifu/article/details/46495045
Java Common Log tool--JDK comes with java.util.logging package, APACHE log4j and slf4j log processing interface