第一篇博客:SpringBoot :Logback 的数据库生成配置(JPA反向生成Entity)

Logback自带数据库输出类型。

需要在Logback的配置文件(默认logback-spring.xml)中额外配置如下的appender:

<appender name="dbLogTask" class="ch.qos.logback.classic.db.DBAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>Warn</level>
        </filter>
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
                <driverClass>com.mysql.jdbc.Driver</driverClass>
                <url>jdbc:mysql://127.0.0.1:3306/logback?useSSL=true&characterEncoding=utf-8&useUnicode=true</url>
                <user>root</user>
                <password>root</password>
        </connectionSource>
 </appender>

注意filter的目的是过滤掉INFO以及以下级别的Log信息。

<url>标签中的&符号需要用转义符来替代:&amp;

配置Logback的数据库输出,需要默认在数据库里生成三张记录表,可以导入如下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;

使用JPA配置数据库表反向生成,需要编写如下5个Entity类。注意类名随意,但是TABLE表名不能修改:

package com.ldap.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;

@Getter
@Setter
@Entity
@Table(name = "logging_event")
public class loggingEvent implements Serializable{
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private long event_id;

    @Column
    private long timestmp;

    @Lob
    @Column(columnDefinition="TEXT",nullable = false)
    private String formatted_message;

    @Column(length = 254,nullable = false)
    private String logger_name;

    @Column(length = 254,nullable = false)
    private String level_string;

    @Column(length = 254)
    private String thread_name;

    @Column(columnDefinition="SMALLINT")
    private int reference_flag;

    @Column(length = 254)
    private String arg0;
    @Column(length = 254)
    private String arg1;
    @Column(length = 254)
    private String arg2;
    @Column(length = 254)
    private String arg3;
    @Column(length = 254)
    private String arg4;
    @Column(length = 254)
    private String arg5;

    @Column(length = 254,nullable = false)
    private String caller_filename;
    @Column(length = 254,nullable = false)
    private String caller_class;
    @Column(length = 254,nullable = false)
    private String caller_method;
    @Column(columnDefinition="CHAR(4)",nullable = false)
    private char caller_line;


}
package com.ldap.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name = "logging_event_exception")
public class loggingEventException {
    @EmbeddedId
    private loggingEventExceptionIdentity loggingEventExceptionIdentity;

    @Column(nullable = false,length = 254)
    private String trace_line;

}
package com.ldap.entity;

import lombok.*;

import javax.persistence.*;
import java.io.Serializable;

@Setter
@Getter
@ToString
@EqualsAndHashCode
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class loggingEventExceptionIdentity implements Serializable{

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="event_id",nullable = false,foreignKey = @ForeignKey(name="FOREIGN_EXCEPTION_EVENT",value = ConstraintMode.CONSTRAINT))
    private loggingEvent loggingEvent;

    @Column(columnDefinition="SMALLINT",nullable = false)
    private int i;
}

package com.ldap.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name = "logging_event_property")
public class loggingEventProperty {
    @EmbeddedId
    private loggingEventPropertyIdentity loggingEventPropertyIdentity;

    @Lob
    @Column(columnDefinition="TEXT")
    private String mapped_value;
}

package com.ldap.entity;

import lombok.*;

import javax.persistence.*;
import java.io.Serializable;

@Setter
@Getter
@ToString
@EqualsAndHashCode
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class loggingEventPropertyIdentity implements Serializable{
    @ManyToOne(fetch= FetchType.EAGER)
    @JoinColumn(name="event_id",nullable = false,foreignKey = @ForeignKey(name="FOREIGN_PROPERTY_EVENT",value = ConstraintMode.CONSTRAINT))
    private loggingEvent loggingEvent;

    @Column(length = 254,nullable = false)
    private String  mapped_key;
}



  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:书香水墨 设计师:CSDN官方博客 返回首页
评论

打赏作者

狗蛋丶

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值