0
0
NestJSframework~20 mins

Logging exceptions in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when an exception is logged using NestJS Logger?

Consider this NestJS service method that throws an exception and logs it using the Logger service:

import { Logger } from '@nestjs/common';

const logger = new Logger('MyService');

function riskyOperation() {
  try {
    throw new Error('Something went wrong');
  } catch (error) {
    logger.error('Operation failed', error.stack);
  }
}

riskyOperation();

What will be the output in the console?

NestJS
import { Logger } from '@nestjs/common';

const logger = new Logger('MyService');

function riskyOperation() {
  try {
    throw new Error('Something went wrong');
  } catch (error) {
    logger.error('Operation failed', error.stack);
  }
}

riskyOperation();
A
[MyService] ERROR Operation failed
Error: Something went wrong
    at riskyOperation (...stack trace...)
B
[MyService] WARN Operation failed
Error: Something went wrong
    at riskyOperation (...stack trace...)
C
[MyService] LOG Operation failed
Error: Something went wrong
    at riskyOperation (...stack trace...)
D
[MyService] DEBUG Operation failed
Error: Something went wrong
    at riskyOperation (...stack trace...)
Attempts:
2 left
💡 Hint

Remember that logger.error logs messages with the ERROR level.

🧠 Conceptual
intermediate
1:30remaining
Which NestJS built-in class is best for catching and logging all exceptions globally?

In NestJS, you want to catch all exceptions thrown anywhere in your app and log them centrally. Which built-in class should you extend or use?

AInterceptor
BExceptionFilter
CPipe
DGuard
Attempts:
2 left
💡 Hint

Think about the class designed to handle exceptions.

Configuration
advanced
2:30remaining
How to configure NestJS Logger to write exceptions to a file?

You want to log exceptions to a file instead of the console in a NestJS app. Which approach is correct?

ACreate a custom LoggerService implementing LoggerService interface and override error() to write to a file.
BSet environment variable LOG_TO_FILE=true and NestJS will auto-log to a file.
CUse the built-in Logger with a config option <code>file: 'errors.log'</code> in main.ts.
DCall <code>Logger.error('message', { file: 'errors.log' })</code> to write to a file.
Attempts:
2 left
💡 Hint

Think about how to customize logging behavior in NestJS.

Troubleshoot
advanced
2:00remaining
Why does the stack trace not appear when logging exceptions with NestJS Logger?

You use logger.error('Failed', error.message) inside a catch block, but the stack trace is missing in logs. Why?

AThe error object is lost after the catch block ends.
BLogger.error automatically hides stack traces for security.
CBecause <code>error.message</code> is a string without stack info; you should pass <code>error.stack</code> instead.
DYou must enable stack trace logging in NestJS config file.
Attempts:
2 left
💡 Hint

Check what property contains the stack trace.

🔀 Workflow
expert
3:00remaining
Order the steps to implement global exception logging in NestJS

Put these steps in the correct order to set up global exception logging in a NestJS app.

A1,2,4,3
B4,1,2,3
C4,2,1,3
D1,4,2,3
Attempts:
2 left
💡 Hint

Think about class creation, dependency injection, method override, then registration.