loki注解和切面写入日志、日志管理定时器销毁问题

This commit is contained in:
lyd
2022-10-11 11:09:52 +08:00
parent e64c2d0383
commit c3048a5f01
6 changed files with 110 additions and 4 deletions

View File

@@ -9,8 +9,10 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.modules.common.config.RsaProperties;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.RedisUtils;
import org.nl.modules.common.utils.RsaUtils;
import org.nl.modules.common.utils.dto.CurrentUser;
import org.nl.modules.security.service.dto.AuthUserDto;
import org.nl.modules.system.service.RoleService;
@@ -48,8 +50,8 @@ public class MobileAuthorizationController {
@SaIgnore
public ResponseEntity<Object> login(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
// 密码解密 - 前端的加密规则: encrypt根据实际更改
String password = authUser.getPassword();
// String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
// String password = authUser.getPassword();
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
// 校验数据库
// 根据用户名查询,在比对密码
UserDto userDto = userService.findByName(authUser.getUsername()); // 拿不到已经抛出异常
@@ -75,7 +77,6 @@ public class MobileAuthorizationController {
// 返回 token 与 用户信息
JSONObject jsonObject = new JSONObject();
jsonObject.put("roles", permissionList);
jsonObject.put("user", userDto);
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
put("token", StpUtil.getTokenValue());

View File

@@ -0,0 +1,15 @@
package org.nl.wms.log;
import java.lang.annotation.*;
/**
* @author: lyd
* @description: 自定义日志注解,用作LOKI日志分类
* @Date: 2022/10/10
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
@Documented
public @interface LokiLog {
LokiLogType type() default LokiLogType.DEFAULT;
}

View File

@@ -0,0 +1,63 @@
package org.nl.wms.log;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author: lyd
* @description: 自定义日志切面:https://cloud.tencent.com/developer/article/1655923
* @Date: 2022/10/10
*/
@Aspect
@Slf4j
@Component
public class LokiLogAspect {
/**
* 切到所有OperatorLog注解修饰的方法
*/
@Pointcut("@annotation(org.nl.wms.log.LokiLog)")
public void operatorLog() {
// 空方法
}
/**
* 利用@Around环绕增强
*
* @return
*/
@Around("operatorLog()")
public synchronized Object around(ProceedingJoinPoint pjp) throws Throwable {
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request = attributes.getRequest();
// HttpServletResponse response = attributes.getResponse();
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
LokiLog lokiLog = method.getAnnotation(LokiLog.class);
// 获取描述信息
LokiLogType logType = lokiLog.type();
MDC.put("log_file_type", logType.name());
log.info("入参:" + JSONObject.toJSONString(pjp.getArgs()));
Object proceed = pjp.proceed();
log.info("返回参数:" + JSONObject.toJSONString(proceed));
MDC.remove("log_file_type");
return proceed;
}
}

View File

@@ -0,0 +1,17 @@
package org.nl.wms.log;
/**
* @author: lyd
* @description:
* @Date: 2022/10/11
*/
public enum LokiLogType {
DEFAULT( "默认"),
LMS_TO_MES( "LMS请求MES"),
MES_TO_LMS( "MES请求LMS");
private String desc;
LokiLogType(String desc) {
}
}

View File

@@ -79,12 +79,15 @@ https://juejin.cn/post/6844903775631572999
<springProfile name="dev">
<root level="info">
<appender-ref ref="CONSOLE"/>
<!-- <appender-ref ref="lokiAppender" />-->
<appender-ref ref="lokiAppender" />
</root>
<logger name="jdbc.audit" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="jdbc.sqltiming" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="jdbc.resultset" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>

View File

@@ -216,6 +216,13 @@ export default {
created() {
this.initLabelOptions()
},
// 关闭定时器 - 加了缓存就必须使用deactivated
deactivated() {
// js提供的clearInterval方法用来清除定时器
console.log('定时任务销毁')
clearInterval(this.timer)
window.removeEventListener('scroll', this.handleScroll)
},
beforeDestroy() {
// js提供的clearInterval方法用来清除定时器
console.log('定时任务销毁')