Bird
0
0

How can you enhance a LoggingInterceptor to log the HTTP method and URL of each request along with processing time?

hard📝 Workflow Q8 of 15
NestJS - Interceptors
How can you enhance a LoggingInterceptor to log the HTTP method and URL of each request along with processing time?
AExtract request from ExecutionContext using <code>context.switchToHttp().getRequest()</code> and log method and url
BUse <code>next.handle()</code> to get method and URL directly
CInject HttpService and call it to get request details
DOverride the controller methods to add logging manually
Step-by-Step Solution
Solution:
  1. Step 1: Access HTTP request

    Use context.switchToHttp().getRequest() to get the request object.
  2. Step 2: Extract method and URL

    From the request object, access method and url properties.
  3. Step 3: Log details with timing

    Combine method, URL, and duration in the log message inside the tap operator.
  4. Final Answer:

    Extract request from ExecutionContext using context.switchToHttp().getRequest() and log method and url -> Option A
  5. Quick Check:

    Only Extract request from ExecutionContext using context.switchToHttp().getRequest() and log method and url correctly accesses request info in interceptor. [OK]
Quick Trick: Use context.switchToHttp().getRequest() for HTTP info [OK]
Common Mistakes:
  • Trying to get request info from next.handle()
  • Manually adding logging in controllers instead of interceptor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes