0
0
NestJSframework~20 mins

Logging interceptor in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logging Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of a simple NestJS logging interceptor
Given the following NestJS logging interceptor code, what will be logged when a controller method returns 'Hello World'?
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 execution');
    return next.handle().pipe(
      tap(() => console.log('After handler execution'))
    );
  }
}
ADoes not log anything
BLogs only 'Before handler execution' before the method
CLogs only 'After handler execution' after the method
DLogs 'Before handler execution' before the method and 'After handler execution' after the method
Attempts:
2 left
💡 Hint
Think about when the tap operator runs in an RxJS observable pipeline.
🧠 Conceptual
intermediate
1:00remaining
Purpose of a logging interceptor in NestJS
What is the main purpose of using a logging interceptor in a NestJS application?
ATo modify the response body before sending it to the client
BTo log details about incoming requests and outgoing responses centrally
CTo handle exceptions thrown by controller methods
DTo authenticate users before accessing routes
Attempts:
2 left
💡 Hint
Think about what interceptors can do with requests and responses.
Configuration
advanced
1:30remaining
Correct way to apply a global logging interceptor in NestJS
Which code snippet correctly applies a logging interceptor globally in a NestJS application?
NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggingInterceptor } from './logging.interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // Apply interceptor here
  await app.listen(3000);
}
bootstrap();
Aapp.useGlobalInterceptors(new LoggingInterceptor());
Bapp.useGlobalInterceptor(new LoggingInterceptor());
Capp.applyGlobalInterceptors(new LoggingInterceptor());
Dapp.useInterceptorGlobally(new LoggingInterceptor());
Attempts:
2 left
💡 Hint
Check the exact method name for applying global interceptors in NestJS.
Troubleshoot
advanced
2:00remaining
Why does the logging interceptor not log after handler execution?
A developer notices that their NestJS logging interceptor logs the message before the handler but never logs the message after the handler execution. What is the most likely cause?
AThe controller method returns a Promise instead of an Observable
BThe tap operator is missing in the interceptor's pipe
CThe interceptor forgot to return next.handle() observable
DThe interceptor is not decorated with @Injectable()
Attempts:
2 left
💡 Hint
Check the return statement in the intercept method.
🔀 Workflow
expert
2:30remaining
Order of execution in multiple NestJS interceptors
Given two global interceptors, LoggingInterceptor and TimingInterceptor, applied in this order: app.useGlobalInterceptors(new LoggingInterceptor(), new TimingInterceptor()); Which order will their 'before' and 'after' logs appear when a controller method is called?
ALoggingInterceptor before, TimingInterceptor before, controller runs, TimingInterceptor after, LoggingInterceptor after
BTimingInterceptor before, LoggingInterceptor before, controller runs, LoggingInterceptor after, TimingInterceptor after
CLoggingInterceptor before, TimingInterceptor before, controller runs, LoggingInterceptor after, TimingInterceptor after
DTimingInterceptor before, LoggingInterceptor before, controller runs, TimingInterceptor after, LoggingInterceptor after
Attempts:
2 left
💡 Hint
Think of interceptors like nested layers wrapping the controller method.