-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: controller 진입점 로그 추가 * feat: controller 진입점 로그 추가
- Loading branch information
Showing
7 changed files
with
181 additions
and
7 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...alettee/global/aop/ConnectionHandler.java → ...l/aop/querycounter/ConnectionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...alettee/global/aop/PerformanceAspect.java → ...l/aop/querycounter/PerformanceAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
...com/palettee/global/aop/QueryCounter.java → ...global/aop/querycounter/QueryCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/palettee/global/aop/tracer/LogAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.palettee.global.aop.tracer; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
|
||
@Aspect | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class LogAspect { | ||
|
||
private final LogTracer logTracer; | ||
|
||
@Pointcut("execution(* com.palettee..*Controller.*(..))") | ||
public void everyRequest() { } | ||
|
||
@Around("everyRequest()") | ||
public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable { | ||
TraceStatus status = null; | ||
boolean hasException = false; | ||
try { | ||
status = logTracer.begin(" Method : " + getKeySignature(joinPoint), | ||
Arrays.deepToString(joinPoint.getArgs())); | ||
return joinPoint.proceed(); | ||
} catch (Exception ex) { | ||
logTracer.handleException(status, ex); | ||
hasException = true; | ||
throw ex; | ||
} finally { | ||
if(!hasException) logTracer.end(status); | ||
} | ||
} | ||
|
||
private String getKeySignature(ProceedingJoinPoint joinPoint) { | ||
String[] split = joinPoint.getSignature().toString().split("\\."); | ||
int length = split.length; | ||
String[] arr = Arrays.copyOfRange(split, length-3, length); | ||
return arr[1] + "." + arr[2]; | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/palettee/global/aop/tracer/LogTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.palettee.global.aop.tracer; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
public class LogTracer { | ||
|
||
private static final String PREFIX = "-->"; | ||
private static final String SUFFIX = "<--"; | ||
private static final String ERR_FIX = "<X-"; | ||
|
||
private final ThreadLocal<TraceId> traceIdHolder = new ThreadLocal<>(); | ||
|
||
public TraceStatus begin(String message, String args){ | ||
syncTraceId(); | ||
TraceId traceId = traceIdHolder.get(); | ||
Long startTimeMs = System.currentTimeMillis(); | ||
if(traceId.isFirstLevel()) { | ||
log.info("--------------------- [TraceId: {}] start ---------------------", | ||
traceId.getId()); | ||
} | ||
log.info("[{}] {}{}, args = {}",traceId.getId(), addSpace(PREFIX, traceId.getLevel()), message, args); | ||
return new TraceStatus(traceId, startTimeMs, message); | ||
} | ||
|
||
private void syncTraceId() { | ||
TraceId traceId = traceIdHolder.get(); | ||
if (traceId == null) { | ||
traceIdHolder.set(new TraceId()); | ||
} else { | ||
traceIdHolder.set(traceId.createNextId()); | ||
} | ||
} | ||
|
||
public void end(TraceStatus traceStatus){ | ||
complete(traceStatus, null); | ||
} | ||
|
||
public void handleException(TraceStatus traceStatus, Exception ex){ | ||
complete(traceStatus, ex); | ||
} | ||
|
||
private void complete(TraceStatus traceStatus, Exception ex) { | ||
Long stopTimeMs = System.currentTimeMillis(); | ||
Long resultTimeMs = stopTimeMs - traceStatus.getStartTimesMs(); | ||
TraceId traceId = traceStatus.getTraceId(); | ||
if(ex == null){ | ||
log.info("[{}] {}{} time = {}ms", traceId.getId(), addSpace(SUFFIX, traceId.getLevel()), | ||
traceStatus.getMessage(), resultTimeMs); | ||
} else { | ||
log.info("[{}] {} {} time = {}ms ex={}", traceId.getId(), addSpace(ERR_FIX, traceId.getLevel()), | ||
traceStatus.getMessage(), resultTimeMs, ex.toString()); | ||
} | ||
if(traceStatus.getTraceId().isFirstLevel()) { | ||
log.info("--------------------- [TraceId: {}] end Time: {} ms ---------------------", | ||
traceId.getId(), resultTimeMs); | ||
} | ||
releaseTraceId(); | ||
} | ||
|
||
private void releaseTraceId() { | ||
TraceId traceId = traceIdHolder.get(); | ||
if (traceId.isFirstLevel()) { | ||
traceIdHolder.remove(); | ||
} else { | ||
traceIdHolder.set(traceId.createPrevId()); | ||
} | ||
} | ||
|
||
private String addSpace(String prefix, int level) { | ||
StringBuilder sb = new StringBuilder(); | ||
for(int i = 0; i< level; i++){ | ||
sb.append((i==level-1) ? "|" + prefix : "| "); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.palettee.global.aop.tracer; | ||
|
||
import java.util.UUID; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TraceId { | ||
private final String id; | ||
private final int level; | ||
|
||
public TraceId() { | ||
this.id = createdTransactionId(); | ||
this.level = 0; | ||
} | ||
|
||
private TraceId(String id, int level) { | ||
this.id = id; | ||
this.level = level; | ||
} | ||
|
||
private String createdTransactionId() { | ||
return UUID.randomUUID().toString().substring(0,8); | ||
} | ||
|
||
public TraceId createNextId(){ | ||
return new TraceId(id, level + 1); | ||
} | ||
|
||
public TraceId createPrevId(){ | ||
return new TraceId(id, level - 1); | ||
} | ||
|
||
public boolean isFirstLevel(){ | ||
return level == 0; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/palettee/global/aop/tracer/TraceStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.palettee.global.aop.tracer; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class TraceStatus { | ||
private TraceId traceId; | ||
private Long startTimesMs; | ||
private String message; | ||
} |