10 changed files with 218 additions and 299 deletions
@ -1,15 +0,0 @@ |
|||
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; |
|||
} |
@ -1,63 +0,0 @@ |
|||
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.getDesc()); |
|||
log.info("输入参数:" + JSONObject.toJSONString(pjp.getArgs())); |
|||
|
|||
Object proceed = pjp.proceed(); |
|||
|
|||
log.info("返回参数:" + JSONObject.toJSONString(proceed)); |
|||
MDC.remove("log_file_type"); |
|||
return proceed; |
|||
|
|||
} |
|||
} |
@ -1,25 +0,0 @@ |
|||
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"), |
|||
LMS_TO_ACS("LMS请求ACS"), |
|||
ACS_TO_LMS("ACS请求LMS"); |
|||
|
|||
private String desc; |
|||
|
|||
LokiLogType(String desc) { |
|||
this.desc=desc; |
|||
} |
|||
|
|||
public String getDesc() { |
|||
return desc; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue