diff --git a/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt b/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt index 2288877..ba1c40b 100644 --- a/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt +++ b/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt @@ -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/**") } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/controller/SysLogController.kt b/src/main/kotlin/top/fatweb/api/controller/SysLogController.kt deleted file mode 100644 index 9d58d09..0000000 --- a/src/main/kotlin/top/fatweb/api/controller/SysLogController.kt +++ /dev/null @@ -1,17 +0,0 @@ -package top.fatweb.api.controller; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - *

- * 系统日志表 前端控制器 - *

- * - * @author FatttSnake - * @since 2023-10-18 - */ -@RestController -@RequestMapping("/api/sysLog") -class SysLogController - diff --git a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt new file mode 100644 index 0000000..af7533c --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt @@ -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 + +/** + *

+ * 系统日志表 前端控制器 + *

+ * + * @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> { + return ResponseResult.success( + ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo( + sysLogService.getPage( + sysLogGetParam.page, sysLogGetParam.pageSize + ) + ) + ) + } +} diff --git a/src/main/kotlin/top/fatweb/api/converter/SysLogConverter.kt b/src/main/kotlin/top/fatweb/api/converter/SysLogConverter.kt new file mode 100644 index 0000000..cd2e475 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/converter/SysLogConverter.kt @@ -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): PageVo = PageVo( + 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 + ) + }) + +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt index f6095bb..e536c40 100644 --- a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt +++ b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/entity/SysLog.kt b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt similarity index 97% rename from src/main/kotlin/top/fatweb/api/entity/SysLog.kt rename to src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt index 0e49aa2..29b12e7 100644 --- a/src/main/kotlin/top/fatweb/api/entity/SysLog.kt +++ b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt @@ -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 /** * 异常信息 diff --git a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt index 860c565..ce26b0e 100644 --- a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt +++ b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt @@ -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 { private val logger: Logger = LoggerFactory.getLogger(this::class.java) private val sysLogThreadLocal = ThreadLocal() @@ -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 } } diff --git a/src/main/kotlin/top/fatweb/api/mapper/SysLogMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt similarity index 75% rename from src/main/kotlin/top/fatweb/api/mapper/SysLogMapper.kt rename to src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt index f913efc..110ffce 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/SysLogMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt @@ -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; diff --git a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt new file mode 100644 index 0000000..9fcb3a3 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt @@ -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 +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/ISysLogService.kt b/src/main/kotlin/top/fatweb/api/service/ISysLogService.kt deleted file mode 100644 index 206694c..0000000 --- a/src/main/kotlin/top/fatweb/api/service/ISysLogService.kt +++ /dev/null @@ -1,14 +0,0 @@ -package top.fatweb.api.service; - -import com.baomidou.mybatisplus.extension.service.IService -import top.fatweb.api.entity.SysLog - -/** - *

- * 系统日志表 服务类 - *

- * - * @author FatttSnake - * @since 2023-10-18 - */ -interface ISysLogService : IService diff --git a/src/main/kotlin/top/fatweb/api/service/impl/SysLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/impl/SysLogServiceImpl.kt deleted file mode 100644 index 8df1569..0000000 --- a/src/main/kotlin/top/fatweb/api/service/impl/SysLogServiceImpl.kt +++ /dev/null @@ -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 - -/** - *

- * 系统日志表 服务实现类 - *

- * - * @author FatttSnake - * @since 2023-10-18 - */ -@Service -class SysLogServiceImpl : ServiceImpl(), ISysLogService diff --git a/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt new file mode 100644 index 0000000..50287ec --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt @@ -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 + +/** + *

+ * 系统日志表 服务类 + *

+ * + * @author FatttSnake + * @since 2023-10-18 + */ +interface ISysLogService : IService { + fun getPage(page: Long, pageSize: Long): IPage +} diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt new file mode 100644 index 0000000..ec6c0d5 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt @@ -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 + +/** + *

+ * 系统日志表 服务实现类 + *

+ * + * @author FatttSnake + * @since 2023-10-18 + */ +@Service +class SysLogServiceImpl : ServiceImpl(), ISysLogService { + override fun getPage(page: Long, pageSize: Long): IPage { + var sysLogPage = Page(page, pageSize) + + sysLogPage = baseMapper.selectPage(sysLogPage, KtQueryWrapper(SysLog()).orderByDesc(SysLog::id)) + + return sysLogPage + } +} diff --git a/src/main/kotlin/top/fatweb/api/vo/PageVo.kt b/src/main/kotlin/top/fatweb/api/vo/PageVo.kt new file mode 100644 index 0000000..b10c786 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/PageVo.kt @@ -0,0 +1,21 @@ +package top.fatweb.api.vo + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "分页返回参数") +data class PageVo( + @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 +) diff --git a/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt new file mode 100644 index 0000000..42fe7f7 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt @@ -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? +) diff --git a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql b/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql index 01e04f1..5816ccb 100644 --- a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql +++ b/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql @@ -4,17 +4,17 @@ 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 '操作时间', + 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 '请求服务器地址', - 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 '结束时间', - execute_time bigint default null 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, diff --git a/src/main/resources/mapper/SysLogMapper.xml b/src/main/resources/mapper/SysLogMapper.xml index 6dd14f3..208129b 100644 --- a/src/main/resources/mapper/SysLogMapper.xml +++ b/src/main/resources/mapper/SysLogMapper.xml @@ -1,5 +1,5 @@ - +