Move table t_sys_log to sqlite
This commit is contained in:
@@ -34,6 +34,26 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!File("data/db").isDirectory) {
|
||||
if (!File("data/db").mkdir()) {
|
||||
logger.error("Can not create directory 'data/db', please try again later.")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (!File("data/db/sqlite.db").isFile || File("data/db/sqlite.db").inputStream()
|
||||
.use { it.readNBytes(15).toString(Charsets.UTF_8) != "SQLite format 3" }
|
||||
) {
|
||||
logger.warn("The 'data/db/sqlite.db' database is lost or damaged, recreating...")
|
||||
if (File("data/db/sqlite.db").exists() && !File("data/db/sqlite.db").delete()) {
|
||||
logger.error("Can not recreate database 'data/db/sqlite.db', please try again later.")
|
||||
}
|
||||
|
||||
FatWebApiApplication::class.java.getResourceAsStream("/db/sqlite.db")?.let {
|
||||
File("data/db/sqlite.db").writeBytes(it.readAllBytes())
|
||||
}
|
||||
}
|
||||
|
||||
if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) {
|
||||
runApplication<FatWebApiApplication>(*args)
|
||||
} else {
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import top.fatweb.api.converter.system.SysLogConverter
|
||||
import top.fatweb.api.entity.common.ResponseCode
|
||||
import top.fatweb.api.entity.common.ResponseResult
|
||||
import top.fatweb.api.param.system.SysLogGetParam
|
||||
@@ -44,9 +43,7 @@ class SysLogController(
|
||||
@PreAuthorize("hasAnyAuthority('system:log:query:all')")
|
||||
fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult<PageVo<SysLogVo>> {
|
||||
return ResponseResult.success(
|
||||
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
|
||||
sysLogService.getPage(sysLogGetParam)
|
||||
)
|
||||
ResponseCode.DATABASE_SELECT_SUCCESS, data = sysLogService.getPage(sysLogGetParam)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
|
||||
* @param logType List of log types
|
||||
* @param requestMethod List of request methods
|
||||
* @param searchRequestUrl Request URL to search for
|
||||
* @param searchRegex Use regex
|
||||
* @param searchStartTime Start time to search for
|
||||
* @param searchEndTime end time to search for
|
||||
* @return System log in page
|
||||
@@ -38,7 +37,6 @@ interface SysLogMapper : BaseMapper<SysLog> {
|
||||
logType: List<String>?,
|
||||
requestMethod: List<String>?,
|
||||
searchRequestUrl: String?,
|
||||
searchRegex: Boolean,
|
||||
searchStartTime: LocalDateTime?,
|
||||
searchEndTime: LocalDateTime?
|
||||
): IPage<SysLog>
|
||||
|
||||
@@ -51,8 +51,10 @@ data class SysLogGetParam(
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
/*
|
||||
@Schema(description = "查询使用正则表达式", allowableValues = ["true", "false"], defaultValue = "false")
|
||||
val searchRegex: Boolean = false,
|
||||
*/
|
||||
|
||||
/**
|
||||
* Start time to search for
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package top.fatweb.api.service.permission.impl
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem
|
||||
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
||||
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
|
||||
@@ -47,6 +48,7 @@ import java.time.ZoneOffset
|
||||
* @see User
|
||||
* @see IUserService
|
||||
*/
|
||||
@DS("master")
|
||||
@Service
|
||||
class UserServiceImpl(
|
||||
private val passwordEncoder: PasswordEncoder,
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package top.fatweb.api.service.system
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage
|
||||
import com.baomidou.mybatisplus.extension.service.IService
|
||||
import top.fatweb.api.entity.system.SysLog
|
||||
import top.fatweb.api.param.system.SysLogGetParam
|
||||
import top.fatweb.api.vo.PageVo
|
||||
import top.fatweb.api.vo.system.SysLogVo
|
||||
|
||||
/**
|
||||
* System log service interface
|
||||
@@ -22,8 +23,8 @@ interface ISysLogService : IService<SysLog> {
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
* @see SysLogGetParam
|
||||
* @see IPage
|
||||
* @see SysLog
|
||||
* @see PageVo
|
||||
* @see SysLogVo
|
||||
*/
|
||||
fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog>
|
||||
fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo>
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package top.fatweb.api.service.system.impl
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage
|
||||
import com.baomidou.dynamic.datasource.annotation.DS
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
||||
import org.springframework.stereotype.Service
|
||||
import top.fatweb.api.converter.system.SysLogConverter
|
||||
import top.fatweb.api.entity.system.SysLog
|
||||
import top.fatweb.api.mapper.system.SysLogMapper
|
||||
import top.fatweb.api.param.system.SysLogGetParam
|
||||
import top.fatweb.api.service.permission.IUserService
|
||||
import top.fatweb.api.service.system.ISysLogService
|
||||
import top.fatweb.api.util.PageUtil
|
||||
import top.fatweb.api.vo.PageVo
|
||||
import top.fatweb.api.vo.system.SysLogVo
|
||||
|
||||
/**
|
||||
* System log service implement
|
||||
@@ -21,21 +25,29 @@ import top.fatweb.api.util.PageUtil
|
||||
* @see SysLog
|
||||
* @see ISysLogService
|
||||
*/
|
||||
@DS("sqlite")
|
||||
@Service
|
||||
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
|
||||
override fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> {
|
||||
class SysLogServiceImpl(
|
||||
private val userService: IUserService
|
||||
) : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
|
||||
override fun getPage(sysLogGetParam: SysLogGetParam?): PageVo<SysLogVo> {
|
||||
val sysLogPage = Page<SysLog>(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20)
|
||||
|
||||
PageUtil.setPageSort(sysLogGetParam, sysLogPage, OrderItem.desc("start_time"))
|
||||
|
||||
return baseMapper.selectPage(
|
||||
val sysLogIPage = baseMapper.selectPage(
|
||||
sysLogPage,
|
||||
sysLogGetParam?.logType?.split(","),
|
||||
sysLogGetParam?.requestMethod?.split(","),
|
||||
sysLogGetParam?.searchRequestUrl,
|
||||
sysLogGetParam?.searchRegex ?: false,
|
||||
sysLogGetParam?.searchStartTime,
|
||||
sysLogGetParam?.searchEndTime
|
||||
)
|
||||
sysLogIPage.records.forEach {
|
||||
it.operateUsername =
|
||||
it.operateUserId?.let { it1 -> userService.getOne(it1)?.username }
|
||||
}
|
||||
|
||||
return SysLogConverter.sysLogPageToSysLogPageVo(sysLogIPage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ spring:
|
||||
master:
|
||||
type: com.zaxxer.hikari.HikariDataSource
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
local:
|
||||
sqlite:
|
||||
type: com.zaxxer.hikari.HikariDataSource
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
url: jdbc:sqlite::resource:db/sqlite.db
|
||||
url: jdbc:sqlite:data/db/sqlite.db?date_string_format=yyyy-MM-dd'T'HH:mm:ss.SSS
|
||||
|
||||
|
||||
jackson:
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
drop table if exists t_sys_log;
|
||||
create table t_sys_log
|
||||
(
|
||||
id bigint not null,
|
||||
log_type varchar(50) not null comment '日志类型',
|
||||
operate_user_id bigint not null comment '操作用户',
|
||||
operate_time datetime(3) not null default (utc_timestamp()) comment '操作时间',
|
||||
request_uri varchar(500) default null comment '请求 URI',
|
||||
request_method varchar(10) default null comment '请求方式',
|
||||
request_params text comment '请求参数',
|
||||
request_ip varchar(20) not null comment '请求 IP',
|
||||
request_server_address varchar(50) not null comment '请求服务器地址',
|
||||
exception int not null default 0 comment '是否异常',
|
||||
exception_info text comment '异常信息',
|
||||
start_time datetime(3) not null comment '开始时间',
|
||||
end_time datetime(3) not null comment '结束时间',
|
||||
execute_time bigint default null comment '执行时间',
|
||||
user_agent varchar(500) default null comment '用户代理',
|
||||
primary key (id) using btree,
|
||||
key idx_sys_log_log_type (log_type) using btree,
|
||||
key idx_sys_log_operate_user_id (operate_user_id) using btree,
|
||||
key idx_sys_log_exception (exception) using btree,
|
||||
key idx_sys_log_operate_time (operate_time) using btree
|
||||
) comment '系统日志表';
|
||||
Binary file not shown.
@@ -16,10 +16,8 @@
|
||||
t_sys_log.start_time as sys_log_start_time,
|
||||
t_sys_log.end_time as sys_log_end_time,
|
||||
t_sys_log.execute_time as sys_log_execute_time,
|
||||
t_sys_log.user_agent as sys_log_user_agent,
|
||||
t_user.username as sys_log_operate_username
|
||||
t_sys_log.user_agent as sys_log_user_agent
|
||||
from t_sys_log
|
||||
left join t_user on t_user.id = t_sys_log.operate_user_id
|
||||
<where>
|
||||
<foreach collection="logType" item="item" index="index" open="and t_sys_log.log_type in (" separator=","
|
||||
close=")" nullable="true">
|
||||
@@ -30,17 +28,9 @@
|
||||
#{item}
|
||||
</foreach>
|
||||
<if test="searchRequestUrl != null">
|
||||
<choose>
|
||||
<when test="searchRegex == true">
|
||||
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri),
|
||||
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) regexp #{searchRequestUrl}
|
||||
</when>
|
||||
<otherwise>
|
||||
and concat_ws('?', concat(t_sys_log.request_server_address, t_sys_log.request_uri),
|
||||
if(length(t_sys_log.request_params) != 0, t_sys_log.request_params, null)) like concat('%',
|
||||
#{searchRequestUrl}, '%')
|
||||
</otherwise>
|
||||
</choose>
|
||||
and t_sys_log.request_server_address || t_sys_log.request_uri ||
|
||||
case when length(t_sys_log.request_params) != 0 then '?' || t_sys_log.request_params else '' end like
|
||||
'%'||'mail'||'%'
|
||||
</if>
|
||||
<if test="searchStartTime != null and searchEndTime != null">
|
||||
and t_sys_log.start_time between #{searchStartTime} and #{searchEndTime}
|
||||
@@ -64,7 +54,5 @@
|
||||
<result property="endTime" column="sys_log_end_time"/>
|
||||
<result property="executeTime" column="sys_log_execute_time"/>
|
||||
<result property="userAgent" column="sys_log_user_agent"/>
|
||||
<result property="operateUsername" column="sys_log_operate_username"/>
|
||||
</resultMap>
|
||||
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user