Created
July 4, 2025 13:51
-
-
Save raxetul/1d0d6507ee3e01929d1839d7f939d40e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
CallHandler, | |
ExecutionContext, | |
Injectable, | |
Logger, | |
NestInterceptor | |
} from '@nestjs/common'; | |
import { Observable, tap } from 'rxjs'; | |
@Injectable() | |
export class AccessLoggerInterceptor implements NestInterceptor { | |
private readonly logger = new Logger(AccessLoggerInterceptor.name); | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
const now = Date.now(); | |
const path = context.getArgs()[0].url; | |
const method = context.getArgs()[0].method; | |
const statusCode = context.getArgs()[1].statusCode; | |
this.logger.log(`Request: ${method} | ${path}`); | |
return next.handle().pipe( | |
tap(() => { | |
this.logger.log( | |
`Response: ${method} | ${path} | Status:${statusCode} | Duration: ${Date.now() - now}ms`, | |
); | |
}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment