Application Example of log4j

Source: Internet
Author: User
I have not written examples in the log4j usage notes. In this article, I will write a complete application instance of log4j.
In the log4j usage notes, we already know that the same log information can be output to multiple output destinations at the same time. In the following example, I will demonstrate how to output log information to the console, files, and databases at the same time. Create a table as follows: Log4j. SQL
Create   Table Log4j
(
Logid Int   Not   Null Auto_increment, -- Serial number
Createdate Varchar ( 45 ) Default   Null , -- Log generation time
Thread Varchar ( 45 ) Default   Null , -- Current thread
Level   Varchar ( 45 ) Default   Null , -- Current Log Level
Class Varchar ( 45 ) Default   Null , -- Log generation class
Message Varchar ( 245 ) Default   Null , -- Log details

Primary   Key (Logid)
)

Compile the configuration file mylog4j. Properties

# Define three output terminals
Log4j. rootcategory = info, A1, A2, A3

# Define A1 output to Controller
Log4j. appender. A1 = org. Apache. log4j. leleappender
# Define the layout mode of A1 as paternlayout
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
# Define A1 output format
Log4j. appender. a1.layout. conversionpattern = % 4 P [% T] (% F: % L)-% m % N

# Define A2 output to file
Log4j. appender. A2 = org. Apache. log4j. rollingfileappender
# Define the file to which A2 is output
Log4j. appender. a2.file =./sample. Log
# Define the maximum length of the A2 output file
Log4j. appender. a2.maxfilesize = 1kb
# Define the number of backup files for A2
Log4j. appender. a2.maxbackupindex = 3
# Define the layout mode of A2 as patternlayout
Log4j. appender. a2.layout = org. Apache. log4j. patternlayout
# Define A2 output mode
Log4j. appender. a2.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS }:% P % T % C-% m % N

# Define A3 output to database
Log4j. appender. A3 = org. Apache. log4j. JDBC. jdbcappender
Log4j. appender. a3.url = JDBC: mysql: // localhost: 3306/Study
Log4j. appender. a3.driver = com. MySQL. JDBC. Driver
Log4j. appender. a3.user = root
Log4j. appender. a3.password = root
# Define the layout of A3 and the executed SQL statement
Log4j. appender. a3.layout = org. Apache. log4j. patternlayout
Log4j. appender. a3.layout. conversionpattern = insert into log4j (createdate, thread, level, class, message) values (\ '% d \', \ '% t \', \ '%-5 p \', \ '% C \', \ '% m \')

Final TestProgramAs follows:

Public   Static   Void Sample ()
{
Logger = Logger. getlogger (log4jtest. Class );

Propertyconfigurator. Configure ( " Mylog4j. Properties " );
Logger. debug ( " Here is debug messgae " );
Logger.info ( " Here is info message " );
Logger. Warn ( " Here is warn message " );
Logger. Error ( " Here is error message " );
Logger. Fatal ( " Here is fatal message " );
}

In this way, when we run the program, the above log information will be transferred to three places at the same time:
Console:

Database:

File:

We have seen the sample. log file, which we specified in the configuration. Of course you can also modify other places, and the log4j configuration file is also placed in the project root directory.
At the same time, when we run this program several times, the file will be automatically renamed when the sample. Log exceeds 1 kb, and the log will be recorded in the new sample. log file., As follows:

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.