Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[李小平] #N/A refactor: Catching and Logging,修订坏味道 #390

Merged
merged 5 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alibaba.cola.catchlog;

import com.alibaba.cola.exception.BaseException;
import com.alibaba.cola.exception.BizException;
import com.alibaba.cola.exception.SysException;
import com.alibaba.fastjson.JSON;
Expand All @@ -13,21 +12,19 @@
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
* @ Description : Catching and Logging
* @ Author : Frank Zhang
* @ CreateDate : 2020/11/09
* @ Version : 1.0
* @Description : Catching and Logging
* @author : Frank Zhang
* @CreateDate : 2020/11/09
* @version : 1.0
*/
@Aspect
@Slf4j
@Order(1)
public class CatchLogAspect {

/**
* The syntax of pointcut : https://blog.csdn.net/zhengchao1991/article/details/53391244
* <a href="https://blog.csdn.net/zhengchao1991/article/details/53391244">The syntax of pointcut </a>
*/
@Pointcut("@within(CatchAndLog) && execution(public * *(..))")
public void pointcut() {
Expand All @@ -53,53 +50,54 @@ public Object around(ProceedingJoinPoint joinPoint) {

private Object handleException(ProceedingJoinPoint joinPoint, Throwable e) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Class returnType = ms.getReturnType();
Class<?> returnType = ms.getReturnType();

if (e instanceof BizException) {
log.warn("BIZ EXCEPTION : " + e.getMessage());
//在Debug的时候,对于BizException也打印堆栈
log.warn("BIZ EXCEPTION : {}", e.getMessage());
// 在Debug的时候,对于BizException也打印堆栈
if (log.isDebugEnabled()) {
log.error(e.getMessage(), e);
}
return ResponseHandlerFactory.get().handle(returnType, ((BizException) e).getErrCode(), e.getMessage());
}

if (e instanceof SysException) {
log.error("SYS EXCEPTION :");
log.error(e.getMessage(), e);
return ResponseHandlerFactory.get().handle(returnType, ((SysException) e).getErrCode(), e.getMessage());
return ResponseHandlerFactory.get().handle(returnType,
((BizException) e).getErrCode(), e.getMessage());
} else if (e instanceof SysException) {
log.error("SYS EXCEPTION: {}",e.getMessage(), e);
return ResponseHandlerFactory.get().handle(returnType,
((SysException) e).getErrCode(), e.getMessage());
}

log.error("UNKNOWN EXCEPTION :");
log.error(e.getMessage(), e);

log.error("UNKNOWN EXCEPTION: {}", e.getMessage(), e);
return ResponseHandlerFactory.get().handle(returnType, "UNKNOWN_ERROR", e.getMessage());
}


private void logResponse(long startTime, Object response) {
try {
long endTime = System.currentTimeMillis();
log.debug("RESPONSE : " + JSON.toJSONString(response));
log.debug("COST : " + (endTime - startTime) + "ms");
if (log.isDebugEnabled()) {
log.debug("RESPONSE: {}", JSON.toJSONString(response));
log.debug("COST: {}ms", (endTime - startTime));
}
} catch (Exception e) {
//swallow it
log.error("logResponse error : " + e);
log.error("logResponse error: {}", e.getMessage() , e);
}
}

private void logRequest(ProceedingJoinPoint joinPoint) {
try {
if (!log.isDebugEnabled()) {
return;
}
log.debug("START PROCESSING: " + joinPoint.getSignature().toShortString());
if (!log.isDebugEnabled()) {
return;
}

try {
log.debug("START PROCESSING: {}", joinPoint.getSignature().toShortString());
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
log.debug("REQUEST : " + JSON.toJSONString(arg, SerializerFeature.IgnoreErrorGetter));
log.debug("REQUEST: {}", JSON.toJSONString(arg, SerializerFeature.IgnoreErrorGetter));
}
} catch (Exception e) {
//swallow it
log.error("logReqeust error : " + e);
log.error("logReqeust error: {}", e.getMessage() , e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
* @author fulan.zjf 2017-11-05
*/
public class ExtensionCoordinate {

private String extensionPointName;
private String bizScenarioUniqueIdentity;

private final String extensionPointName;
private final String bizScenarioUniqueIdentity;


//Wrapper
private Class extensionPointClass;
/**
* Wrapper
*/
private Class<?> extensionPointClass;
private BizScenario bizScenario;

public Class getExtensionPointClass() {
Expand All @@ -30,26 +30,22 @@ public BizScenario getBizScenario() {
return bizScenario;
}

public static ExtensionCoordinate valueOf(Class extPtClass, BizScenario bizScenario){
public static ExtensionCoordinate valueOf(Class<?> extPtClass, BizScenario bizScenario){
return new ExtensionCoordinate(extPtClass, bizScenario);
}

public ExtensionCoordinate(Class extPtClass, BizScenario bizScenario){
public ExtensionCoordinate(Class<?> extPtClass, BizScenario bizScenario){
this.extensionPointClass = extPtClass;
this.extensionPointName = extPtClass.getName();
this.bizScenario = bizScenario;
this.bizScenarioUniqueIdentity = bizScenario.getUniqueIdentity();
}

/**
* @param extensionPoint
* @param bizScenario
*/
public ExtensionCoordinate(String extensionPoint, String bizScenario){
this.extensionPointName = extensionPoint;
this.bizScenarioUniqueIdentity = bizScenario;
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -60,22 +56,36 @@ public int hashCode() {
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
if (this == obj) {
return true;
}

if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}

ExtensionCoordinate other = (ExtensionCoordinate) obj;
if (bizScenarioUniqueIdentity == null) {
if (other.bizScenarioUniqueIdentity != null) return false;
} else if (!bizScenarioUniqueIdentity.equals(other.bizScenarioUniqueIdentity)) return false;
if (other.bizScenarioUniqueIdentity != null) {
return false;
}
} else if (!bizScenarioUniqueIdentity.equals(other.bizScenarioUniqueIdentity)) {
return false;
}
if (extensionPointName == null) {
if (other.extensionPointName != null) return false;
} else if (!extensionPointName.equals(other.extensionPointName)) return false;
return true;
return other.extensionPointName == null;
} else {
return extensionPointName.equals(other.extensionPointName);
}
}

@Override
public String toString() {
return "ExtensionCoordinate [extensionPointName=" + extensionPointName + ", bizScenarioUniqueIdentity=" + bizScenarioUniqueIdentity + "]";
}

}