-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathlogging.interceptor.ts
executable file
·28 lines (25 loc) · 1 KB
/
logging.interceptor.ts
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
/**
* @file Dev logging interceptor
* @module interceptor/logging
* @author Surmon <https://github.com/surmon-china>
*/
import { Request } from 'express'
import { Observable } from 'rxjs'
import { tap } from 'rxjs/operators'
import { Injectable, NestInterceptor, CallHandler, ExecutionContext } from '@nestjs/common'
import { isDevEnv } from '@app/app.environment'
import { createLogger } from '@app/utils/logger'
const logger = createLogger({ scope: 'LoggingInterceptor', time: isDevEnv })
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
if (!isDevEnv) {
return next.handle()
}
const request = context.switchToHttp().getRequest<Request>()
const content = request.method.padStart(6, '_') + ' -> ' + request.url
logger.debug('+++ REQ:', content)
const now = Date.now()
return next.handle().pipe(tap(() => logger.debug('--- RES:', content, '|', `${Date.now() - now}ms`)))
}
}