Bird
Raised Fist0
NestJSframework~20 mins

Logging exceptions in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of logging exceptions in a NestJS application?
easy
A. To increase the app's memory usage
B. To speed up the application runtime
C. To track and fix errors by recording error details
D. To prevent users from accessing the app

Solution

  1. Step 1: Understand the role of exception logging

    Logging exceptions records error details like messages and stack traces to help developers find and fix bugs.
  2. Step 2: Identify the correct purpose

    Among the options, only tracking and fixing errors matches the purpose of logging exceptions.
  3. Final Answer:

    To track and fix errors by recording error details -> Option C
  4. Quick Check:

    Logging exceptions = track and fix errors [OK]
Hint: Logging exceptions helps find bugs by saving error info [OK]
Common Mistakes:
  • Thinking logging improves app speed
  • Confusing logging with security features
  • Believing logging increases memory unnecessarily
2. Which of the following is the correct way to log an error with NestJS Logger inside a catch block?
easy
A. Logger.error(error.message, error.stack, 'MyContext')
B. Logger.log(error.message, 'MyContext')
C. Logger.warn(error.stack, 'MyContext')
D. Logger.debug(error.message, error.stack)

Solution

  1. Step 1: Recall Logger.error method signature

    Logger.error takes three arguments: message, stack trace, and optional context string.
  2. Step 2: Match the correct syntax

    Logger.error(error.message, error.stack, 'MyContext') correctly uses Logger.error with message, stack, and context. Others use wrong methods or missing parameters.
  3. Final Answer:

    Logger.error(error.message, error.stack, 'MyContext') -> Option A
  4. Quick Check:

    Logger.error(message, stack, context) = Logger.error(error.message, error.stack, 'MyContext') [OK]
Hint: Use Logger.error with message, stack, and context [OK]
Common Mistakes:
  • Using Logger.log or Logger.warn instead of Logger.error
  • Omitting the stack trace argument
  • Passing arguments in wrong order
3. Given this NestJS code snippet inside a service method:
try {
  throw new Error('Failed operation');
} catch (error) {
  Logger.error(error.message, error.stack, 'MyService');
}

What will be logged?
medium
A. An info log with stack trace only
B. A warning log with message 'Failed operation' only
C. No log will be created because throw stops execution
D. An error log with message 'Failed operation' and stack trace under 'MyService' context

Solution

  1. Step 1: Understand the try-catch behavior

    The error is thrown and immediately caught by catch block, so execution continues inside catch.
  2. Step 2: Analyze Logger.error call

    Logger.error logs an error message with stack trace and context 'MyService'. This creates an error-level log entry.
  3. Final Answer:

    An error log with message 'Failed operation' and stack trace under 'MyService' context -> Option D
  4. Quick Check:

    Throw caught and logged as error with message and stack [OK]
Hint: Catch errors to log message and stack with Logger.error [OK]
Common Mistakes:
  • Thinking throw stops all logging
  • Confusing log levels (warn vs error)
  • Ignoring the context parameter
4. Identify the error in this NestJS exception logging code:
try {
  // some code
} catch (error) {
  Logger.error(error.stack, error.message, 'AppModule');
}
medium
A. Arguments to Logger.error are reversed; message should come first
B. Logger.error cannot be used inside catch blocks
C. Missing await keyword before Logger.error
D. Logger.error requires only one argument

Solution

  1. Step 1: Check Logger.error argument order

    Logger.error expects (message, stack, context). Here, stack and message are swapped.
  2. Step 2: Confirm correct usage

    Swapping arguments causes wrong log output or errors. Correct order is message first, then stack trace.
  3. Final Answer:

    Arguments to Logger.error are reversed; message should come first -> Option A
  4. Quick Check:

    Logger.error(message, stack, context) order matters [OK]
Hint: Logger.error needs message first, then stack, then context [OK]
Common Mistakes:
  • Swapping message and stack arguments
  • Thinking Logger.error is async needing await
  • Assuming Logger.error takes only one argument
5. You want to log exceptions in a NestJS global exception filter to capture all errors with context 'GlobalFilter'. Which code snippet correctly implements this?
hard
A. catch(exception: any) { Logger.log(exception.message, 'GlobalFilter'); }
B. catch(exception: any) { Logger.error(exception.message, exception.stack, 'GlobalFilter'); }
C. catch(exception: any) { Logger.warn(exception.stack, 'GlobalFilter'); }
D. catch(exception: any) { Logger.debug(exception.message, exception.stack); }

Solution

  1. Step 1: Understand global exception filter logging

    Global filters catch all exceptions; logging must use Logger.error with message, stack, and context.
  2. Step 2: Evaluate each option

    catch(exception: any) { Logger.error(exception.message, exception.stack, 'GlobalFilter'); } correctly calls Logger.error with message, stack, and 'GlobalFilter' context. Others use wrong log levels or miss stack/context.
  3. Final Answer:

    catch(exception: any) { Logger.error(exception.message, exception.stack, 'GlobalFilter'); } -> Option B
  4. Quick Check:

    Global filter logs errors with Logger.error and context [OK]
Hint: Use Logger.error with message, stack, and context in global filters [OK]
Common Mistakes:
  • Using Logger.log or Logger.warn instead of Logger.error
  • Omitting stack trace or context
  • Using Logger.debug which is too low level
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard