Skip to content
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
116 changes: 1 addition & 115 deletions src/main/java/doldol_server/doldol/common/config/LoggingAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.ContentCachingRequestWrapper;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,95 +26,22 @@
@RequiredArgsConstructor
public class LoggingAspect {
private final ObjectMapper objectMapper = new ObjectMapper();
private final MeterRegistry meterRegistry;

@Autowired
private Environment environment;

private static final String START_LOG = "================================================NEW===============================================\n";
private static final String START_LOG = "================================================ERROR===============================================\n";
private static final String END_LOG = "================================================END===============================================\n";

@Pointcut("execution(* doldol_server.doldol.*.controller..*(..)) || ( execution(* doldol_server.doldol.common.exception..*(..)) && !execution(* doldol_server.doldol.common.exception.GlobalExceptionHandler.handle*(..)))")
public void controllerInfoLevelExecute() {
}

@Pointcut("execution(* doldol_server.doldol.common.exception.GlobalExceptionHandler.handle*(..))")
public void controllerErrorLevelExecute() {
}

@Around("doldol_server.doldol.common.config.LoggingAspect.controllerInfoLevelExecute()")
public Object requestInfoLevelLogging(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
final ContentCachingRequestWrapper cachingRequest = (ContentCachingRequestWrapper) request;

Timer.Sample sample = Timer.start(meterRegistry);
long startAt = System.currentTimeMillis();

try {
Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
long endAt = System.currentTimeMillis();
long responseTime = endAt - startAt;

if (isProdProfile()) {
recordMetrics(request, startAt, endAt, "SUCCESS");

log.info("REQUEST_METRICS method={} uri={} response_time_ms={} status=SUCCESS timestamp={}",
request.getMethod(),
request.getRequestURI(),
responseTime,
startAt);
}

log.info(getCommunicationData(request, cachingRequest, startAt, endAt, returnValue, null));

return returnValue;
} catch (Exception e) {
long endAt = System.currentTimeMillis();
long responseTime = endAt - startAt;

if (isProdProfile()) {
recordMetrics(request, startAt, endAt, "ERROR");

log.error("ERROR_METRICS method={} uri={} response_time_ms={} status=ERROR timestamp={} error_class={} error_message=\"{}\"",
request.getMethod(),
request.getRequestURI(),
responseTime,
startAt,
e.getClass().getSimpleName(),
e.getMessage());
}

log.error(getCommunicationData(request, cachingRequest, startAt, endAt, null, e));

throw e;
} finally {
if (isProdProfile()) {
sample.stop(Timer.builder("http_request_duration_seconds")
.tag("method", request.getMethod())
.tag("uri", getSimplifiedUri(request.getRequestURI()))
.register(meterRegistry));
}
}
}

@Around("doldol_server.doldol.common.config.LoggingAspect.controllerErrorLevelExecute()")
public Object requestErrorLevelLogging(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
final ContentCachingRequestWrapper cachingRequest = (ContentCachingRequestWrapper) request;
long startAt = System.currentTimeMillis();

Object returnValue = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());

long endAt = System.currentTimeMillis();
long responseTime = endAt - startAt;

if (isProdProfile()) {
log.error("ERROR_METRICS method={} uri={} response_time_ms={} status=ERROR timestamp={} handler=GlobalExceptionHandler",
request.getMethod(),
request.getRequestURI(),
responseTime,
startAt);
}

log.error(getCommunicationData(request, cachingRequest, startAt, endAt, returnValue, null));
return returnValue;
Expand Down Expand Up @@ -182,40 +104,4 @@ private Map<String, Object> getHeaders(HttpServletRequest request) {
}
return headerMap;
}

private void recordMetrics(HttpServletRequest request, long startAt, long endAt, String status) {
String method = request.getMethod();
String uri = getSimplifiedUri(request.getRequestURI());

Counter.builder("http_requests_total")
.description("Total HTTP requests")
.tag("method", method)
.tag("uri", uri)
.tag("status", status)
.register(meterRegistry)
.increment();

Timer.builder("http_request_duration_seconds")
.description("HTTP request duration in seconds")
.tag("method", method)
.tag("uri", uri)
.tag("status", status)
.register(meterRegistry)
.record(endAt - startAt, java.util.concurrent.TimeUnit.MILLISECONDS);
}

private String getSimplifiedUri(String requestUri) {
return requestUri.replaceAll("/\\d+", "/{id}")
.replaceAll("/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", "/{uuid}");
}

private boolean isProdProfile() {
String[] activeProfiles = environment.getActiveProfiles();
for (String profile : activeProfiles) {
if ("prod".equals(profile)) {
return true;
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/config
24 changes: 0 additions & 24 deletions src/main/resources/file-info-appender.xml

This file was deleted.

12 changes: 2 additions & 10 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@
</springProfile>

<springProfile name="dev">
<include resource="file-info-appender.xml"/>
<include resource="file-error-appender.xml"/>

<root level="INFO">
<appender-ref ref="FILE-INFO"/>
<root level="WARN">
<appender-ref ref="FILE-ERROR"/>
</root>
</springProfile>

<springProfile name="prod">
<include resource="file-info-appender.xml"/>
<include resource="file-error-appender.xml"/>
<include resource="slack-prod-appender.xml"/>

<root level="INFO">
<appender-ref ref="FILE-INFO"/>
<root level="WARN">
<appender-ref ref="FILE-ERROR"/>
<appender-ref ref="ASYNC_SLACK_PROD"/>
</root>
</springProfile>
</configuration>
29 changes: 0 additions & 29 deletions src/main/resources/slack-prod-appender.xml

This file was deleted.

Loading