0
0
Node.jsframework~20 mins

Logging with structured formats in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structured Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of JSON structured log with winston
What is the output of this Node.js code using winston logger configured for JSON format?
Node.js
import winston from 'winston';

const logger = winston.createLogger({
  format: winston.format.json(),
  transports: [new winston.transports.Console()]
});

logger.info('User login', { userId: 123, success: true });
Ainfo: User login { userId: 123, success: true }
B{"level":"info","message":"User login","userId":123,"success":true}
C{"message":"User login","level":"info"}
DUser login - userId: 123, success: true
Attempts:
2 left
💡 Hint
The winston JSON format outputs a single JSON object with all properties at top level.
🧠 Conceptual
intermediate
1:30remaining
Purpose of structured logging
Why is structured logging preferred over plain text logging in modern applications?
AIt allows logs to be easily parsed and queried by log management tools.
BIt automatically encrypts logs for security.
CIt reduces the size of log files by compressing text.
DIt prevents logs from being written to disk.
Attempts:
2 left
💡 Hint
Think about how logs are used in monitoring and debugging.
Configuration
advanced
2:30remaining
Correct JSON logger configuration in Node.js
Which configuration correctly sets up a pino logger to output pretty-printed JSON logs to the console?
A
import pino from 'pino';
const logger = pino({ prettyPrint: true });
logger.info('Server started');
B
import pino from 'pino';
const logger = pino({ format: 'json' });
logger.info('Server started');
C
import pino from 'pino';
const logger = pino({ json: true });
logger.info('Server started');
D
import pino from 'pino';
const logger = pino({ prettyPrint: { colorize: true } });
logger.info('Server started');
Attempts:
2 left
💡 Hint
Check pino's documentation for prettyPrint options.
Troubleshoot
advanced
2:00remaining
Why does this structured log miss fields?
Given this code snippet, why does the output JSON log miss the 'userId' field? const logger = winston.createLogger({ format: winston.format.json(), transports: [new winston.transports.Console()] }); logger.info('User login', { userId: 123 });
ABecause winston's json format ignores metadata unless combined with splat format.
BBecause the userId field is not a string and gets dropped.
CBecause the Console transport does not support JSON format.
DBecause the info method only accepts one argument.
Attempts:
2 left
💡 Hint
Check how winston handles extra metadata in JSON format.
🔀 Workflow
expert
3:00remaining
Order of middleware for structured logging in Express
In an Express.js app, you want to log each request with structured JSON format including method, url, and response time. Which middleware order is correct?
A1,3,2,4
B2,1,3,4
C1,2,3,4
D2,3,1,4
Attempts:
2 left
💡 Hint
Think about parsing JSON body before logging and measuring response time after request starts.