A Logging Interceptor in NestJS is a special piece of code that runs before and after a request to log important information like request details and response time. It helps track what happens inside the app.
Click to reveal answer
beginner
How does a Logging Interceptor improve debugging?
It records details about requests and responses, such as method names, execution time, and errors. This makes it easier to find and fix problems by showing what happened step-by-step.
Click to reveal answer
intermediate
Which NestJS interface must a Logging Interceptor implement?
It must implement the NestInterceptor interface, which requires a method called intercept to handle the logging logic.
Click to reveal answer
intermediate
What method inside the Logging Interceptor captures the request and response?
The intercept method captures the request before it reaches the handler and the response after the handler finishes, allowing logging at both points.
Click to reveal answer
advanced
Why use RxJS operators like tap() inside a Logging Interceptor?
RxJS tap() lets you run code when the response is ready without changing the response itself. This is perfect for logging response details after the request completes.
Click to reveal answer
What is the main purpose of a Logging Interceptor in NestJS?
ATo handle database connections
BTo log request and response details
CTo manage user authentication
DTo serve static files
✗ Incorrect
Logging Interceptors are designed to log details about requests and responses to help with monitoring and debugging.
Which method must be implemented in a NestJS interceptor?
Aprocess()
Bhandle()
Cexecute()
Dintercept()
✗ Incorrect
The intercept() method is required by the NestInterceptor interface to define the interceptor's behavior.
What does the RxJS tap() operator do in a Logging Interceptor?
ARuns side effects like logging without changing data
BFilters out errors
CModifies the response data
DStops the request from continuing
✗ Incorrect
tap() allows running code such as logging when the response is ready, without altering the response stream.
Where in the request lifecycle does a Logging Interceptor run?
AOnly before the request handler
BOnly after the request handler
CBoth before and after the request handler
DOnly during server startup
✗ Incorrect
Logging Interceptors run before the handler to capture request info and after to capture response info.
Which NestJS decorator is used to apply an interceptor to a controller or route?
A@UseInterceptors()
B@Controller()
C@Get()
D@Injectable()
✗ Incorrect
@UseInterceptors() applies interceptors like Logging Interceptor to controllers or routes.
Explain how a Logging Interceptor works in NestJS and why it is useful.
Think about what happens when a request comes in and how logging helps.
You got /4 concepts.
Describe how RxJS tap() is used inside a Logging Interceptor.
Consider how to log data without affecting the flow.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a LoggingInterceptor in a NestJS application?
easy
A. To manage user authentication and authorization
B. To handle database connections automatically
C. To log details about incoming requests and outgoing responses
D. To serve static files like images and CSS
Solution
Step 1: Understand the role of interceptors
Interceptors in NestJS run code before and after request handlers, often used for logging or modifying requests/responses.
Step 2: Identify the purpose of LoggingInterceptor
A LoggingInterceptor specifically tracks and logs request and response details for debugging and monitoring.
Final Answer:
To log details about incoming requests and outgoing responses -> Option C
Quick Check:
LoggingInterceptor = logs requests/responses [OK]
Hint: LoggingInterceptor logs request and response info [OK]
Common Mistakes:
Confusing interceptors with middleware
Thinking interceptors handle authentication
Assuming interceptors serve static files
2. Which of the following is the correct way to implement the intercept method in a NestJS LoggingInterceptor?
easy
A. intercept(context, next) { return next.handle(); }
B. intercept(context, next) { next.handle(); }
C. intercept(context) { return next.handle(); }
D. intercept(next) { return context.handle(); }
Solution
Step 1: Recall the intercept method signature
The intercept method receives two parameters: context and next. It must return next.handle() to continue the request lifecycle.
Step 2: Check each option for correct syntax
intercept(context, next) { return next.handle(); } correctly uses both parameters and returns next.handle(). Options A, C, and D either miss parameters or do not return the observable.
Final Answer:
intercept(context, next) { return next.handle(); } -> Option A
Hint: intercept always returns next.handle() with both parameters [OK]
Common Mistakes:
Omitting return statement
Missing one of the parameters
Calling handle on wrong object
3. Given this LoggingInterceptor code snippet, what will be logged when a request is handled?
intercept(context, next) {
const now = Date.now();
return next.handle().pipe(
tap(() => console.log(`Request took ${Date.now() - now}ms`))
);
}
medium
A. Logs the time taken to process the request in milliseconds
B. Logs the request URL only
C. Logs the response body content
D. Logs an error message if the request fails
Solution
Step 1: Analyze the timing logic
The code records the current time before the request is handled, then logs the difference after the response is sent.
Step 2: Understand the logged message
The console.log outputs "Request took Xms" where X is the elapsed time in milliseconds.
Final Answer:
Logs the time taken to process the request in milliseconds -> Option A
Quick Check:
LoggingInterceptor measures request duration [OK]
Hint: Look for Date.now() difference to find elapsed time [OK]
Common Mistakes:
Assuming it logs request URL or body
Thinking it logs errors only
Ignoring the timing calculation
4. Identify the error in this LoggingInterceptor code:
intercept(context, next) {
const now = Date.now();
next.handle().pipe(
tap(() => console.log(`Request took ${Date.now() - now}ms`))
);
}
medium
A. tap operator is not imported
B. Incorrect use of Date.now()
C. Missing context parameter
D. Missing return statement before next.handle()
Solution
Step 1: Check the intercept method return
The intercept method must return the observable from next.handle() to continue the request lifecycle.
Step 2: Identify missing return
The code calls next.handle().pipe(...) but does not return it, so the request will not proceed properly.
Final Answer:
Missing return statement before next.handle() -> Option D
Quick Check:
intercept must return next.handle() observable [OK]
Hint: Always return next.handle() in intercept method [OK]
Common Mistakes:
Forgetting to return the observable
Misusing Date.now() for timing
Ignoring required parameters
5. You want to create a LoggingInterceptor that logs the request method, URL, and the time taken for each request. Which code snippet correctly implements this behavior?