Skip to content

Commit

Permalink
#89 - 진입점 로깅 추가 (#90)
Browse files Browse the repository at this point in the history
* feat: controller 진입점 로그 추가

* feat: controller 진입점 로그 추가
  • Loading branch information
AnTaeho authored Dec 6, 2024
1 parent 2fabb2d commit 950b6ea
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.palettee.global.aop;
package com.palettee.global.aop.querycounter;

import lombok.extern.slf4j.Slf4j;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.palettee.global.aop;
package com.palettee.global.aop.querycounter;

import java.lang.reflect.*;
import java.sql.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.palettee.global.aop;
package com.palettee.global.aop.querycounter;

import lombok.Getter;

@Getter
public class QueryCounter {

private int count;
public void increase() {
count++;
}

public int getCount() {
return count;
}

public boolean isWarn() {
return count > 10;
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/palettee/global/aop/tracer/LogAspect.java
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 src/main/java/com/palettee/global/aop/tracer/LogTracer.java
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();
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/palettee/global/aop/tracer/TraceId.java
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 src/main/java/com/palettee/global/aop/tracer/TraceStatus.java
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;
}

0 comments on commit 950b6ea

Please sign in to comment.