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

Nestjs Logger에 request ID 및 User ID 추가하기 #1174

Closed
3 tasks done
SH9480P opened this issue Jan 10, 2024 · 0 comments · Fixed by #1207
Closed
3 tasks done

Nestjs Logger에 request ID 및 User ID 추가하기 #1174

SH9480P opened this issue Jan 10, 2024 · 0 comments · Fixed by #1207
Assignees
Labels
backlog 🌙 We will go through this later ⛳️ team-backend

Comments

@SH9480P
Copy link
Member

SH9480P commented Jan 10, 2024

Describe the problem and solution

Problem

현재 로그를 구분하는 식별자는 auto-increment하는 정수값으로 자동 삽입됩니다.
로그에 unique한 Request ID와 User ID가 있으면 Production 환경에서 로그를 분석할 때 편리할 것입니다.

Solution (request ID)

UUIDv4로 unique한 식별자를 생성할 수 있음. 들어오는 모든 요청에 대해 request ID를 들려 보내는 미들웨어를 만들어 볼 수 있음.
문제는 생성한 ID를 어떻게 전달하느냐.

  1. CLS (continuation-local storage)
    Node.js에서 Thread Local Storage의 대안. 여기에 request id를 집어넣고 가져다 쓸 수 있음. 그러나, prisma의 rust-based engine 관련 이슈로 context가 유지되지 않아 prisma에서는 CLS를 사용할 수 없음.
  2. request 객체에 X-Request-Id 헤더를 집어넣고, 사용할 때마다 request 객체에서 전달전달~

Solution (user ID)

JWT Auth Guard를 거치면서 req.user.id가 생성됨. 이 값을 logger에 전달하면 되는데, 어떻게 하지..?

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog 🌙 We will go through this later ⛳️ team-backend
Projects
Status: Done ✔️
Development

Successfully merging a pull request may close this issue.

1 participant