Use log4j to log information in the Java Web __HTML5

Source: Internet
Author: User
Tags html form java web log4j
Recently want to record the use of log4j, took a little time through the collection of information on the Internet to organize the blog, I hope to need to use log4j log friends a little help, if there is a wrong place to correct. Log4j is an open source project for Apache, by using log4j, we can control the destination of log information delivery is console, file GUI, even interface server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the information level of each log, we can control the log generation process more carefully. (Excerpt from Baidu Encyclopedia) Apache's log4j mainly includes three parts. In the application to use log4j to log, first know the role of these three parts, only know how to configure log4j to log. These three parts are: The priority of log information, the input destination of log information and the output format of log information.

Here is an introduction to these three parts, and then at the end give some examples to explain how to configure log4j. First, log level log level, is to tell log4j what level of log information is recorded, mainly including the following levels (level from low to high): all: At all levels, including custom levels; debug: Generally used on fine-grained, very helpful for debugging applications; INFO: Generally used in fine-grained, emphasizing the running of the application; WARN: Specifies a potentially hazardous situation; Error: Specifies that the error event occurs, but does not affect the system running; FATAL: Specifies a very serious error event that may cause the application to terminate; off: The highest level,

Specifies that all features of logging are turned off. Second, log information output destination log information output destination, specify log4j log information output to where. In the daily development, the more commonly used are the following: 1, Org.apache.log4j.ConsoleAppender (console): Configurable properties: Threshold: Specify the lowest level of log message output Innediateflush:
Log messages are immediately exported, default to True Target: Specify who outputs, default is System.out 2, Org.apache.log4j.FileAppender (file): Configurable properties: Threshold: Specify the lowest level of log message output Encoding: You can specify any character encoding, which defaults to the encoding scheme for a particular platform Innediateflush: whether the log message is exported immediately, and the default is true file: Specify log output to that file Append: Specify whether the log information is appended to the file. The default is true 3, org. Apache.log4j.RollingFileAppender (Specifies that a new file is generated after the log file reaches a certain size): This configuration inherits from the Fileappender and includes the following properties in addition to Fileappender configurable properties: MaxFileSize: Specifies that when the log file reaches a certain size, a new file is generated, and the unit can be specified with KB/MB/GB Maxbackupindex: Specifies how many new files can be generated 4,
Org.appche.log4j.DailyRollingFileAppender (a log file is generated every day): This configuration inherits from Fileappender and includes the following properties, in addition to Fileappender configurable properties: Datepattern: Specifies when a new log file is generated, the default is 0 points, the format can be specified: '. ' YYYY-MM: The end of each month and the beginning of next month '. ' YYYY-MM-DD: Default value, 0 dots per day '. ' Yyyy-mm-dd-a: 0 o ' Day and 12 o '. YYYY-MM-DD-HH: Every hour '. ' YYYY-MM-DD-HH-MM: Every minute '. '
YYYY-WW: The first day of the Week 5, Org.apache.log4j.jdbc.JDBCAppender (log information to the database): Configurable properties: buffersize: Set the size of the buffer, default to 1 driver: Set the database driver
Layout: Sets the layout used by the log, by default Org.apache.log4j.PatternLayout sql: Specifies that the statement executes at the time each event occurs, and can be a Insert/update/delete URL: set the JDBC URL User: Set database username password: Database password Three, log information output format can be configured with the following output format: 1, org.apache.log4j.HTMLLayout (in HTML form layout) 2, Org.apache.log4j.PatternLayout (can flexibly specify layout format) 3, Org.apache.log4j.SimpleLayout (contains log level and information string) 4, Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log generation through the above configurable layout, You also need to specify the Org.appender.appendName.layout.ConversionPattern property's specific grid, here is an example: log4j.appender.r.layout.conversionpattern=%-4r%-5p%d{yyyy-mm-dd HH:mm:ss}%c%m%n specific configuration items are described as follows:- X: Log information input left-aligned%p: output log level%d: output format for the specified date%r: Output from application startup to the number of milliseconds to output this log information is the class of the output log information, typically the class full name%t: Output the thread name that generated the log time%l: Where the output log event occurred

, including the class name, the thread that occurred, and the number of rows in code%x: Threads relative of nested diagnostics associated with the current line: output a "%"%F: The output log message is generated by the file name%l: The line number in the output code%m: The log information specified in the output code%n: output a carriage return line feed After the introduction of the log4j three parts of the common configuration, the following combination of examples to show the real environment in the configuration example: 1, download log4j, download the jar package to the Lib directory download address: http://logging.apache.org/log4j/1.2/
 Download.html 2, write the configuration file and add the file to the classpath:


3. Start the log4j when you configure the boot server in Web.xml

4. Load log4j configuration information in code:

Through the above 4 steps, then the LOG4J environment even if completed. Because it is based on the Java Web build log4j, you need to start the server to test. Here I take the example of executing a SQL, the following figure is the use of log4j log logs in a section of code:

Console input LOG:

Log file output:

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.