First requisite: Jar package
Required Packages:
Logback-core-0.9.8.jar
Logback-classic-0.9.8.jar
Slf4j-api-1.6.8.jar
When writing this article, the latest version of Logback is 1.1.2. The corresponding Slf4j-api is 1.7.6 (two recipes are available)
Second necessary condition: Database table creation script.
Coder, including many friends on the StackOverflow, have been officially
Http://logback.qos.ch/manual/appenders.html
The getGeneratedKeys misleading, all thought, as long as the table is supported, you do not have to set up the table automatically, and only need to add Sqldialect dialect. No, it's not, you create the table manually, whether it's supported or not. (You can, of course, write a Java Bean autorun script to create it yourself).
Also read a lot of online articles, often appear sqldialect this configuration node.
I started testing from Logback 0.9.8, and for MySQL, there is no need to configure this node, nor does Oracle need it.
This step, you only need to run the following script in MySQL:
# logback:the reliable, generic, fast and flexible logging framework.# Copyright (C) 1999-2010, qos.ch. All rights reserved.## See http://logback.qos.ch/license.html for the applicable licensing # conditions.# this SQL script Creates the required tables by ch.qos.logback.classic.db.dbappender.## It's intended for MySQL databases. It has been tested on MySQL 5.1.37 # on Linuxuse tumorpredict; Begin;drop table if EXISTS logging_event_property;drop table if EXISTS logging_event_exception;drop table if EXISTS loggin G_event; COMMIT; BEGIN; CREATE TABLE logging_event (timestmp BIGINT not NULL, formatted_message TEXT is not NULL, logger_name varchar (254) NOT NULL, level_string varchar (254) is not NULL, Thread_name varchar (254), Reference_fla G SMALLINT, arg0 varchar (254), arg1 varchar (254), arg2 varchar (254), Arg3 varchar (254), Caller_filename varchar (254) not NULL, Caller_class varchar (254) NOT NULL, Caller_method VARCHAR (254) is not NULL, Caller_line CHAR (4) is not NULL, event_id BIGINT not NULL auto_increment PRIMARY KEY); COMMIT; BEGIN; CREATE TABLE Logging_event_property (event_id BIGINT not NULL, Mapped_key VARCHAR (254) is not NULL, MA Pped_value TEXT, PRIMARY key (event_id, Mapped_key), FOREIGN key (event_id) REFERENCES logging_event (event_id) ); COMMIT; BEGIN; CREATE TABLE logging_event_exception (event_id BIGINT not NULL, I SMALLINT not null, tra Ce_line VARCHAR (254) not NULL, PRIMARY key (event_id, i), FOREIGN key (event_id) REFERENCES logging_event (event _id)); COMMIT;
The third necessary condition: correct configuration.
Here is a logback configuration that is managed with the C3P0 connection pool:
<appender name= "Db-classic-mysql" class= "Ch.qos.logback.classic.db.DBAppender" > <connectionsource class= "Ch.qos.logback.core.db.DataSourceConnectionSource" > <datasource class= " Com.mchange.v2.c3p0.ComboPooledDataSource "> <driverclass>com.mysql.jdbc.driver</driverclass > <jdbcurl>jdbc:mysql://{$server ip}:3306/{$dbname}</jdbcurl> <user>{$user}</ user> <password>{$password}</password> </dataSource> </connectionsource > </appender>
Indispensable.
[LOGBACK+SLF4J] Mysql dbappender Correct configuration method and error handling