Optimize get sys log api

This commit is contained in:
2023-11-03 18:24:58 +08:00
parent e5c71c8a51
commit b4f1b655dc
12 changed files with 102 additions and 55 deletions

View File

@@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
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.RestController
import top.fatweb.api.converter.system.SysLogConverter
@@ -31,11 +30,11 @@ class SysLogController(
) {
@Operation(summary = "获取")
@GetMapping
fun get(@Valid @RequestBody sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
fun get(@Valid sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
sysLogService.getPage(
sysLogGetParam.page, sysLogGetParam.pageSize
sysLogGetParam.currentPage, sysLogGetParam.pageSize
)
)
)

View File

@@ -13,21 +13,21 @@ object SysLogConverter {
syslogPage.current,
syslogPage.records.map {
SysLogGetVo(
it.id,
it.logType,
it.operateUserId,
it.operateTime,
it.requestUri,
it.requestMethod,
it.requestParams,
it.requestIp,
it.requestServerAddress,
it.isException,
it.exceptionInfo,
it.startTime,
it.endTime,
it.executeTime,
it.userAgent
id = it.id,
logType = it.logType,
operateUserId = it.operateUserId,
operateTime = it.operateTime,
requestUri = it.requestUri,
requestMethod = it.requestMethod,
requestParams = it.requestParams,
requestIp = it.requestIp,
requestServerAddress = it.requestServerAddress,
exception = it.exception == 1,
exceptionInfo = it.exceptionInfo,
startTime = it.startTime,
endTime = it.endTime,
executeTime = it.executeTime,
userAgent = it.userAgent
)
})

View File

@@ -71,8 +71,8 @@ class SysLog : Serializable {
/**
* 是否异常
*/
@TableField("is_exception")
var isException: Int? = null
@TableField("exception")
var exception: Int? = null
/**
* 异常信息
@@ -104,7 +104,10 @@ class SysLog : Serializable {
@TableField("user_agent")
var userAgent: String? = null
@TableField(exist = false)
var operateUsername: String? = null
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)"
}
}

View File

@@ -2,8 +2,6 @@ package top.fatweb.api.interceptor
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.core.MethodParameter
import org.springframework.http.MediaType
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.servlet.HandlerInterceptor
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.system.SysLog
import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.util.WebUtil
import java.net.URI
@@ -27,7 +25,6 @@ import java.util.concurrent.Executor
class SysLogInterceptor(
private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService
) : HandlerInterceptor, ResponseBodyAdvice<Any> {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
private val sysLogThreadLocal = ThreadLocal<SysLog>()
private val resultThreadLocal = ThreadLocal<Any>()
@@ -59,12 +56,12 @@ class SysLogInterceptor(
if (result.success) {
sysLog.apply {
logType = "INFO"
isException = 0
exception = 0
}
} else {
sysLog.apply {
logType = "ERROR"
isException = 1
exception = 1
exceptionInfo = result.msg
}
}

View File

@@ -2,6 +2,7 @@ package top.fatweb.api.mapper.system
import top.fatweb.api.entity.system.SysLog
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.core.metadata.IPage
import org.apache.ibatis.annotations.Mapper
/**
@@ -13,4 +14,6 @@ import org.apache.ibatis.annotations.Mapper
* @since 2023-10-18
*/
@Mapper
interface SysLogMapper : BaseMapper<SysLog>
interface SysLogMapper : BaseMapper<SysLog> {
fun selectPage(page: IPage<SysLog>): IPage<SysLog>
}

View 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
}

View File

@@ -2,16 +2,14 @@ package top.fatweb.api.param.authentication
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
import java.io.Serializable
@Schema(description = "登录请求参数")
class LoginParam : Serializable {
data class LoginParam(
@Schema(description = "用户名", example = "test", required = true)
@NotBlank(message = "Username can not be blank")
val username: String? = null
@field:NotBlank(message = "Username can not be blank")
val username: String? = null,
@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
}
)

View File

@@ -1,17 +1,9 @@
package top.fatweb.api.param.system
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Min
import java.io.Serializable
import top.fatweb.api.param.PageParam
@Schema(description = "获取系统日志请求参数")
class SysLogGetParam : Serializable {
@Schema(description = "分页页码", example = "1", defaultValue = "1")
@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
}
data class SysLogGetParam(
val logType: String? = null
) : PageParam()

View File

@@ -1,7 +1,6 @@
package top.fatweb.api.service.system.impl
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.service.impl.ServiceImpl
import org.springframework.stereotype.Service
@@ -20,10 +19,8 @@ import top.fatweb.api.service.system.ISysLogService
@Service
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
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 sysLogPage
return baseMapper.selectPage(sysLogPage)
}
}

View File

@@ -1,16 +1,20 @@
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 java.time.LocalDateTime
@Schema(description = "获取系统日志返回参数")
class SysLogGetVo(
@JsonSerialize(using = ToStringSerializer::class)
val id: Long?,
@Schema(description = "日志类型")
val logType: String?,
@Schema(description = "操作用户 ID")
@JsonSerialize(using = ToStringSerializer::class)
val operateUserId: Long?,
@Schema(description = "操作时间")
@@ -32,7 +36,7 @@ class SysLogGetVo(
val requestServerAddress: String?,
@Schema(description = "是否异常")
val isException: Int?,
val exception: Boolean?,
@Schema(description = "异常信息")
val exceptionInfo: String?,