LOGBACK.XML中配置
<appender name="db_log" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource"> <driverClass>${logback.dblog.dirver}</driverClass> <url>jdbc:mysql://${logback.dblog.serverName}:${logback.dblog.port}/${logback.dblog.databaseName}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC</url> <user>${logback.dblog.user}</user> <password>${logback.dblog.password}</password> </connectionSource> </appender>
<root level="error"> <appender-ref ref="console"/> <appender-ref ref="db_log" /> </root> <logger name="dbLogger" level="error" additivity="false"> <appender-ref ref="db_log"/> </logger>
资源文件配置:
# LOGBACK
logback.dblog.dirver=${logback.dblog.dirver}
logback.dblog.serverName=${logback.dblog.serverName}
logback.dblog.port=${logback.dblog.port}
logback.dblog.databaseName=${logback.dblog.databaseName}
logback.dblog.user=${logback.dblog.user}
logback.dblog.password=${logback.dblog.password}
另外如果要日志写库需要在自己的库里建立三张表:
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;