Add system log api

This commit is contained in:
2023-10-23 16:08:43 +08:00
parent f927851cb0
commit 634e2f6a67
17 changed files with 240 additions and 68 deletions

View File

@@ -10,6 +10,7 @@ class SysLogConfig(
private val sysLogInterceptor: SysLogInterceptor
) : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(sysLogInterceptor).addPathPatterns("/**").excludePathPatterns("/error/thrown")
registry.addInterceptor(sysLogInterceptor).addPathPatterns("/**")
.excludePathPatterns("/error/thrown", "/webjars/**")
}
}

View File

@@ -1,17 +0,0 @@
package top.fatweb.api.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 系统日志表 前端控制器
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
@RestController
@RequestMapping("/api/sysLog")
class SysLogController

View File

@@ -0,0 +1,43 @@
package top.fatweb.api.controller.system;
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.SysLogConverter
import top.fatweb.api.entity.common.ResponseCode
import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.param.system.SysLogGetParam
import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogGetVo
/**
* <p>
* 系统日志表 前端控制器
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
@RestController
@RequestMapping("/system/log")
@Tag(name = "系统日志", description = "系统日志相关接口")
class SysLogController(
private val sysLogService: ISysLogService
) {
@Operation(summary = "获取")
@GetMapping
fun get(@Valid @RequestBody sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
return ResponseResult.success(
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
sysLogService.getPage(
sysLogGetParam.page, sysLogGetParam.pageSize
)
)
)
}
}

View File

@@ -0,0 +1,34 @@
package top.fatweb.api.converter
import com.baomidou.mybatisplus.core.metadata.IPage
import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.system.SysLogGetVo
object SysLogConverter {
fun sysLogPageToSysLogPageVo(syslogPage: IPage<SysLog>): PageVo<SysLogGetVo> = PageVo<SysLogGetVo>(
syslogPage.total,
syslogPage.pages,
syslogPage.size,
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
)
})
}

View File

@@ -18,7 +18,16 @@ enum class ResponseCode(val code: Int) {
SYSTEM_REQUEST_ILLEGAL(BusinessCode.SYSTEM, 40),
SYSTEM_ARGUMENT_NOT_VALID(BusinessCode.SYSTEM, 41),
SYSTEM_ERROR(BusinessCode.SYSTEM, 50),
SYSTEM_TIMEOUT(BusinessCode.SYSTEM, 51);
SYSTEM_TIMEOUT(BusinessCode.SYSTEM, 51),
DATABASE_SELECT_SUCCESS(BusinessCode.DATABASE, 0),
DATABASE_SELECT_FAILED(BusinessCode.DATABASE, 5),
DATABASE_INSERT_SUCCESS(BusinessCode.DATABASE, 10),
DATABASE_INSERT_FAILED(BusinessCode.DATABASE, 15),
DATABASE_UPDATE_SUCCESS(BusinessCode.DATABASE, 20),
DATABASE_UPDATE_FILED(BusinessCode.DATABASE, 25),
DATABASE_DELETE_SUCCESS(BusinessCode.DATABASE, 30),
DATABASE_DELETE_FILED(BusinessCode.DATABASE, 35);
constructor(businessCode: BusinessCode, code: Int) : this(businessCode.code * 100 + code)
}

View File

@@ -1,4 +1,4 @@
package top.fatweb.api.entity
package top.fatweb.api.entity.system
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
@@ -72,7 +72,7 @@ class SysLog : Serializable {
* 是否异常
*/
@TableField("is_exception")
var isException: String? = null
var isException: Int? = null
/**
* 异常信息

View File

@@ -12,9 +12,9 @@ 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.SysLog
import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.entity.common.ResponseResult
import top.fatweb.api.service.ISysLogService
import top.fatweb.api.service.system.ISysLogService
import top.fatweb.api.util.WebUtil
import java.net.URI
import java.time.LocalDateTime
@@ -25,7 +25,7 @@ import java.util.concurrent.Executor
@ControllerAdvice
class SysLogInterceptor(
val customThreadPoolTaskExecutor: Executor, val sysLogService: ISysLogService
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>()
@@ -45,8 +45,6 @@ class SysLogInterceptor(
sysLogThreadLocal.set(sysLog)
logger.info("开始计时: {} URI: {} IP: {}", sysLog.startTime, sysLog.requestUri, sysLog.requestIp)
return true
}
@@ -61,12 +59,12 @@ class SysLogInterceptor(
if (result.success) {
sysLog.apply {
logType = "INFO"
isException = "N"
isException = 0
}
} else {
sysLog.apply {
logType = "ERROR"
isException = "Y"
isException = 1
exceptionInfo = result.msg
}
}

View File

@@ -1,6 +1,6 @@
package top.fatweb.api.mapper;
package top.fatweb.api.mapper.system;
import top.fatweb.api.entity.SysLog;
import top.fatweb.api.entity.system.SysLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

View File

@@ -0,0 +1,17 @@
package top.fatweb.api.param.system
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Min
import java.io.Serializable
@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
}

View File

@@ -1,14 +0,0 @@
package top.fatweb.api.service;
import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.SysLog
/**
* <p>
* 系统日志表 服务类
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
interface ISysLogService : IService<SysLog>

View File

@@ -1,18 +0,0 @@
package top.fatweb.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service
import top.fatweb.api.entity.SysLog
import top.fatweb.api.mapper.SysLogMapper
import top.fatweb.api.service.ISysLogService
/**
* <p>
* 系统日志表 服务实现类
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
@Service
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService

View File

@@ -0,0 +1,17 @@
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
/**
* <p>
* 系统日志表 服务类
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
interface ISysLogService : IService<SysLog> {
fun getPage(page: Long, pageSize: Long): IPage<SysLog>
}

View File

@@ -0,0 +1,30 @@
package top.fatweb.api.service.system.impl;
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.core.toolkit.Wrappers
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
import top.fatweb.api.entity.system.SysLog
import top.fatweb.api.mapper.system.SysLogMapper
import top.fatweb.api.service.system.ISysLogService
/**
* <p>
* 系统日志表 服务实现类
* </p>
*
* @author FatttSnake
* @since 2023-10-18
*/
@Service
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
override fun getPage(page: Long, pageSize: Long): IPage<SysLog> {
var sysLogPage = Page<SysLog>(page, pageSize)
sysLogPage = baseMapper.selectPage(sysLogPage, KtQueryWrapper(SysLog()).orderByDesc(SysLog::id))
return sysLogPage
}
}

View File

@@ -0,0 +1,21 @@
package top.fatweb.api.vo
import io.swagger.v3.oas.annotations.media.Schema
@Schema(description = "分页返回参数")
data class PageVo<T>(
@Schema(description = "总数量", example = "100")
val total: Long,
@Schema(description = "总页码", example = "10")
val pages: Long,
@Schema(description = "分页大小", example = "10")
val size: Long,
@Schema(description = "当前页码", example = "2")
val current: Long,
@Schema(description = "数据")
val records: List<T>
)

View File

@@ -0,0 +1,51 @@
package top.fatweb.api.vo.system
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDateTime
@Schema(description = "获取系统日志返回参数")
class SysLogGetVo(
val id: Long?,
@Schema(description = "日志类型")
val logType: String?,
@Schema(description = "操作用户 ID")
val operateUserId: Long?,
@Schema(description = "操作时间")
val operateTime: LocalDateTime?,
@Schema(description = "请求 Uri")
val requestUri: String?,
@Schema(description = "请求方式")
val requestMethod: String?,
@Schema(description = "请求参数")
val requestParams: String?,
@Schema(description = "请求 IP")
val requestIp: String?,
@Schema(description = "请求服务器地址")
val requestServerAddress: String?,
@Schema(description = "是否异常")
val isException: Int?,
@Schema(description = "异常信息")
val exceptionInfo: String?,
@Schema(description = "开始时间")
val startTime: LocalDateTime?,
@Schema(description = "结束时间")
val endTime: LocalDateTime?,
@Schema(description = "执行时间")
val executeTime: Long?,
@Schema(description = "用户代理")
val userAgent: String?
)

View File

@@ -10,7 +10,7 @@ create table t_sys_log
request_params text comment '请求参数',
request_ip varchar(20) not null comment '请求 IP',
request_server_address varchar(50) not null comment '请求服务器地址',
is_exception char(1) default null comment '是否异常',
is_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 '结束时间',

View File

@@ -1,5 +1,5 @@
<?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">
<mapper namespace="top.fatweb.api.mapper.SysLogMapper">
<mapper namespace="top.fatweb.api.mapper.system.SysLogMapper">
</mapper>