diff --git a/db/schema.sql b/db/schema.sql index 73ff84c..70cc8af 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -28,7 +28,7 @@ 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 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 '请求参数', @@ -36,12 +36,10 @@ create table t_sys_log request_server_address varchar(50) not null comment '请求服务器地址', is_exception char(1) default null comment '是否异常', exception_info text comment '异常信息', - start_time datetime not null comment '开始时间', - end_time datetime not null comment '结束时间', - execute_time int 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 '用户代理', - device_name varchar(100) default null comment '操作系统', - browser_name varchar(100) 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, diff --git a/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt b/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt new file mode 100644 index 0000000..2288877 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/config/SysLogConfig.kt @@ -0,0 +1,15 @@ +package top.fatweb.api.config + +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.InterceptorRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer +import top.fatweb.api.interceptor.SysLogInterceptor + +@Configuration +class SysLogConfig( + private val sysLogInterceptor: SysLogInterceptor +) : WebMvcConfigurer { + override fun addInterceptors(registry: InterceptorRegistry) { + registry.addInterceptor(sysLogInterceptor).addPathPatterns("/**").excludePathPatterns("/error/thrown") + } +} \ 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/SysLog.kt index 2fc1047..0e49aa2 100644 --- a/src/main/kotlin/top/fatweb/api/entity/SysLog.kt +++ b/src/main/kotlin/top/fatweb/api/entity/SysLog.kt @@ -1,10 +1,10 @@ -package top.fatweb.api.entity; +package top.fatweb.api.entity -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import java.io.Serializable; -import java.time.LocalDateTime; +import com.baomidou.mybatisplus.annotation.TableField +import com.baomidou.mybatisplus.annotation.TableId +import com.baomidou.mybatisplus.annotation.TableName +import java.io.Serializable +import java.time.LocalDateTime /** *
@@ -96,7 +96,7 @@ class SysLog : Serializable {
* 执行时间
*/
@TableField("execute_time")
- var executeTime: Int? = null
+ var executeTime: Long? = null
/**
* 用户代理
@@ -104,19 +104,7 @@ class SysLog : Serializable {
@TableField("user_agent")
var userAgent: String? = null
- /**
- * 操作系统
- */
- @TableField("device_name")
- var deviceName: String? = null
-
- /**
- * 浏览器名称
- */
- @TableField("browser_name")
- var browserName: 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, deviceName=$deviceName, browserName=$browserName)"
+ 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)"
}
}
diff --git a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt
new file mode 100644
index 0000000..860c565
--- /dev/null
+++ b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt
@@ -0,0 +1,110 @@
+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
+import org.springframework.http.server.ServerHttpRequest
+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.common.ResponseResult
+import top.fatweb.api.service.ISysLogService
+import top.fatweb.api.util.WebUtil
+import java.net.URI
+import java.time.LocalDateTime
+import java.time.ZoneOffset
+import java.time.temporal.ChronoUnit
+import java.util.*
+import java.util.concurrent.Executor
+
+@ControllerAdvice
+class SysLogInterceptor(
+ val customThreadPoolTaskExecutor: Executor, val sysLogService: ISysLogService
+) : HandlerInterceptor, ResponseBodyAdvice