mySql---logback日誌寫入數據庫(mysql)配置

如題  建議將日誌級別設置爲ERROR。這樣能夠避免存儲過多的數據到數據中。html

1  logback 配置文件(以下)mysql

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <!-- 將日誌寫入日誌文件 -->
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${catalina.base}/logs/cnsurvey-order-ws.log</file>
        <append>true</append><!-- 日誌追加 -->
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <!-- 將日誌寫入數據庫 -->
    <appender name="DB-CLASSIC-MYSQL-POOL" class="ch.qos.logback.classic.db.DBAppender">  
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">  
            <dataSource class="org.apache.commons.dbcp.BasicDataSource">  
                <driverClassName>com.mysql.jdbc.Driver</driverClassName>  
                <url>jdbc:mysql://localhost:3306/cbs_ifs_order?characterEncoding=UTF-8</url>  
                <username>root</username>  
                <password>root</password>  
            </dataSource>  
        </connectionSource>  
    </appender> 
    <root level="ERROR">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
        <appender-ref ref="DB-CLASSIC-MYSQL-POOL" />  
    </root>
</configuration> 

 

 若是想讓日誌寫入到數據庫中,必須手動添加額外的數據庫表(mysql 測試)。若是mysql 中沒有指定表,則不會寫數據。sql

 表結構以下數據庫

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;

 將日誌設置爲ERROR 級別。會自動將ERROR 界別的錯誤信息 ,插入到數據庫中apache

 

 

 

 

參考:
http://logback.qos.ch/manual/appenders.htmlapp

http://blog.csdn.net/kimsoft/article/details/16330869測試

http://blog.csdn.net/jiaincs/article/details/5686287#url

相關文章
相關標籤/搜索