Logback DBAppender + Mysql 配置简述

简述

Logback DBAppender

  • Marker 过滤
  • Mysql 数据库,c3p0 数据源
  • 异步记录

配置文件

配置文件示例

    
    <appender name="DB_APPENDER" class="ch.qos.logback.classic.db.DBAppender">
        <filter class="com.sunknew.logging.filter.LogbackMarkerFilter">
            <marker>DBmarker>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>com.mysql.jdbc.DriverdriverClass>
                <jdbcUrl>${DS_URL}jdbcUrl>
                <user>${DS_USERNAME}user>
                <password>${DS_PASSWORD}password>
            dataSource>
        connectionSource>
    appender>

    
    <appender name="ASYNC_APPENDER" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="DB_APPENDER" />
        <includeCallerData>trueincludeCallerData>
    appender>

    
    <root level="${LOG_LEVEL}">
        <appender-ref ref="ASYNC_APPENDER" />
    root>

数据库连接及数据源包

    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.32version>
    dependency>
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>

过滤方法

package angel.stone.web.filter;

import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;

public class LogbackMarkerFilter extends AbstractMatcherFilter<ILoggingEvent> {

    private Marker markerToMatch = null;

    @Override
    public void start() {
        if (null != this.markerToMatch) {
            super.start();
        } else {
            addError(" no marker yet !");
        }
    }

    @Override
    public FilterReply decide(ILoggingEvent event) {
        Marker marker = event.getMarker();
        if (!isStarted()) {
            return FilterReply.NEUTRAL;
        }
        if (null == marker) {
            return onMismatch;
        }
        if (markerToMatch.contains(marker)) {
            return onMatch;
        }
        return onMismatch;
    }

    public void setMarker(String markerStr) {
        if(null != markerStr) {
            markerToMatch = MarkerFactory.getMarker(markerStr);
        }
    }

记录日志

MDC.put("prop01", "prop value 01.");
logger.info(MarkerFactory.getMarker("DB"), "log msg.");

建表语句

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;

保存至自定义表

未尝试,可参考:
1.Store log data into user defined table using Logback db appender
2.Interface DBNameResolver

参考

1.Logback Manual Chapter 4: Appenders
2.Logback Manual Chapter 7: Filters
3.logback-examples
4.SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender
5.Markers in Logback
6.Best practices for using Markers in SLF4J/Logback
7.Slf4j MDC 使用和 基于 Logback 的实现分析

你可能感兴趣的:(----,----,Tool,Logback,----,PL,-,Java,CL,-,Computer,languages)