Use log4j to write logs to MySQL database

Source: Internet
Author: User

Log4j can support log output to files, databases, and even remote servers, this tutorial takes the MySQL database as an example to explain:

1. Database design

Database tables

Table 4-1 Journal table (log)

Field name

Field interpretation

Data type

Data

Length

is empty

Whether the primary key

is a foreign key

Id

Log number

bigint

20

Whether

Is

Whether

Create_time

Creation time

Datetime

Whether

Whether

Whether

Logs

Log content

varchar

200

Whether

Whether

Whether

Database creation:

Create database log;

To switch databases:

Use log;

To create a table:

CREATE TABLE`Log' (' ID 'bigint( -) unsigned not NULLauto_increment, ' Create_time 'datetime  not NULL, ' logs 'varchar( $) not NULL, Primary Key(' id ')) ENGINE=InnoDB auto_increment=2 DEFAULTCHARSET=UTF8;
2. Project Preparation

Create a new Java project log4j_write_mysql:

File->new->java Project:

Create a new test class:

Create a new Lib file and import the LOG4J package:

Configuring the log4j configuration file

Log4j.rootlogger=Error,appender1,appender2log4j.appender.appender1=Org.apache.log4j.ConsoleAppenderlog4j.appender.appender1.layout=Org.apache.log4j.PatternLayoutlog4j.appender.appender1.layout.ConversionPattern=%D{YYYY-MM-DD hh:mm:ss:sss}[%p]:%m%Nlog4j.appender.appender2=Org.apache.log4j.jdbc.JDBCAppenderlog4j.appender.appender2.driver=Com.mysql.jdbc.Driverlog4j.appender.appender2.URL=jdbc:mysql://Localhost:3306/log?useunicode=true&characterencoding=utf-8Log4j.appender.appender2.user=Rootlog4j.appender.appender2.password=137151Log4j.appender.appender2.sql=insert into Zuidaima_log (create_time,log) VALUES ('%d{yyyy-mm-dd hh:mm:ss} ', '%c%p%m%n ') Log4j.appender.appender2.layout=org.apache.log4j.patternlayout

Test Class Code:

/** *  */ PackageLog4j_write_mysql;ImportOrg.apache.log4j.Logger;/*** Project Name: Log4j_write_mysql * Class Name: Test * Class Description: * Creator: Deng Jiahai * Created: May 28, 2015 1:03:01 * Modified by: Deng * Modified time : May 28, 2015 1:03:01 * Edit NOTES: *@version    *    */ Public classTest { Public StaticLogger log = Logger.getlogger (Test.class);  Public Static voidMain (string[] args) {Try {                Throw NewException ("Most Code error log"); } Catch(Exception e) {log.error (E.getmessage ()); }        }    }
3: Run Result:

Console information:

Database information:

Improved, integrated console, log file, database three output:

Configuration file Code:

Log4j.rootlogger=Error,appender1,appender2,log2log4j.appender.appender1=Org.apache.log4j.ConsoleAppenderlog4j.appender.appender1.Target=System.errlog4j.appender.appender1.layout=Org.apache.log4j.PatternLayoutlog4j.appender.appender1.layout.ConversionPattern=%D{YYYY-MM-DD hh:mm:ss:sss}[%p]:%m%Nlog4j.appender.appender2=Org.apache.log4j.jdbc.JDBCAppenderlog4j.appender.appender2.driver=Com.mysql.jdbc.Driverlog4j.appender.appender2.URL=jdbc:mysql://Localhost:3306/log?useunicode=true&characterencoding=utf-8Log4j.appender.appender2.user=Rootlog4j.appender.appender2.password=137151Log4j.appender.appender2.sql=insert into log (create_time,logs) VALUES ('%d{yyyy-mm-dd hh:mm:ss} ', '%c%p%m%n ') Log4j.appender.appender2.layout=org.apache.log4j.PatternLayoutlog4j.appender.log2=Org.apache.log4j.FileAppenderlog4j.appender.log2.file=Log.loglog4j.appender.log2.layout=Org.apache.log4j.PatternLayoutlog4j.appender.log2.layout.ConversionPattern=%D{YYYY-MM-DD hh:mm:ss:sss}[%p]:%m%n

If you need source code, please post the request:

Http://tieba.baidu.com/f?kw=%C8%ED%BC%FE%C6%B5%B5%C0&fr=home

Use log4j to write logs to MySQL database

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.