0
0
NestJSframework~20 mins

Logging with Winston or Pino in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logging Mastery with Winston and Pino
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Winston logger configuration?

Given this NestJS Winston logger setup, what will be logged when calling logger.info('Server started')?

NestJS
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

const logger = WinstonModule.createLogger({
  transports: [
    new winston.transports.Console({
      level: 'info',
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.printf(({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`)
      )
    })
  ]
});

logger.info('Server started');
Ainfo: Server started
B2024-06-01T12:00:00.000Z info: Server started
CServer started
DError: logger.info is not a function
Attempts:
2 left
💡 Hint

Look at the format used in the Console transport.

Configuration
intermediate
2:00remaining
Which Pino configuration enables pretty printing in NestJS?

Choose the correct Pino logger configuration snippet to enable pretty printing (human-readable logs) in a NestJS application.

A
import pino from 'pino';
const logger = pino({ format: 'pretty' });
B
import pino from 'pino';
const logger = pino({ prettyPrint: true });
C
import pino from 'pino';
const logger = pino({ transport: { target: 'pino-pretty' } });
D
import pino from 'pino';
const logger = pino({ pretty: true });
Attempts:
2 left
💡 Hint

Check the latest Pino documentation for pretty printing options.

Troubleshoot
advanced
2:00remaining
Why does this Winston logger not output any logs?

Consider this Winston logger setup in NestJS. Why does calling logger.info('Failure') produce no output?

NestJS
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

const logger = WinstonModule.createLogger({
  transports: [
    new winston.transports.Console({ level: 'warn' })
  ]
});

logger.info('Failure');
ABecause the transport level is set to 'warn', and 'info' is lower priority so it does not log.
BBecause the transport level is set to 'warn', and 'info' is a higher priority so it should log.
CBecause the transport level is set to 'warn', but the logger instance is not properly created.
DBecause the transport level is set to 'warn', and 'info' is higher priority so it logs.
Attempts:
2 left
💡 Hint

Remember the log level priorities: error > warn > info.

🔀 Workflow
advanced
2:00remaining
What is the correct order to integrate Pino logger in a NestJS app?

Arrange these steps in the correct order to add Pino logging to a NestJS application.

A2,1,3,4
B3,1,2,4
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint

Think about installation before usage.

Best Practice
expert
2:00remaining
Which practice improves performance when using Pino in production?

Choose the best practice to improve logging performance in a production NestJS app using Pino.

AUse asynchronous logging with a separate process or transport for log writing
BDisable logging completely in production to avoid overhead
CUse synchronous logging to ensure all logs are written immediately
DLog all debug and trace level messages in production for full details
Attempts:
2 left
💡 Hint

Think about how to reduce blocking operations in production.