log4j
Log4j is an open source Apache project, through the use of Log4j, we can control the destination of the log information transmission is the console, file, GUI components, or even socket servers, NT's event recorder, UNIX Syslog daemon, etc.; we can control the output format of each log; by defining the level of each log message, we can more carefully control the generation of the log. Most interesting is that these can be a flexible configuration file to configure the application without the need to modify the code.
directory
Log4j configuration describes the first step Step Step Step Description log4j Log4j Detailed log information defines the configuration file format used in the code Log4jlog4j describes the combination with the emphasis on J2EE Today, the development of reusable components, in addition to its own from start to finish development of a reusable logging operations outside of class, Apache provides us with a powerful logging operations package-Log4j. Official site:
http://logging.apache.org/log4j/ addition, Log4j other language interface, you can C, C + +,
air force 1 high,. Net, PL / SQL program using Log4j, the grammar and usage and in the Java program Like so many languages to get a uniform and consistent distributed system log component module. Moreover, by using a variety of third-party extensions, you can easily be integrated into the Log4j J2EE, JINI and even SNMP applications. Note: The following is divided into three parts, the first explains how to configure log4j; the second part of the log4j.properties configuration file to explain the various properties; the third part is a detailed explanation of log4j. If you want to configure the log4j, then just look at the first two parts can, if you want to log4j-depth understanding of the need to look at Part III. Use license: Apache License V2.0 (
www.apache.org/licenses/LICENSE-2.0) Log4j configuration step to add log4j-1.2.8.jar lib. Established under the second step in the CLASSPATH log4j.properties. As follows: 1 log4j.rootCategory = INFO, stdout, R 2 3 log4j.appender.stdout = org.apache.log4j.ConsoleAppender 4 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout 5 log4j.appender.stdout . layout.ConversionPattern = [QC]% p [% t]% C.% M (% L) |% m% n 6 7 log4j.appender.R = org.apache.log4j.DailyRollingFileAppender 8 log4j.appender.R. File = D: Tomcat 5.5 logs qc.log 9 log4j.appender.R.layout = org.apache.log4j.PatternLayout 10 log4j.appender.R.layout.ConversionPattern =% d-[TS]% p% t% c -% m% n 11 12 log4j.logger.com.neusoft = DEBUG 13 log4j.logger.com.opensymphony.oscache = ERROR 14 log4j.logger.net.sf.navigator = ERROR 15 log4j.logger. org.apache.commons = ERROR 16 log4j.logger.org.apache.struts = WARN 17 log4j.logger.org.displaytag = ERROR 18 log4j.logger.org.springframework = DEBUG 19 log4j.logger.com.ibatis.db = WARN 20 log4j.logger.org.apache.velocity = FATAL 21 22 log4j.logger.com.canoo.webtest = WARN 23 24 log4j.logger.org.hibernate.ps.PreparedStatementCac he = WARN 25 log4j.logger.org.hibernate = DEBUG 26 log4j.logger.org.logicalcobwebs = WARN modify the third step the corresponding attribute to be aware of these before changes are doing, in the second part of the explanation. The fourth step in the output log of the class to add the relevant statement: custom attributes: protected final Log log = LogFactory.getLog (getClass ()); in the corresponding method: if (log.isDebugEnabled ()) {log.debug ( code, which could be named. Level can be divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, OFF if the configuration is not playing any of the information, if configured to INFO This displays only INFO, WARN, ERROR in the log information, and DEBUG information will not be displayed specific reference to the third part can explain the definition of the configuration file logger. 3 log4j.appender.stdout = org.apache.log4j.ConsoleAppender named stdout the sentence for the definition of what type of output, which can be org.apache.log4j.ConsoleAppender (console), org.apache.log4j.FileAppender (file), org.apache.log4j.DailyRollingFileAppender (a log file generated every day), org.apache.log4j.RollingFileAppender (file size reaches the specified size when generating a new file) org.apache.log4j.WriterAppender (the stream format to log information sent to any designated place) can be explained with reference to the third part of the specific definition of the configuration file Appender. 4 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout sentence called for the definition of the output of stdout what type of layout, which can be org.apache.log4j.HTMLLayout (in HTML table form layout), org . apache.log4j.PatternLayout (the flexibility to specify the layout mode),
nike air force 1, org.apache.log4j.SimpleLayout (including the level of log information and the information string), org.apache.log4j.TTCCLayout (including log generation time, thread, category and so forth) can be explained with reference to the third part of the specific definition of the configuration file in the Layout. 5 log4j.appender.stdout.layout.ConversionPattern = [QC]% p [% t]% C.% M (% L) |% m% n If you use a pattern layout of the printed information must specify the specific format ConversionPattern, print parameters are as follows:% m output code output of the specified message% p priority, that is DEBUG, INFO, WARN, ERROR,
air force one high, FATAL% r output from the application starts to output the log information on the number of milliseconds spent the output belongs to the category% c ,
air force one low, where the class is usually the full name of% t out of the log events generated thread name% n output a carriage return line feed, Windows platform, The default format is ISO8601, you can specify the format in the following, for example:% d {yyyy MMM dd HH: mm: ss, SSS}, output similar to: October 18, 2002 22:10:28,921% l output location of log events, including the category name, place of thread, and the number of lines in the code. [QC] is the log information at the beginning, can be any character, generally referred to for the project. Output information [TS] DEBUG [main] AbstractBeanFactory.getBean (189) | Returning cached instance of singleton bean 'MyAutoProxy' can refer to the third part explains the specific definition of the configuration file formatting log messages. 7 log4j.appender.R = org.apache.log4j.DailyRollingFileAppender the same sentence with the first line 3. Called R, the definition of the type of output to generate a log file daily. 8 log4j.appender.R.File = D: Tomcat 5.5 logs qc.log sentence called for the definition of R, the output file name D: Tomcat 5.5 logs qc.log can modify. 9 log4j.appender.R.layout = org.apache.log4j.PatternLayout with the same line 4. 10 log4j.appender.R.layout.ConversionPattern =% d-[TS]% p% t% c -% m% n with the same line 5. 12 log4j.logger.com. Neusoft = DEBUG specify all the classes under the package com.neusoft level as DEBUG. Com.neusoft to their project can be used by the package name. 13 log4j.logger.com.opensymphony.oscache = ERROR 14 log4j.logger.net.sf.navigator = ERROR these two is under these two packages set the level of error ERROR, if the project is not configured EHCache, you do not need these two. 15 log4j.logger.org.apache.commons = ERROR 16 log4j.logger.org.apache.struts = WARN these two is the struts of the package. 17 log4j.logger.org.displaytag = ERROR phrase is displaytag package. (QC issues list, used) 18 log4j.logger.org.springframework = DEBUG sentence for the Spring of the package. 24 log4j.logger.org.hibernate.ps.PreparedStatementCac he = WARN 25 log4j.logger.org.hibernate = DEBUG the two packages is hibernate. The settings for these packages the actual situation of the project on their own customization. Detailed definition of the configuration file log4j Log4j supports two configuration file formats, one is the XML format, one is the Java properties file log4j.properties (key = value). The following describes the log4j.properties file as a configuration file using the method: ①, configure the root Logger Logger responsible for the majority of logging operations. The syntax is: log4j.rootLogger = [level], appenderName, appenderName, ... which, level is the priority of the log records, divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or a custom level. Log4j recommended to use only four grades, from highest to lowest priority are ERROR, WARN, INFO, DEBUG. Defined by the level here, you can control the application of the appropriate level of log messages switch. For example, in the definition of the INFO level here, and only equal to and above this level before processing, the application of all the DEBUG level log messages will not be printed. ALL: print all of the logs, OFF: Turn off all the log output. appenderName is to specify the output log information to which place. Can also specify multiple output destinations. ②, configure the log information output destination Appender Appender is responsible for controlling the output of the logging operation. The syntax is: log4j.appender.appenderName = fully.qualified.name.of.appender.class log4j.appender.appenderName.option1 = value1 ... log4j.appender.appenderName.optionN = valueN appenderName here in the definition in ①, can be arbitrarily named. Which, Log4j appender provided are the following: org.apache.log4j.ConsoleAppender (console), org.apache.log4j.FileAppender (file), org.apache.log4j.DailyRollingFileAppender (a log file generated every day), org.apache.log4j.RollingFileAppender (file size reaches the specified size when generating a new file), set by log4j.appender.R.MaxFileSize = 100KB file size, can also be set by log4j.appender.R.MaxBackupIndex = 1 To save a backup file. org.apache.log4j.WriterAppender (stream format to log information sent to any designated place) For example: log4j.appender.stdout = org.apache.log4j.ConsoleAppender stdout output to define a named destination, ConsoleAppender for the console . ③, configure the log message format (layout) Layout Layout Appender is responsible for formatting the output. The syntax is: log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class log4j.appender.appenderName.layout.option1 = value1 ... log4j.appender.appenderName.layout.optionN = valueN which, Log4j provides the layout are the following: org.apache.log4j.HTMLLayout (in HTML table form layout), org.apache.log4j.PatternLayout (the flexibility to specify the layout mode), org.apache.log4j.SimpleLayout (contains the log information level and the information string), org.apache.log4j.TTCCLayout (including log generation time, thread, category and so forth) use a similar format Log4J log information in the C language printf function to format the log message print format, print parameters are as follows:% m output code output of the specified message% p priority, that is DEBUG, INFO, WARN, ERROR, FATAL% r output from the application starts to output the log information on the number of milliseconds spent the output belongs to the category% c , where the class is usually the full name of% t out of the log events generated thread name% n output a carriage return line feed, Windows platform, The default format is ISO8601, you can specify the format in the following, for example:% d {yyyy MMM dd HH: mm: ss, SSS}, output similar to: October 18, 2002 22:10:28,921% l output location of log events, including the category name, place of thread, and the number of lines in the code. Using Log4j in the code we need to output log information to do the following three categories of work: 1, import all the required commongs-logging class: import org.apache.commons.logging.Log; import org.apache.commons. logging.LogFactory; 2, in their own class to define a private static class org.apache.commons.logging.Log class members: private final Log log = LogFactory.getLog (getClass ()); LogFactory.getLog () method parameters using the current class of class. 3, members of the class using the method of output org.apache.commons.logging.Log log message: if (log.isDebugEnabled ()) {log.debug (. info (if (log.isFatalEnabled ()) {log.fatal (1) defined in the configuration file the appropriate recorder. Defined in the configuration file format, there are two recorder? Defines the format of the root logger for the log4j.rootLogger = [level], appendName1, appendName2, ... appendNameN? A non-root logger defines the format for the log4j.logger.loggerName1 = [level], appendName1, ... appendNameN ... ... log4j.logger.loggerNameM = [level], appendName1, ... appendNameN we can define as non-root logger. 2) in the code made the class called Logger logger method to get the corresponding record object. To obtain the root logger object can Logger.getRootLogger () function, to obtain non-root logger can Logger.getLogger () function. Theory that will stop here, a piece of paper come Zhongjue shallow, the following, we come to a small drill bit. Example 4-a: package TestLog4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.Priority; import TestLog4j.TestLog4j2.TestLog4j2; public class TestLog4j {static Logger logger = Logger.getLogger (TestLog4j.class.getName ()); / / (2) public TestLog4j () {} public static void main (String [] args) {/ / output to the console and also an instance of a file and realized Logger inheritance PropertyConfigurator.configure (with priority set to INFO testLog4j2 = new TestLog4j2 (); / / (1) testLog4j2.testLog ();}} in the class TestLog4j another class, we call TestLog4j2, the following categories TestLog4j2 look at the code. Example 4-b: package TestLog4j.TestLog4j2; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.Priority; public class TestLog4j2 {static Logger logger = Logger.getLogger (TestLog4j2 . class.getName ()); / / (1) public TestLog4j2 () {} public void testLog () {/ / while the output to the console and a file instance PropertyConfigurator.configure (log4j2.properties log message with priority set to WARN log (Priority.DEBUG, Example 4-c: log4j2.properties contents of the file # 1 District # # # # Use two appenders, one to log to console, another to log to a file log4j.rootLogger = debug, stdout # 2 District # Print only messages of priority WARN or higher for your category log4j.logger.TestLog4j =, R log4j.logger.TestLog4j.TestLog4j2 = WARN # 3 District # # # # First appender writes to console log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender . stdout.layout = org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern =% 5p [% t] (% F:% L) -% m % n # 4 District # # # # Second appender writes to a file log4j.appender.R = org.apache.log4j.RollingFileAppender log4j.appender.R.File = F: nepalon classes TestLog4j example . log # Control the maximum log file size log4j.appender.R.MaxFileSize = 100KB # Archive log files (one backup file here) log4j.appender.R.MaxBackupIndex = 1 log4j.appender.R.layout = org.apache.log4j . PatternLayout log4j.appender.R.layout.ConversionPattern =% d {yyyy-MM-dd hh: mm: ss}:% p% t% c -% m% n look at operating results. The results in the console: DEBUG [main] (?:?) - Start of the main () in TestLog4j INFO [main] (?:?) - Just testing a log message with priority set to INFO WARN [main] ( ?:?) - Just testing a log message with priority set to WARN ERROR [main] (?:?) - Just testing a log message with priority set to ERROR FATAL [main] (?:?) - Just testing a log message with priority set to FATAL WARN [main] (?:?) - Testing a log message use a alternate form DEBUG [main] (?:?) - TestLog4j.TestLog4j WARN [main] (?:?) - 2Just testing a log message with priority set to WARN ERROR [main] (?:?) - 2Just testing a log message with priority set to ERROR FATAL [main] (?:?) - 2Just testing a log message with priority set to FATAL results output file To: 2003-12-19 04:19:44: DEBUG main TestLog4j.TestLog4j - Start of the main () in TestLog4j 2003-12-19 04:19:44: INFO main TestLog4j.TestLog4j - Just testing a log message with priority set to INFO 2003-12-19 04:19:44: WARN main TestLog4j.TestLog4j - Just testing a log message with priority set to WARN 2003-12-19 04:19:44: ERROR main TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR 2003-12-19 04:19:44: FATAL main TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL 2003-12-19 04:19:44: WARN main TestLog4j. TestLog4j - Testing a log message use a alternate form 2003-12-19 04:19:44: DEBUG main TestLog4j.TestLog4j - TestLog4j.TestLog4j 2003-12-19 04:19:44: WARN main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to WARN 2003-12-19 04:19:44: ERROR main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to ERROR 2003-12-19 04:19:44: FATAL main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to FATAL First of all, the first look at what are some of the configuration file. 1) In an area define a root logger. The root logger with DEBUG level and have a name for the output of stdout appender. 2) 2 area of the content is the focus of this section also apply to local-level recorder, but in fact only two, fully reflects the simplicity of log4j. Here, we define the two names are TestLog4j and TestLog4j.TestLog4j2 designer. ? TestLog4j recorder in the definition did not specify the level, the level it inherited from its parent logger, that is, the root logger, so it's level for DEBUG. Shiyou TestLog4j recorder in the definition defines a name for the output of R, so it has two outputs, one inherited from the root logger named stdout the output, and the other in the name of this definition for the R output. In this note,
nike air force one low, in the definition of the recorder must first define the level of the recorder, and then the recorder output. If you want to define the definition of output rather than levels, although the level can be empty, but can not omit the comma separator. If the definition of the practice TestLog4j recorder. ? In the definition of TestLog4j.TestLog4j2 recorder Shiyou its specified level,
nike air force 1 high, due to a record level of only one device, the new specified level will override its parent logger's level (which is like Java in the polymorphism). We do not define TestLog4j.TestLog4j2 recorder output, so it's output from its inherited from the parent logger. Its parent logger to estLog4j recorder, so it estLog4j recorder has two names are the same and the R output of stdout. 3) the rest of Region 3 and 4 the two areas were set output parameters. Next, back to our code, look at how to get the recorder, the recorder Shiyou take what happened. 1) Example 4-a code (2), the statement Logger.getLogger () the parameters TestLog4j.class.getName () the value of TestLog4j. TestLog4j, so this result is to obtain a statement called TestLog4j. TestLog4j The object records. However, in the configuration file does not define such a recorder, it will eventually return with the required name of the TestLog4j. TestLog4j object closest to the record, that is called TestLog4j the recorder object. 2) patients with 4-b of the code (1) Principles and case 4-a code (2) similar to expect to achieve is called TestLog4j.TestLog4j2. TestLog4j2 record object, but the final returns TestLog4j. TestLog4j2 recorder object. So far the combination of log4j and J2EE, this article talked about how the application is the application of log4j, the Java application is now the mainstream J2EE and J2ME. Now, we look at how the development of applications in J2EE log4j. In fact, the application of Web application is very simple log4j, and log4j in the application the application is different from all the applications in before the code log4j log4j initialized. Therefore, we have to put log4j in web application initialization independent, put it in the Servlet. Next, we look at an example. Example 5-a: to initialize the Servlet: import org.apache.log4j.PropertyConfigurator; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; / * * * log4j.jar initialization class, reference web.xml * / public class Log4jInit extends HttpServlet {public void init () {/ / dynamically get through the web.xml configuration file String prefix = getServletContext (). getRealPath (;/prefix + file); / / (1)}} public void doGet (HttpServletRequest req, HttpServletResponse res) {}} The following look at the Servlet in the web.xml definition. Example 5-b: log4j-init TestLog4j.Log4jInit
; log4j-init-file
sort.properties 1 ; for log4j initialization to complete all of the log4j calls, so in web.xml file, we must take the corresponding Servlet-defined load-on-startup should be set to 1, in order to Web container is loaded at startup that the Servlet. This is done after these two steps, we can, as in application development as any place in the web application application log4j the. Here are javabean an example of the application. Example 5-c: import org.apache.log4j.Logger; public class InfoForm {static Logger logger = Logger.getLogger (InfoForm.class); protected String title; protected String content; public InfoForm () {} public void setTitle (Object value) {logger.debug (public void setContent (String value) {content = value; logger.debug (+ content); return content;}} entry for more Atlas Atlas Open Category: computer technology, programming, procedures, components I have to perfect