Java Web practice topic-log4j

Source: Internet
Author: User

During application development, logs can be used to debug programs and track program running tracks. During the program running process, you can record the running status of the program and use it for auditing. Log4j is a popular implementation that supports multiple log implementations. This section describes how to use log4j logs. The use of log4j logs includes the following processes: l Log retrieval implementation; l writing configuration files; l initialization; l use. The following is an introduction. 1. Log retrieval implementationLog implementation is usually located in the compressed package, the compressed package name log4j-1.2.15.jar, storage location: Under the WEB-INF/lib. You can download the latest version from the official website. 2. ConfigurationTo use log4j, you must first configure the log. The log configuration must use a configuration file. You can use an attribute file or an XML file. The instance described in this article uses an attribute file. Location of the property file: Under the WEB-INF, named log4j. properties. The main contents of the log file: 1) set the basic format of the root recorder with the recorder as follows: log4j. rootlogger = debug, and the r equal sign contains two information: Log Level and log purpose. Log levels include: Off, fatal, error, warn, info, debug, and all logs can be any name or multiple logs. 2) set the log type. The log types include Org. apache. log4j. consoleappender (console), org. apache. log4j. fileappender (file), org. apache. log4j. dailyrollingfileappender (a log file is generated every day), RG. apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size), org. apache. log4j. writerappender (send log information to any specified place in stream format) the following code can be used to set the Log Type: log4j. appender. R = org. apache. log4j. rollingfileappender3) set the relevant attributes of the log file. If the log type is file, you need to point out the location of the file, as shown in the following method: log4j. appender. r. file =$ {Catalina. h Ome}/logs/My. the log file size is determined by the following method: log4j. appender. r. maxfilesize = 10 MB Number of log file duplicates log4j. appender. r. maxbackupindex = 10 4) Common log output methods are as follows: Org. apache. log4j. htmllayoutorg. apache. log4j. patternlayoutorg. apache. log4j. simplelayoutorg. apache. log4j. the code below ttcclayout is used to set the file output mode: log4j. appender. r. layout = org. apache. log4j. patternlayout5) If the output format of the log file is patternlayout, you can use the following format symbol. % R the number of milliseconds consumed since the beginning of the program % t indicates the thread generated by the log record request % P indicates the priority level of the log Statement % R category name related to the log request % C log information the Class % m % N indicates the content of log information, for example: log4j. appender. r. layout. conversionpattern = % P % T % C-% m % n the following is the content of a complete configuration file (# indicates the annotation): # debug indicates the log level, r indicates one of the logs. You can use log4j as the name. rootlogger = debug, R # Log Type log4j. appender. R = org. apache. log4j. rollingfileappender # Log File log4j. appender. r. file =$ {Catalina. home}/logs/My. log # Log File Size log4j. appender. r. maxfilesize = 10 MB # log4j of the log file. appender. r. maxbackupindex = 10 # output mode log4j. appender. r. layout = org. apache. log4j. patternlayout # specific output method log4j. appender. r. layout. conversionpattern = % P % T % C-% m % N 3. InitializationYou can perform initialization in multiple ways. The process is basically the same. The following is an example of servlet initialization. The reference code is as follows: Servlet Source File: Package bookstore. servlet; import javax. servlet. *; import javax. servlet. HTTP. *; import Java. io. *; import Java. util. *; import Org. apache. log4j. propertyconfigurator; public class log4jservlet extends httpservlet {public void Init () throws servletexception {string Path = getservletcontext (). getrealpath ("/"); string filename = path + "/WEB-INF/log4j. properties "; proper Tyconfigurator. Configure (filename) ;}} to load the servlet when the web application starts, configure the <load-on-startup> element in Web. xml. The reference code is as follows: Declaration in Web. xml: <? XML version = "1.0" encoding = "ISO-8859-1"?> <Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4"> <servlet-Name> log4j </servlet-Name> <servlet-class> bookstore. servlet. log4jservlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-Name> log4j </ servlet-Name> <URL-pattern>/log4j </url-pattern> </servlet-mapping> </Web-app> 4. CallAfter the log is configured and initialized, it can be used. The main process is to obtain the logstore and then call the corresponding method. The logger. getlogger (string Str) parameter indicates the log category. The returned value is a logger object. The method of using the logger object to output log information is as follows: Fatal (string info) error (string info) warn (string info) Info (string info) debug (string info: import Org. apache. log4j. logger. The following is an example of using log4j: <% @ page contenttype = "text/html; charset = gb2312" %> <% @ page import = "org. apache. log4j. logger "%> <% logger log = logger. getlogger ("testlogger"); log. debug ("Debug message"); log. error ("error message"); log. warn ("Warn message"); log.info ("info message"); log. fatal ("fatal message"); %> the running result is as follows: (Format: % P % T % C-% m % N ): debug http-8080-1 testlogger-Debug messageerror http-8080-1 testlogger-error messagewarn http-8080-1 testlogger-Warn messageinfo http-8080-1 testlogger-Info messagefatal http-8080-1 testlogger-fatal message 5. expand knowledgeUnder normal circumstances, logs are not used in JSP files, but in the business layer or control layer, and there are many files using logs. Therefore, logger objects are usually created in the base class, then you can directly use the method of each output information in each subclass.

 

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.