Skip to content

Commit

Permalink
feat(be): add user info and request id for logging (#1207)
Browse files Browse the repository at this point in the history
* feat: add user info to pino-http

http request log에 대해 인증된 사용자의 경우 user id와 username을 추가한다.

* feat: add request id to request and response
  • Loading branch information
SH9480P committed Jan 24, 2024
1 parent 9b336b7 commit 6ca2902
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/libs/logger/src/pino-option.logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { gray, italic, white } from 'colorette'
import { randomUUID } from 'crypto'
import type { Params } from 'nestjs-pino'
import PinoPretty from 'pino-pretty'
import type { PrettyOptions } from 'pino-pretty'
import { format } from 'sql-formatter'
import type { AuthenticatedRequest } from '@libs/auth'

const pinoPrettyOptions: PrettyOptions = {
messageFormat: (log, messageKey) => {
Expand Down Expand Up @@ -38,6 +40,22 @@ export const pinoLoggerModuleOption: Params = {
mergeObject = { ...mergeObject, msg: mergeObject.message }
}
return mergeObject
},
customProps(req: AuthenticatedRequest) {
return req.user
? {
user: {
id: req.user.id,
username: req.user.username
}
}
: { user: 'undefined' }
},
genReqId(req, res) {
// TODO: x-request-id를 reverse proxy에서 추가하는 경우, 아래 코드를 변경해야 함. 현재는 request id를 nest에서만 할당하는 것으로 가정.
const id = randomUUID()
res.setHeader('X-Request-Id', id)
return id
}
}
}

0 comments on commit 6ca2902

Please sign in to comment.