Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#564 [feat] sql 로깅 모듈 구현 #568

Merged
merged 1 commit into from
Nov 3, 2024
Merged

#564 [feat] sql 로깅 모듈 구현 #568

merged 1 commit into from
Nov 3, 2024

Conversation

sohyundoh
Copy link
Member

@sohyundoh sohyundoh commented Nov 3, 2024

✒️ 관련 이슈번호

Key Changes 🔑

  1. p6spy로 sql 문의 인자 포함된 로깅을 제공하고, 이를 포맷한 후 진입점을 로깅할 수 있게 AOP 방식으로 repository 내부 메서드 호출 시 MDC에 진입점을 담도록 진행했습니다. 그리고 이를 logging formatter에서 꺼내오도록했습니다.
  2. 이 때 MDC.get 값은 경우는 null 인 경우를 보장해주어서 NPE 나는 경우를 걱정하지 않아도 될 것이라고 판단했습니다.
    image
    결과는 아래와 같습니다!
    image

To Reviewers 📢

@sohyundoh sohyundoh self-assigned this Nov 3, 2024
Copy link
Contributor

@parkheeddong parkheeddong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AOP를 사용해서 진입점 로그를 기록하는 게 좋은 것 같습니다! 유용한 라이브러리를 알아가네요~ 적용해 주셔서 감사합니다!

Comment on lines +24 to +28
if (tmpsql.startsWith("create") || tmpsql.startsWith("alter") || tmpsql.startsWith("comment")) {
sql = FormatStyle.DDL.getFormatter().format(sql);
} else {
sql = FormatStyle.BASIC.getFormatter().format(sql);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DDL과 일반 쿼리를 나눠 포맷팅 해서 더 좋은것 같습니다!

public void sqlLoggingPoint() {
}

@Around("sqlLoggingPoint()")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 around로 쿼리 실행될 때 시작지점 기록을 하는거군요 👍

Comment on lines +31 to +34
MDC.put(MDC_KEY, "[ QUERY START -> " + joinPoint.getSignature().toShortString() + "]");

return joinPoint.proceed();
}
Copy link
Contributor

@parkheeddong parkheeddong Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5) 찾아보니 mdc는 스레드로컬 방식으로 관리되는데 aop로 sql 실행시점에 mdc 에 데이터를 넣고 그대로 두면 그 스레드가 이후 다른작업을 할때도 같은 값을 사용할 수 있어서 메서드가 끝나면 mdc.remove나 clear를 하는 게 좋다고 합니다! try-finally 사용해서 메서드 실행 후, finally { MDC.remove(MDC_KEY); } 해주는건 어떻게 생각하시나요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

옷 아주 좋습니다! request ID 담고 있는 부분도 포함해서 수정하겠습니다!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호라 request Id 부분에는 제가 이미 mdc.clear()을 구현해놨네요! clear()을 통해 로컬 스레드 맵을 지우는 방식으로 진행하겠습니다!
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니댜!!

Copy link
Member Author

@sohyundoh sohyundoh Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

실제로 디버깅 해보니 Interceptor에 clear해주기 때문에 AOP에서도 clear하진 않아도 될 것 같습니다! clear해주는 ThreadLocalMap이 모두 같은 객체더라고요! 같이 알면 좋을 것 같아 남깁니다! 앞에 인터셉터에서 해주길 잘했네요 ㅎㅎ
image

@sohyundoh sohyundoh merged commit e6b328e into develop Nov 3, 2024
1 check passed
@sohyundoh sohyundoh deleted the feat/#564 branch November 5, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] sql 도입점 로깅 (InjectSqlRepository)
2 participants