[Logback + slf4j] correct configuration and error handling of Mysql DBAppender, logbackslf4j
Prerequisites: 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 (both recipes are acceptable)
The second requirement is the database table creation script.
Coder, including many friends on stackoverflow, is officially
Http://logback.qos.ch/manual/appenders.html
Mentioned ingetGeneratedKeys
If this is misleading, we all think that as long as the table is supported, we don't have to create a table automatically, but just add the sqlDialect dialect. No. You must create a table manually, whether supported or not. (Of course, you can write a java bean automatically run script to create it ).
I have read many online articles and often see the sqlDialect configuration node.
I started the test from logback 0.9.8. For Mysql, you do not need to configure this node or oracle.
In 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 is 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 logging_event;COMMIT;BEGIN;CREATE TABLE logging_event ( timestmp BIGINT NOT NULL, formatted_message TEXT NOT NULL, logger_name VARCHAR(254) NOT NULL, level_string VARCHAR(254) NOT NULL, thread_name VARCHAR(254), reference_flag 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) NOT NULL, caller_line CHAR(4) 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) NOT NULL, mapped_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, trace_line VARCHAR(254) NOT NULL, PRIMARY KEY(event_id, i), FOREIGN KEY (event_id) REFERENCES logging_event(event_id) );COMMIT;
Third necessary condition: Correct configuration.
Here is a logback configuration managed using 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.