-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathhttp-logger.interceptor.ts
More file actions
executable file
·30 lines (22 loc) · 1.01 KB
/
Copy pathhttp-logger.interceptor.ts
File metadata and controls
executable file
·30 lines (22 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @see https://github.com/mikemajesty/nestjs-microservice-boilerplate-api/blob/master/guides/middlewares/http-logger.interceptor.md
*/
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'
import { Observable } from 'rxjs'
import { ILoggerAdapter } from '@/infra/logger'
import { IDGeneratorUtils } from '@/utils/id-generator'
@Injectable()
export class HttpLoggerInterceptor implements NestInterceptor {
constructor(private readonly logger: ILoggerAdapter) {}
intercept(executionContext: ExecutionContext, next: CallHandler): Observable<unknown> {
const context = `${executionContext.getClass().name}/${executionContext.getHandler().name}`
const request = executionContext.switchToHttp().getRequest()
request['context'] = context
if (!request.headers?.traceid) {
request.headers.traceid = IDGeneratorUtils.uuid()
request.id = request.headers.traceid
}
this.logger.setGlobalParameters({ traceid: request.id })
return next.handle()
}
}