Write Log4J to database

Source: Internet
Author: User
In some daily projects, log logs need to be output to the database for analysis. This is easy for log4J. Next we will configure to output log to the database. Before starting the project, add the mysql driver jar package to the current project. Then, create the test database in the database used, create a log table, and create a table.

In some daily projects, log logs need to be output to the database for analysis. This is easy for log4J. Next we will configure to output log to the database. Before starting the project, add the mysql driver jar package to the current project. Then, create the test database in the database used, create a log table, and create a table.

In some daily projects, log logs need to be output to the database for analysis. This is easy for log4J. Next we will configure to output log to the database.

Before starting the project, add the mysql driver jar package to the current project. Then, create the test database in the database used and create a log table. The table creation statement is as follows:

Create table 'log4j '(
'Logid' int (11) not null AUTO_INCREMENT,
'Createdate' varchar (45) default null,
'Thread' varchar (45) default null,
'Level' varchar (45) default null,
'Class' varchar (150) default null,
'Message' varchar (245) default null,
Primary key ('logid ')
) ENGINE = InnoDB AUTO_INCREMENT = 394 default charset = utf8


The log4j. properties information is as follows:

# Log4j. rootCategory = INFO, A3
Log4j. rootCategory = INFO, A3
Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
Log4j. appender. stdout. layout. ConversionPattern = % d {yyyy MMM dd HH: mm: ss, SSS} % m % n

Log4j. logger. java. SQL. Connection = debug
Log4j. logger. java. SQL. Statement = debug
Log4j. logger. java. SQL. PreparedStatement = debug, stdout, logfile, A3


# Define A3 output to database
Log4j. appender. A3 = org. apache. log4j. jdbc. JDBCAppender
Log4j. appender. A3.URL = jdbc: mysql: // 18.8.10.104: 3306/test
Log4j. appender. A3.driver = com. mysql. jdbc. Driver
Log4j. appender. A3.user = root
Log4j. appender. A3.password = bistest1
# 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 \')


The test code is as follows:

Public class Log4JTest {
Logger logger = Logger. getLogger (Log4JTest. class );
Public Log4JTest (){
Logger. warn ("yes I do ");
}
Public static void main (String [] args ){
Logger logger = Logger. getLogger (Log4JTest. class );
System. out. println (System. getProperty ("user. dir "));
PropertyConfigurator. configure ("src/log4j. 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 ");
}
}

When you run the test code, you will find that the log information is written to the database.

Note: The database used in this example is mysql 5.1. If other types of databases are used, modify the driver information and other related information according to the specific database.

This example is no longer detailed. For more information about the parameters, see my other article: http://blog.csdn.net/liyong635/article/details/13988209

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.