From 6b3a30ea184acd633fe5570c382332773e9f54e5 Mon Sep 17 00:00:00 2001 From: zlt Date: Sat, 8 Feb 2020 12:17:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A1=E8=AE=A1=E6=97=A5=E5=BF=97=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8F=92=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 8 ++- .../zlt-log-spring-boot-starter/pom.xml | 5 ++ .../log/properties/AuditLogProperties.java | 5 ++ .../log/properties/LogDbProperties.java | 19 +++++ .../log/service/impl/DbAuditServiceImpl.java | 69 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + zlt-doc/sql/logger-center.sql | 19 +++++ 7 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/LogDbProperties.java create mode 100644 zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/service/impl/DbAuditServiceImpl.java create mode 100644 zlt-doc/sql/logger-center.sql diff --git a/zlt-business/user-center/src/main/resources/application.yml b/zlt-business/user-center/src/main/resources/application.yml index bff4c5b..bfc3237 100644 --- a/zlt-business/user-center/src/main/resources/application.yml +++ b/zlt-business/user-center/src/main/resources/application.yml @@ -37,4 +37,10 @@ zlt: - com.central.user.mapper.SysRoleMapper.findAll #审计日志 # audit-log: -# enabled: true \ No newline at end of file +# enabled: true +# log-type: db +# datasource: +# driver-class-name: com.mysql.cj.jdbc.Driver +# jdbc-url: jdbc:mysql://${zlt.datasource.ip}:3306/logger-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai +# username: ${zlt.datasource.username} +# password: ${zlt.datasource.password} \ No newline at end of file diff --git a/zlt-commons/zlt-log-spring-boot-starter/pom.xml b/zlt-commons/zlt-log-spring-boot-starter/pom.xml index bd48453..8272da1 100644 --- a/zlt-commons/zlt-log-spring-boot-starter/pom.xml +++ b/zlt-commons/zlt-log-spring-boot-starter/pom.xml @@ -52,5 +52,10 @@ javax.servlet-api true + + org.springframework.boot + spring-boot-starter-jdbc + true + diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/AuditLogProperties.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/AuditLogProperties.java index af73dd1..f298928 100644 --- a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/AuditLogProperties.java +++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/AuditLogProperties.java @@ -27,4 +27,9 @@ public class AuditLogProperties { * 日志记录类型(logger/redis/db/es) */ private String logType; + /** + * 数据源配置 + * logType=db时生效(非必须),如果不配置则使用当前数据源 + */ + private LogDbProperties datasource = new LogDbProperties(); } diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/LogDbProperties.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/LogDbProperties.java new file mode 100644 index 0000000..a870d9b --- /dev/null +++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/LogDbProperties.java @@ -0,0 +1,19 @@ +package com.central.log.properties; + +import com.zaxxer.hikari.HikariConfig; +import lombok.Getter; +import lombok.Setter; + +/** + * 日志数据源配置 + * + * @author zlt + * @date 2020/2/8 + *

+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 + */ +@Setter +@Getter +public class LogDbProperties extends HikariConfig { +} diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/service/impl/DbAuditServiceImpl.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/service/impl/DbAuditServiceImpl.java new file mode 100644 index 0000000..0d73529 --- /dev/null +++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/service/impl/DbAuditServiceImpl.java @@ -0,0 +1,69 @@ +package com.central.log.service.impl; + +import com.central.log.model.Audit; +import com.central.log.properties.AuditLogProperties; +import com.central.log.service.IAuditService; +import com.zaxxer.hikari.HikariDataSource; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.scheduling.annotation.Async; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +/** + * 审计日志实现类-数据库 + * + * @author zlt + * @date 2020/2/8 + *

+ * Blog: https://blog.csdn.net/zlt2000 + * Github: https://github.com/zlt2000 + */ +@Slf4j +@ConditionalOnProperty(name = "zlt.audit-log.log-type", havingValue = "db") +@ConditionalOnClass(JdbcTemplate.class) +public class DbAuditServiceImpl implements IAuditService { + private static final String INSERT_SQL = " insert into sys_logger " + + " (application_name, class_name, method_name, user_id, user_name, client_id, operation, timestamp) " + + " values (?,?,?,?,?,?,?,?)"; + + private final JdbcTemplate jdbcTemplate; + + public DbAuditServiceImpl(AuditLogProperties auditLogProperties, DataSource dataSource) { + //优先使用配置的日志数据源,否则使用默认的数据源 + if (StringUtils.isNotEmpty(auditLogProperties.getDatasource().getJdbcUrl())) { + dataSource = new HikariDataSource(auditLogProperties.getDatasource()); + } + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + @PostConstruct + public void init() { + String sql = "CREATE TABLE IF NOT EXISTS `sys_logger` (\n" + + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + + " `application_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '应用名',\n" + + " `class_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '类名',\n" + + " `method_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '方法名',\n" + + " `user_id` int(11) NULL COMMENT '用户id',\n" + + " `user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '用户名',\n" + + " `client_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '租户id',\n" + + " `operation` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '操作信息',\n" + + " `timestamp` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '创建时间',\n" + + " PRIMARY KEY (`id`) USING BTREE\n" + + ") ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;"; + this.jdbcTemplate.execute(sql); + } + + @Async + @Override + public void save(Audit audit) { + this.jdbcTemplate.update(INSERT_SQL + , audit.getApplicationName(), audit.getClassName(), audit.getMethodName() + , audit.getUserId(), audit.getUserName(), audit.getClientId() + , audit.getOperation(), audit.getTimestamp()); + } +} diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories index e828014..9fbeb78 100644 --- a/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -4,4 +4,5 @@ com.central.log.config.TtlMDCAdapterInitializer org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.central.log.config.LogAutoConfigure,\ com.central.log.service.impl.LoggerAuditServiceImpl,\ +com.central.log.service.impl.DbAuditServiceImpl,\ com.central.log.aspect.AuditLogAspect diff --git a/zlt-doc/sql/logger-center.sql b/zlt-doc/sql/logger-center.sql new file mode 100644 index 0000000..7e7fa71 --- /dev/null +++ b/zlt-doc/sql/logger-center.sql @@ -0,0 +1,19 @@ +CREATE DATABASE IF NOT EXISTS `logger-center` DEFAULT CHARACTER SET = utf8; +Use `logger-center`; + +-- ---------------------------- +-- Table structure for t_logger +-- ---------------------------- +DROP TABLE IF EXISTS `sys_logger`; +CREATE TABLE `sys_logger` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `application_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '应用名', + `class_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '类名', + `method_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '方法名', + `user_id` int(11) NULL COMMENT '用户id', + `user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '用户名', + `client_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '租户id', + `operation` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '操作信息', + `timestamp` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '创建时间', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; \ No newline at end of file -- GitLab