Bird
Raised Fist0
Node.jsframework~20 mins

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

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

Practice

(1/5)
1. What is the main benefit of using structured logging in Node.js applications?
easy
A. Logs are stored as plain text without any format.
B. Logs are only saved in memory and not written to files.
C. Logs are encrypted automatically for security.
D. Logs are saved in a clear, organized JSON format for easy reading and processing.

Solution

  1. Step 1: Understand structured logging

    Structured logging means saving logs in a format like JSON that is easy to read and parse.
  2. Step 2: Identify the benefit

    This format helps both humans and tools quickly understand log data.
  3. Final Answer:

    Logs are saved in a clear, organized JSON format for easy reading and processing. -> Option D
  4. Quick Check:

    Structured logging = JSON format logs [OK]
Hint: Structured logs use JSON for clarity and easy parsing [OK]
Common Mistakes:
  • Thinking logs are encrypted by default
  • Confusing plain text logs with structured logs
  • Assuming logs are only stored in memory
2. Which of the following is the correct way to create a logger using the pino library in Node.js?
easy
A. const logger = require('pino')();
B. const logger = new pino.Logger();
C. const logger = pino.create();
D. const logger = require('pino').createLogger();

Solution

  1. Step 1: Recall pino import syntax

    The pino library is imported using require('pino'), which returns a function.
  2. Step 2: Create logger instance

    Calling this function with () creates the logger instance: require('pino')()
  3. Final Answer:

    const logger = require('pino')(); -> Option A
  4. Quick Check:

    pino logger created by calling require('pino')() [OK]
Hint: Use require('pino')() to create logger instance [OK]
Common Mistakes:
  • Using createLogger() which does not exist
  • Trying to use new keyword with pino
  • Calling pino.create() which is invalid
3. What will be the output of the following Node.js code using pino?
const pino = require('pino');
const logger = pino();
logger.info({ user: 'alice', action: 'login' }, 'User action');
medium
A. User action: user=alice, action=login
B. {"level":30,"time":<timestamp>,"msg":"User action","user":"alice","action":"login"}
C. {"message":"User action","user":"alice","action":"login"}
D. Error: Missing log level

Solution

  1. Step 1: Understand pino.info() output format

    pino logs messages as JSON with fields like level, time, msg, and any extra data.
  2. Step 2: Analyze given code

    Calling logger.info with an object and message logs JSON with level 30 (info), timestamp, msg, and object keys.
  3. Final Answer:

    {"level":30,"time":<timestamp>,"msg":"User action","user":"alice","action":"login"} -> Option B
  4. Quick Check:

    pino logs JSON with level and message [OK]
Hint: pino logs JSON with level, time, msg, and extra data [OK]
Common Mistakes:
  • Expecting plain text output instead of JSON
  • Confusing message key name
  • Thinking log level is missing
4. Identify the error in this code snippet using pino for structured logging:
const pino = require('pino');
const logger = pino();
logger.info('User logged in', { user: 'bob' });
medium
A. The order of arguments is incorrect; the object should come first.
B. Missing semicolon after require statement.
C. pino() must be called with configuration object.
D. logger.info cannot accept two arguments.

Solution

  1. Step 1: Check pino.info argument order

    pino expects the first argument as an object with extra data, second as message string.
  2. Step 2: Analyze given code

    The code passes message string first, then object, which is reversed and causes wrong output.
  3. Final Answer:

    The order of arguments is incorrect; the object should come first. -> Option A
  4. Quick Check:

    pino.info(object, message) argument order [OK]
Hint: Pass object first, then message string to logger.info [OK]
Common Mistakes:
  • Swapping message and object arguments
  • Thinking semicolon is mandatory for correctness
  • Assuming pino() needs config always
5. You want to log user events with structured logs including user ID, event type, and timestamp using pino. Which code snippet correctly logs this data ensuring timestamp is included in the JSON output?
hard
A. logger.info({ userId: 123, event: 'click' }, 'User event at ' + new Date());
B. logger.info('User event', { userId: 123, event: 'click', timestamp: Date.now() });
C. logger.info({ userId: 123, event: 'click', time: new Date().toISOString() }, 'User event');
D. logger.info({ userId: 123, event: 'click', timestamp: Date.now() });

Solution

  1. Step 1: Ensure correct argument order and timestamp format

    pino expects object first, then message string. Timestamp should be ISO string for readability.
  2. Step 2: Check each option

    logger.info({ userId: 123, event: 'click', time: new Date().toISOString() }, 'User event'); passes object with userId, event, and ISO timestamp, then message string. This is correct.
  3. Final Answer:

    logger.info({ userId: 123, event: 'click', time: new Date().toISOString() }, 'User event'); -> Option C
  4. Quick Check:

    Object first with ISO timestamp, then message [OK]
Hint: Pass object with ISO timestamp first, then message string [OK]
Common Mistakes:
  • Swapping message and object arguments
  • Using Date.now() without formatting
  • Concatenating message and timestamp in string
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