Skip to content

Commit

Permalink
[Refactor] logging 방식
Browse files Browse the repository at this point in the history
  • Loading branch information
nahyeon99 committed Aug 19, 2023
1 parent eb3268f commit f070cbe
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 112 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ LOGGING_ROLLING_FILE_PATH_IS_UNDEFINED
LOGGING_ERROR_FILE_PATH_IS_UNDEFINED
idorm-rolling.log

### Not Useed
src/main/**/master

src/main/**/util
src/main/**/util

src/main/resources/application-local.yml
src/main/resources/application-dev.yml
src/main/resources/application-prod.yml
131 changes: 131 additions & 0 deletions src/main/java/idorm/idormServer/aop/AopLogging.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package idorm.idormServer.aop;

import idorm.idormServer.exception.CustomException;
import io.sentry.Sentry;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.lang.reflect.Method;

import static idorm.idormServer.exception.ExceptionCode.*;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

@Slf4j
@Aspect
@Component
public class AopLogging {

@Pointcut("execution(* idorm.idormServer..*Controller.*(..))")
private void methodFromController(){}

@Pointcut("execution(public * idorm.idormServer..*(..))")
private void all() {
}

@Before("all()")
public void logBeforeStart(JoinPoint joinPoint) {

try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();

String className = signature.getDeclaringType().getSimpleName();
Method method = signature.getMethod();

String[] parameterNames = signature.getParameterNames();
Object[] args = joinPoint.getArgs();

String returnArgs = null;

for (int i = 0; i < method.getParameters().length; i++) {
if (parameterNames[i] != null) {
returnArgs += " | " + parameterNames[i] + " = ";
returnArgs += args[i] + " ";
}
}
log.info("[START] {} | {} {}", className, method.getName(), returnArgs);
} catch (Exception e) {
throw new CustomException(e, AOP_LOGGING_ERROR);
}
}

@AfterReturning(value = "all()", returning = "result")
public void logAfterSuccess(JoinPoint joinPoint, Object result) {

try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();

String className = signature.getDeclaringType().getSimpleName();
Method method = signature.getMethod();

log.info("[SUCCESS] {} | {} | return = {}", className, method.getName(), result);
} catch (Exception e) {
throw new CustomException(e, AOP_LOGGING_ERROR);
}
}

@Around("methodFromController()")
public Object logEndpoint(ProceedingJoinPoint joinPoint) throws Throwable {

MethodSignature signature = (MethodSignature) joinPoint.getSignature();

String className = signature.getDeclaringType().getSimpleName();
Method method = signature.getMethod();

log.info("========= [API START] {} {} API =========", className, method.getName());
StopWatch stopWatch = new StopWatch();

stopWatch.start();
Object proceed = joinPoint.proceed();
stopWatch.stop();

long totalTimeMillis = stopWatch.getTotalTimeMillis();

log.info("========= [API FINISH] {} {} API | {} ms =========", className, method.getName(), totalTimeMillis);
return proceed;
}

@AfterThrowing(value = "all()", throwing = "exception")
public void logAfterThrowing(JoinPoint joinPoint, CustomException exception) {

try {

MethodSignature signature = (MethodSignature) joinPoint.getSignature();

String className = signature.getDeclaringType().getSimpleName();
Method method = signature.getMethod();

String[] parameterNames = signature.getParameterNames();
Object[] args = joinPoint.getArgs();

String returnArgs = null;

for (int i = 0; i < method.getParameters().length; i++) {
if (parameterNames[i] != null) {
returnArgs += " | " + parameterNames[i] + " = ";
returnArgs += args[i] + " ";
}
}

log.error("[ERROR] {} | {} | throwing = {} | reqArgs = {}",
className,
method.getName(),
exception.getExceptionCode().name(),
returnArgs);

if (exception.getExceptionCode().getHttpStatus().equals(INTERNAL_SERVER_ERROR)) {

if (exception.getException() != null) {
Sentry.captureException(exception);
log.error("##### SERVER ERROR DESCRIPTION #####", exception.getException());
}
}
} catch (Exception e) {
throw new CustomException(e, AOP_LOGGING_ERROR);
}
}
}
91 changes: 0 additions & 91 deletions src/main/java/idorm/idormServer/aspect/AopLogging.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EmailService {
private final JavaMailSender javaMailSender;
private final EmailRepository emailRepository;

@Value("${MAIL_USERNAME}")
@Value("${spring.mail.username}")
private String adminMail;

@Value("${s3.logo}")
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/idorm/idormServer/exception/ExceptionCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public enum ExceptionCode {
SERVER_ERROR(INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다."),
EMAIL_SENDING_ERROR(INTERNAL_SERVER_ERROR, "이메일 전송 중에 서버 에러가 발생했습니다."),
FIREBASE_SERER_ERROR(INTERNAL_SERVER_ERROR, "푸시 알람 전송 중에 서버 에러가 발생했습니다."),
S3_SERVER_ERROR(INTERNAL_SERVER_ERROR, "S3 사진 저장 중에 서버 에러가 발생했습니다.");
S3_SERVER_ERROR(INTERNAL_SERVER_ERROR, "S3 사진 저장 중에 서버 에러가 발생했습니다."),
AOP_LOGGING_ERROR(INTERNAL_SERVER_ERROR, "로깅 중 서버 에러가 발생했습니다.")
;

private final HttpStatus httpStatus;
private final String message;
Expand Down
34 changes: 17 additions & 17 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ spring:
properties:
hibernate:
show_sql: false
format_sql: false
highlight_sql: false
use_sql_comments: false

jackson:
serialization:
fail-on-empty-beans: false
format_sql: true
highlight_sql: true
use_sql_comments: true

mvc:
pathmatch:
path match:
matching-strategy: ant_path_matcher

servlet:
multipart:
max-file-size: ${MAX_FILE_SIZE}
max-request-size: ${MAX_REQUEST_SIZE}
enabled: true

mail:
host: ${MAIL_HOST}
port: ${MAIL_PORT}
Expand All @@ -33,15 +35,9 @@ spring:
properties:
mail:
smtp:
auth: ${MAIL_AUTH}
auth: false
starttls:
enable: ${MAIL_TLS}

servlet:
multipart:
max-file-size: ${MAX_FILE_SIZE}
max-request-size: ${MAX_REQUEST_SIZE}
enabled: true
enable: false

jwt:
secret: ${JWT_SECRET_KEY}
Expand Down Expand Up @@ -73,4 +69,8 @@ firebase:
logging:
file:
path: ${LOGGING_ROLLING_FILE_PATH}
name: idorm-rolling.log
name: idorm-rolling.log
level:
org:
springframework.web: info
hibernate: info

0 comments on commit f070cbe

Please sign in to comment.