From c2e5248aa028d12e2d7a1e2fec4278b8c0461926 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Thu, 14 Dec 2023 18:28:59 +0800 Subject: [PATCH] Optimize code --- .../top/fatweb/api/entity/system/SysLog.kt | 17 ++++++++++++++++- .../api/interceptor/SysLogInterceptor.kt | 19 ++++++++++--------- .../fatweb/api/settings/MailSecurityType.kt | 2 +- .../top/fatweb/api/vo/system/SysLogVo.kt | 3 ++- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt index 7ab85a0..80b8c1e 100644 --- a/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt +++ b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt @@ -1,9 +1,11 @@ package top.fatweb.api.entity.system +import com.baomidou.mybatisplus.annotation.EnumValue import com.baomidou.mybatisplus.annotation.TableField import com.baomidou.mybatisplus.annotation.TableId import com.baomidou.mybatisplus.annotation.TableName import com.fasterxml.jackson.annotation.JsonFormat +import com.fasterxml.jackson.annotation.JsonValue import java.io.Serializable import java.time.LocalDateTime @@ -15,6 +17,18 @@ import java.time.LocalDateTime */ @TableName("t_sys_log") class SysLog : Serializable { + /** + * Log type enum + * + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ + enum class LogType(@field:EnumValue @field:JsonValue val code: String) { + INFO("INFO"), ERROR("ERROR"), LOGIN("LOGIN"), LOGOUT("LOGOUT"), REGISTER("REGISTER"), STATISTIC("STATISTIC"), API( + "API" + ) + } + /** * ID * @@ -29,9 +43,10 @@ class SysLog : Serializable { * * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 + * @see LogType */ @TableField("log_type") - var logType: String? = null + var logType: LogType? = null /** * Operate user ID diff --git a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt index f324d7f..d424d3c 100644 --- a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt +++ b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt @@ -2,6 +2,7 @@ package top.fatweb.api.interceptor import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import org.springframework.beans.factory.annotation.Qualifier import org.springframework.core.MethodParameter import org.springframework.http.MediaType import org.springframework.http.converter.HttpMessageConverter @@ -32,7 +33,7 @@ import java.util.concurrent.Executor */ @ControllerAdvice class SysLogInterceptor( - private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService + @Qualifier("applicationTaskExecutor") private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService ) : HandlerInterceptor, ResponseBodyAdvice { private val sysLogThreadLocal = ThreadLocal() private val resultThreadLocal = ThreadLocal() @@ -66,19 +67,19 @@ class SysLogInterceptor( sysLog.apply { logType = requestUri?.let { when { - it.startsWith("/login") -> "LOGIN" - it.startsWith("/logout") -> "LOGOUT" - it.startsWith("/register") -> "REGISTER" - it.startsWith("/system/statistic/") -> "STATISTIC" - it.startsWith("/api/") -> "API" - else -> "INFO" + it.startsWith("/login") -> SysLog.LogType.LOGIN + it.startsWith("/logout") -> SysLog.LogType.LOGOUT + it.startsWith("/register") -> SysLog.LogType.REGISTER + it.startsWith("/system/statistic/") -> SysLog.LogType.STATISTIC + it.startsWith("/api/") -> SysLog.LogType.API + else -> SysLog.LogType.INFO } - } ?: "INFO" + } ?: SysLog.LogType.INFO exception = 0 } } else { sysLog.apply { - logType = "ERROR" + logType = SysLog.LogType.ERROR exception = 1 exceptionInfo = result.msg } diff --git a/src/main/kotlin/top/fatweb/api/settings/MailSecurityType.kt b/src/main/kotlin/top/fatweb/api/settings/MailSecurityType.kt index 8288221..b7b1f85 100644 --- a/src/main/kotlin/top/fatweb/api/settings/MailSecurityType.kt +++ b/src/main/kotlin/top/fatweb/api/settings/MailSecurityType.kt @@ -10,7 +10,7 @@ enum class MailSecurityType(val code: String) { @JsonCreator fun fromCode(code: String): MailSecurityType? { - values().forEach { + entries.forEach { if (it.code == code) { return it } diff --git a/src/main/kotlin/top/fatweb/api/vo/system/SysLogVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/SysLogVo.kt index 184bbef..28e5483 100644 --- a/src/main/kotlin/top/fatweb/api/vo/system/SysLogVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/system/SysLogVo.kt @@ -3,6 +3,7 @@ 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 top.fatweb.api.entity.system.SysLog import java.time.LocalDateTime /** @@ -29,7 +30,7 @@ data class SysLogVo( * @since 1.0.0 */ @Schema(description = "日志类型") - val logType: String?, + val logType: SysLog.LogType?, /** * Operate user ID