0
0
NestJSframework~10 mins

Logging interceptor in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging interceptor
Request received
Interceptor intercepts request
Log request details
Pass control to handler
Handler processes request
Interceptor intercepts response
Log response details
Send response back
The interceptor catches the request, logs info, lets the handler run, then logs the response before sending it back.
Execution Sample
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, tap } from 'rxjs';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    console.log('Before handler');
    return next.handle().pipe(tap(() => console.log('After handler')));
  }
}
This interceptor logs a message before and after the request handler runs.
Execution Table
StepActionLog OutputHandler CalledResponse Sent
1Request received by interceptor'Before handler'NoNo
2Pass control to handlerNothingYesNo
3Handler processes requestNothingYesNo
4Response interceptedNothingYesNo
5Log after handler'After handler'YesNo
6Send response backNothingYesYes
💡 Response sent back after logging post-handler message
Variable Tracker
VariableStartAfter Step 1After Step 5Final
Log Output'''Before handler''After handler''After handler'
Handler Calledfalsefalsetruetrue
Response Sentfalsefalsefalsetrue
Key Moments - 2 Insights
Why do we see 'Before handler' logged before the handler runs?
Because the interceptor logs 'Before handler' immediately when intercept() is called, before passing control to the handler (see execution_table step 1).
Why is 'After handler' logged only after the handler finishes?
The interceptor uses RxJS tap operator on the handler's observable to log after the handler completes (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is logged at step 3?
A'Before handler'
BNothing
C'After handler'
D'Handler running'
💡 Hint
Check the 'Log Output' column at step 3 in the execution_table.
At which step is the handler actually called?
AStep 2
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Handler Called' column in execution_table to find when it changes to 'Yes'.
If we remove the tap operator, what changes in the logs?
A'Before handler' will not log
BBoth logs will still appear
C'After handler' will not log
DNo logs will appear
💡 Hint
The tap operator triggers 'After handler' log after handler finishes, see code in execution_sample.
Concept Snapshot
Logging Interceptor in NestJS:
- Implements NestInterceptor interface
- intercept() logs before passing control
- Uses next.handle() to call handler
- Uses RxJS tap() to log after handler completes
- Helps track request and response flow
Full Transcript
A logging interceptor in NestJS catches incoming requests before they reach the handler. It logs a message 'Before handler' immediately. Then it passes control to the handler using next.handle(). After the handler finishes processing, the interceptor logs 'After handler' using the RxJS tap operator on the observable returned by the handler. Finally, the response is sent back to the client. This flow helps developers see when requests start and finish processing.