로깅 모듈 개선
This commit is contained in:
@@ -5,12 +5,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Aspect
|
||||
@@ -25,13 +21,11 @@ public class LogAspect {
|
||||
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
TraceStatusVO status = null;
|
||||
boolean hasException = false;
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
String clientIP = IpGetHelper.getClientIP(request);
|
||||
try {
|
||||
status = logTracer.begin(joinPoint.getSignature().toString(), Arrays.deepToString(joinPoint.getArgs()), clientIP);
|
||||
status = logTracer.begin("Return:"+joinPoint.getSignature().toString(), Arrays.deepToString(joinPoint.getArgs()));
|
||||
return joinPoint.proceed();
|
||||
} catch (Exception ex) {
|
||||
logTracer.exception(status, ex);
|
||||
logTracer.handleException(status, ex);
|
||||
hasException = true;
|
||||
throw ex;
|
||||
} finally {
|
||||
|
||||
@@ -2,6 +2,10 @@ package myblog.blog.base.log;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -13,17 +17,22 @@ public class LogTracer {
|
||||
|
||||
private ThreadLocal<TraceId> traceIdHolder = new ThreadLocal<>();
|
||||
|
||||
public TraceStatusVO begin(String message, String args, String clientIP){
|
||||
syncTraceId(clientIP);
|
||||
public TraceStatusVO begin(String message, String args){
|
||||
syncTraceId();
|
||||
TraceId traceId = traceIdHolder.get();
|
||||
Long startTimeMs = System.currentTimeMillis();
|
||||
if(traceId.isFirstLevel()) {
|
||||
log.info("------------------------------"+traceId.getId()+"'s transaction start------------------------------");
|
||||
}
|
||||
log.info("[{}] {}{} ,args = {}",traceId.getId(), addSpace(START_PREFIX, traceId.getLevel()), message, args);
|
||||
return new TraceStatusVO(traceId, startTimeMs, message);
|
||||
}
|
||||
|
||||
private void syncTraceId(String clientIP) {
|
||||
private void syncTraceId() {
|
||||
TraceId traceId = traceIdHolder.get();
|
||||
if (traceId == null) {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
String clientIP = IpGetHelper.getClientIP(request);
|
||||
traceIdHolder.set(new TraceId(clientIP));
|
||||
} else {
|
||||
traceIdHolder.set(traceId.createNextId());
|
||||
@@ -34,7 +43,7 @@ public class LogTracer {
|
||||
complete(traceStatusVO, null);
|
||||
}
|
||||
|
||||
public void exception(TraceStatusVO traceStatusVO, Exception ex){
|
||||
public void handleException(TraceStatusVO traceStatusVO, Exception ex){
|
||||
complete(traceStatusVO, ex);
|
||||
}
|
||||
|
||||
@@ -43,12 +52,15 @@ public class LogTracer {
|
||||
Long resultTimeMs = stopTimeMs - traceStatusVO.getStartTimesMs();
|
||||
TraceId traceId = traceStatusVO.getTraceId();
|
||||
if(ex == null){
|
||||
log.info("[{}] {} {} time = {}ms", traceId.getId(), addSpace(COMPLETE_PREFIX, traceId.getLevel()),
|
||||
log.info("[{}] {}{} time = {}ms", traceId.getId(), addSpace(COMPLETE_PREFIX, traceId.getLevel()),
|
||||
traceStatusVO.getMessage(), resultTimeMs);
|
||||
} else {
|
||||
log.info("[{}] {} {} time = {}ms ex={}", traceId.getId(), addSpace(EX_PREFIX, traceId.getLevel()),
|
||||
traceStatusVO.getMessage(), resultTimeMs, ex.toString());
|
||||
}
|
||||
}
|
||||
if(traceStatusVO.getTraceId().isFirstLevel()) {
|
||||
log.info("-------------------------------"+traceId.getId()+"'s transaction end/"+resultTimeMs+"ms-------------------------");
|
||||
}
|
||||
releaseTraceId();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ package myblog.blog.base.log;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
public class TraceId {
|
||||
private final String id;
|
||||
private final int level;
|
||||
|
||||
public TraceId(String clientIP) {
|
||||
this.id = clientIP;
|
||||
public TraceId(String id) {
|
||||
this.id = id +"/"+ createdTransactionId();
|
||||
this.level = 0;
|
||||
}
|
||||
|
||||
@@ -17,9 +19,9 @@ public class TraceId {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
// private String createdId() {
|
||||
// return UUID.randomUUID().toString().substring(0,8);
|
||||
// }
|
||||
private String createdTransactionId() {
|
||||
return UUID.randomUUID().toString().substring(0,8);
|
||||
}
|
||||
|
||||
public TraceId createNextId(){
|
||||
return new TraceId(id, level+1);
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package myblog.blog.base.log;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class TraceStatus {
|
||||
private TraceId traceId;
|
||||
private Long startTimesMs;
|
||||
private String message;
|
||||
}
|
||||
Reference in New Issue
Block a user