Optimize get sys log api
This commit is contained in:
@@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.Operation
|
|||||||
import io.swagger.v3.oas.annotations.tags.Tag
|
import io.swagger.v3.oas.annotations.tags.Tag
|
||||||
import jakarta.validation.Valid
|
import jakarta.validation.Valid
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import top.fatweb.api.converter.system.SysLogConverter
|
import top.fatweb.api.converter.system.SysLogConverter
|
||||||
@@ -31,11 +30,11 @@ class SysLogController(
|
|||||||
) {
|
) {
|
||||||
@Operation(summary = "获取")
|
@Operation(summary = "获取")
|
||||||
@GetMapping
|
@GetMapping
|
||||||
fun get(@Valid @RequestBody sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
|
fun get(@Valid sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
|
||||||
return ResponseResult.success(
|
return ResponseResult.success(
|
||||||
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
|
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
|
||||||
sysLogService.getPage(
|
sysLogService.getPage(
|
||||||
sysLogGetParam.page, sysLogGetParam.pageSize
|
sysLogGetParam.currentPage, sysLogGetParam.pageSize
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,21 +13,21 @@ object SysLogConverter {
|
|||||||
syslogPage.current,
|
syslogPage.current,
|
||||||
syslogPage.records.map {
|
syslogPage.records.map {
|
||||||
SysLogGetVo(
|
SysLogGetVo(
|
||||||
it.id,
|
id = it.id,
|
||||||
it.logType,
|
logType = it.logType,
|
||||||
it.operateUserId,
|
operateUserId = it.operateUserId,
|
||||||
it.operateTime,
|
operateTime = it.operateTime,
|
||||||
it.requestUri,
|
requestUri = it.requestUri,
|
||||||
it.requestMethod,
|
requestMethod = it.requestMethod,
|
||||||
it.requestParams,
|
requestParams = it.requestParams,
|
||||||
it.requestIp,
|
requestIp = it.requestIp,
|
||||||
it.requestServerAddress,
|
requestServerAddress = it.requestServerAddress,
|
||||||
it.isException,
|
exception = it.exception == 1,
|
||||||
it.exceptionInfo,
|
exceptionInfo = it.exceptionInfo,
|
||||||
it.startTime,
|
startTime = it.startTime,
|
||||||
it.endTime,
|
endTime = it.endTime,
|
||||||
it.executeTime,
|
executeTime = it.executeTime,
|
||||||
it.userAgent
|
userAgent = it.userAgent
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ class SysLog : Serializable {
|
|||||||
/**
|
/**
|
||||||
* 是否异常
|
* 是否异常
|
||||||
*/
|
*/
|
||||||
@TableField("is_exception")
|
@TableField("exception")
|
||||||
var isException: Int? = null
|
var exception: Int? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常信息
|
* 异常信息
|
||||||
@@ -104,7 +104,10 @@ class SysLog : Serializable {
|
|||||||
@TableField("user_agent")
|
@TableField("user_agent")
|
||||||
var userAgent: String? = null
|
var userAgent: String? = null
|
||||||
|
|
||||||
|
@TableField(exist = false)
|
||||||
|
var operateUsername: String? = null
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "SysLog(id=$id, logType=$logType, operateUserId=$operateUserId, operateTime=$operateTime, requestUri=$requestUri, requestMethod=$requestMethod, requestParams=$requestParams, requestIp=$requestIp, requestServerAddress=$requestServerAddress, isException=$isException, exceptionInfo=$exceptionInfo, startTime=$startTime, endTime=$endTime, executeTime=$executeTime, userAgent=$userAgent)"
|
return "SysLog(id=$id, logType=$logType, operateUserId=$operateUserId, operateTime=$operateTime, requestUri=$requestUri, requestMethod=$requestMethod, requestParams=$requestParams, requestIp=$requestIp, requestServerAddress=$requestServerAddress, exception=$exception, exceptionInfo=$exceptionInfo, startTime=$startTime, endTime=$endTime, executeTime=$executeTime, userAgent=$userAgent, operateUsername=$operateUsername)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package top.fatweb.api.interceptor
|
|||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest
|
import jakarta.servlet.http.HttpServletRequest
|
||||||
import jakarta.servlet.http.HttpServletResponse
|
import jakarta.servlet.http.HttpServletResponse
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
import org.springframework.core.MethodParameter
|
import org.springframework.core.MethodParameter
|
||||||
import org.springframework.http.MediaType
|
import org.springframework.http.MediaType
|
||||||
import org.springframework.http.converter.HttpMessageConverter
|
import org.springframework.http.converter.HttpMessageConverter
|
||||||
@@ -12,8 +10,8 @@ import org.springframework.http.server.ServerHttpResponse
|
|||||||
import org.springframework.web.bind.annotation.ControllerAdvice
|
import org.springframework.web.bind.annotation.ControllerAdvice
|
||||||
import org.springframework.web.servlet.HandlerInterceptor
|
import org.springframework.web.servlet.HandlerInterceptor
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
|
||||||
import top.fatweb.api.entity.system.SysLog
|
|
||||||
import top.fatweb.api.entity.common.ResponseResult
|
import top.fatweb.api.entity.common.ResponseResult
|
||||||
|
import top.fatweb.api.entity.system.SysLog
|
||||||
import top.fatweb.api.service.system.ISysLogService
|
import top.fatweb.api.service.system.ISysLogService
|
||||||
import top.fatweb.api.util.WebUtil
|
import top.fatweb.api.util.WebUtil
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
@@ -27,7 +25,6 @@ import java.util.concurrent.Executor
|
|||||||
class SysLogInterceptor(
|
class SysLogInterceptor(
|
||||||
private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService
|
private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService
|
||||||
) : HandlerInterceptor, ResponseBodyAdvice<Any> {
|
) : HandlerInterceptor, ResponseBodyAdvice<Any> {
|
||||||
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
|
||||||
private val sysLogThreadLocal = ThreadLocal<SysLog>()
|
private val sysLogThreadLocal = ThreadLocal<SysLog>()
|
||||||
private val resultThreadLocal = ThreadLocal<Any>()
|
private val resultThreadLocal = ThreadLocal<Any>()
|
||||||
|
|
||||||
@@ -59,12 +56,12 @@ class SysLogInterceptor(
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
sysLog.apply {
|
sysLog.apply {
|
||||||
logType = "INFO"
|
logType = "INFO"
|
||||||
isException = 0
|
exception = 0
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sysLog.apply {
|
sysLog.apply {
|
||||||
logType = "ERROR"
|
logType = "ERROR"
|
||||||
isException = 1
|
exception = 1
|
||||||
exceptionInfo = result.msg
|
exceptionInfo = result.msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package top.fatweb.api.mapper.system
|
|||||||
|
|
||||||
import top.fatweb.api.entity.system.SysLog
|
import top.fatweb.api.entity.system.SysLog
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage
|
||||||
import org.apache.ibatis.annotations.Mapper
|
import org.apache.ibatis.annotations.Mapper
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,4 +14,6 @@ import org.apache.ibatis.annotations.Mapper
|
|||||||
* @since 2023-10-18
|
* @since 2023-10-18
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
interface SysLogMapper : BaseMapper<SysLog>
|
interface SysLogMapper : BaseMapper<SysLog> {
|
||||||
|
fun selectPage(page: IPage<SysLog>): IPage<SysLog>
|
||||||
|
}
|
||||||
|
|||||||
14
src/main/kotlin/top/fatweb/api/param/PageParam.kt
Normal file
14
src/main/kotlin/top/fatweb/api/param/PageParam.kt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package top.fatweb.api.param
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema
|
||||||
|
import jakarta.validation.constraints.Min
|
||||||
|
|
||||||
|
open class PageParam {
|
||||||
|
@Schema(description = "分页页码", example = "1", defaultValue = "1")
|
||||||
|
@field:Min(1, message = "Pagination page number must be a positive integer")
|
||||||
|
var currentPage: Long = 1
|
||||||
|
|
||||||
|
@Schema(description = "分页大小", example = "20", defaultValue = "20")
|
||||||
|
@field:Min(1, message = "The number of data per page must be a positive integer")
|
||||||
|
var pageSize: Long = 20
|
||||||
|
}
|
||||||
@@ -2,16 +2,14 @@ package top.fatweb.api.param.authentication
|
|||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema
|
import io.swagger.v3.oas.annotations.media.Schema
|
||||||
import jakarta.validation.constraints.NotBlank
|
import jakarta.validation.constraints.NotBlank
|
||||||
import java.io.Serializable
|
|
||||||
|
|
||||||
@Schema(description = "登录请求参数")
|
@Schema(description = "登录请求参数")
|
||||||
class LoginParam : Serializable {
|
data class LoginParam(
|
||||||
|
|
||||||
@Schema(description = "用户名", example = "test", required = true)
|
@Schema(description = "用户名", example = "test", required = true)
|
||||||
@NotBlank(message = "Username can not be blank")
|
@field:NotBlank(message = "Username can not be blank")
|
||||||
val username: String? = null
|
val username: String? = null,
|
||||||
|
|
||||||
@Schema(description = "密码", example = "test123456", required = true)
|
@Schema(description = "密码", example = "test123456", required = true)
|
||||||
@NotBlank(message = "Password can not be blank")
|
@field:NotBlank(message = "Password can not be blank")
|
||||||
val password: String? = null
|
val password: String? = null
|
||||||
}
|
)
|
||||||
@@ -1,17 +1,9 @@
|
|||||||
package top.fatweb.api.param.system
|
package top.fatweb.api.param.system
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema
|
import io.swagger.v3.oas.annotations.media.Schema
|
||||||
import jakarta.validation.constraints.Min
|
import top.fatweb.api.param.PageParam
|
||||||
import java.io.Serializable
|
|
||||||
|
|
||||||
@Schema(description = "获取系统日志请求参数")
|
@Schema(description = "获取系统日志请求参数")
|
||||||
class SysLogGetParam : Serializable {
|
data class SysLogGetParam(
|
||||||
|
val logType: String? = null
|
||||||
@Schema(description = "分页页码", example = "1", defaultValue = "1")
|
) : PageParam()
|
||||||
@Min(1, message = "Pagination page number must be a positive integer")
|
|
||||||
val page: Long = 1
|
|
||||||
|
|
||||||
@Schema(description = "分页大小", example = "20", defaultValue = "20")
|
|
||||||
@Min(1, message = "The number of data per page must be a positive integer")
|
|
||||||
val pageSize: Long = 20
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package top.fatweb.api.service.system.impl
|
package top.fatweb.api.service.system.impl
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage
|
import com.baomidou.mybatisplus.core.metadata.IPage
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
@@ -20,10 +19,8 @@ import top.fatweb.api.service.system.ISysLogService
|
|||||||
@Service
|
@Service
|
||||||
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
|
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
|
||||||
override fun getPage(page: Long, pageSize: Long): IPage<SysLog> {
|
override fun getPage(page: Long, pageSize: Long): IPage<SysLog> {
|
||||||
var sysLogPage = Page<SysLog>(page, pageSize)
|
val sysLogPage = Page<SysLog>(page, pageSize)
|
||||||
|
|
||||||
sysLogPage = baseMapper.selectPage(sysLogPage, KtQueryWrapper(SysLog()).orderByDesc(SysLog::id))
|
return baseMapper.selectPage(sysLogPage)
|
||||||
|
|
||||||
return sysLogPage
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
package top.fatweb.api.vo.system
|
package top.fatweb.api.vo.system
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
|
||||||
import io.swagger.v3.oas.annotations.media.Schema
|
import io.swagger.v3.oas.annotations.media.Schema
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Schema(description = "获取系统日志返回参数")
|
@Schema(description = "获取系统日志返回参数")
|
||||||
class SysLogGetVo(
|
class SysLogGetVo(
|
||||||
|
@JsonSerialize(using = ToStringSerializer::class)
|
||||||
val id: Long?,
|
val id: Long?,
|
||||||
|
|
||||||
@Schema(description = "日志类型")
|
@Schema(description = "日志类型")
|
||||||
val logType: String?,
|
val logType: String?,
|
||||||
|
|
||||||
@Schema(description = "操作用户 ID")
|
@Schema(description = "操作用户 ID")
|
||||||
|
@JsonSerialize(using = ToStringSerializer::class)
|
||||||
val operateUserId: Long?,
|
val operateUserId: Long?,
|
||||||
|
|
||||||
@Schema(description = "操作时间")
|
@Schema(description = "操作时间")
|
||||||
@@ -32,7 +36,7 @@ class SysLogGetVo(
|
|||||||
val requestServerAddress: String?,
|
val requestServerAddress: String?,
|
||||||
|
|
||||||
@Schema(description = "是否异常")
|
@Schema(description = "是否异常")
|
||||||
val isException: Int?,
|
val exception: Boolean?,
|
||||||
|
|
||||||
@Schema(description = "异常信息")
|
@Schema(description = "异常信息")
|
||||||
val exceptionInfo: String?,
|
val exceptionInfo: String?,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ create table t_sys_log
|
|||||||
request_params text comment '请求参数',
|
request_params text comment '请求参数',
|
||||||
request_ip varchar(20) not null comment '请求 IP',
|
request_ip varchar(20) not null comment '请求 IP',
|
||||||
request_server_address varchar(50) not null comment '请求服务器地址',
|
request_server_address varchar(50) not null comment '请求服务器地址',
|
||||||
is_exception int not null default 0 comment '是否异常',
|
exception int not null default 0 comment '是否异常',
|
||||||
exception_info text comment '异常信息',
|
exception_info text comment '异常信息',
|
||||||
start_time datetime(3) not null comment '开始时间',
|
start_time datetime(3) not null comment '开始时间',
|
||||||
end_time datetime(3) not null comment '结束时间',
|
end_time datetime(3) not null comment '结束时间',
|
||||||
@@ -19,6 +19,6 @@ create table t_sys_log
|
|||||||
primary key (id) using btree,
|
primary key (id) using btree,
|
||||||
key idx_sys_log_log_type (log_type) 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_operate_user_id (operate_user_id) using btree,
|
||||||
key idx_sys_log_is_exception (is_exception) using btree,
|
key idx_sys_log_exception (exception) using btree,
|
||||||
key idx_sys_log_operate_time (operate_time) using btree
|
key idx_sys_log_operate_time (operate_time) using btree
|
||||||
) comment '系统日志表';
|
) comment '系统日志表';
|
||||||
@@ -1,5 +1,45 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="top.fatweb.api.mapper.system.SysLogMapper">
|
<mapper namespace="top.fatweb.api.mapper.system.SysLogMapper">
|
||||||
|
<select id="selectPage" resultMap="sysLogMap">
|
||||||
|
select t_sys_log.id as sys_log_id,
|
||||||
|
t_sys_log.log_type as sys_log_log_type,
|
||||||
|
t_sys_log.operate_user_id as sys_log_operate_user_id,
|
||||||
|
t_sys_log.operate_time as sys_log_operate_time,
|
||||||
|
t_sys_log.request_uri as sys_log_request_uri,
|
||||||
|
t_sys_log.request_method as sys_log_request_method,
|
||||||
|
t_sys_log.request_params as sys_log_request_params,
|
||||||
|
t_sys_log.request_ip as sys_log_request_ip,
|
||||||
|
t_sys_log.request_server_address as sys_log_request_server_address,
|
||||||
|
t_sys_log.exception as sys_log_exception,
|
||||||
|
t_sys_log.exception_info as sys_log_exception_info,
|
||||||
|
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
|
||||||
|
from t_sys_log
|
||||||
|
left join t_user on t_user.id = t_sys_log.operate_user_id
|
||||||
|
order by create_time desc
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<resultMap id="sysLogMap" type="sysLog">
|
||||||
|
<id property="id" column="sys_log_id"/>
|
||||||
|
<result property="logType" column="sys_log_log_type"/>
|
||||||
|
<result property="operateUserId" column="sys_log_operate_user_id"/>
|
||||||
|
<result property="operateTime" column="sys_log_operate_time"/>
|
||||||
|
<result property="requestUri" column="sys_log_request_uri"/>
|
||||||
|
<result property="requestMethod" column="sys_log_request_method"/>
|
||||||
|
<result property="requestParams" column="sys_log_request_params"/>
|
||||||
|
<result property="requestIp" column="sys_log_request_ip"/>
|
||||||
|
<result property="requestServerAddress" column="sys_log_request_server_address"/>
|
||||||
|
<result property="exception" column="sys_log_exception"/>
|
||||||
|
<result property="exceptionInfo" column="sys_log_exception_info"/>
|
||||||
|
<result property="startTime" column="sys_log_start_time"/>
|
||||||
|
<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>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user