0
0
Expressframework~20 mins

Structured logging with JSON in Express - 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
1:30remaining
Output of JSON structured log in Express middleware
Consider this Express middleware that logs request info as JSON. What is the output when a GET request to '/test' with header 'User-Agent: curl/7.68.0' is received?
Express
app.use((req, res, next) => {
  const logEntry = {
    method: req.method,
    url: req.url,
    userAgent: req.headers['user-agent']
  };
  console.log(JSON.stringify(logEntry));
  next();
});
A{"method":"GET","url":"/test"}
B{method: GET, url: /test, userAgent: curl/7.68.0}
C{"method":"GET","url":"/test","userAgent":"curl/7.68.0"}
DSyntaxError: Unexpected token m in JSON at position 1
Attempts:
2 left
💡 Hint
Remember JSON.stringify converts an object to a JSON string with double quotes.
Configuration
intermediate
1:30remaining
Correct JSON logging configuration for Winston in Express
Which Winston logger configuration correctly outputs logs in JSON format with timestamp and level fields?
Aconst logger = winston.createLogger({ format: winston.format.printf(info => `${info.level}: ${info.message}`), transports: [new winston.transports.Console()] });
Bconst logger = winston.createLogger({ format: winston.format.json(), transports: [new winston.transports.Console()] });
Cconst logger = winston.createLogger({ format: winston.format.simple(), transports: [new winston.transports.Console()] });
Dconst logger = winston.createLogger({ format: winston.format.colorize(), transports: [new winston.transports.Console()] });
Attempts:
2 left
💡 Hint
JSON format outputs structured logs as JSON objects.
Troubleshoot
advanced
2:00remaining
Why does JSON.parse fail on logged Express request object?
You try to log the entire Express request object as JSON with JSON.stringify(req) and then parse it back with JSON.parse. It throws an error. Why?
Express
const jsonString = JSON.stringify(req);
const obj = JSON.parse(jsonString);
AJSON.stringify automatically removes all functions, so parse fails due to missing methods.
BJSON.parse cannot parse strings longer than 1KB.
CExpress request object is a string, so JSON.stringify returns the same string causing parse error.
DExpress request object contains circular references causing JSON.stringify to fail silently or produce incomplete output.
Attempts:
2 left
💡 Hint
Think about what kind of data structures JSON.stringify can handle.
🔀 Workflow
advanced
2:00remaining
Best workflow to add structured JSON logging with request IDs in Express
You want to add a unique request ID to every log entry in Express using structured JSON logging. Which workflow correctly achieves this?
AUse middleware to generate a UUID, attach it to req.id, then use a custom logger that includes req.id in every log entry.
BGenerate UUID in each route handler and pass it manually to logger calls only in those handlers.
CUse global variable to store current request ID and read it in logger calls.
DAdd request ID only in error handling middleware, ignoring normal requests.
Attempts:
2 left
💡 Hint
Think about how to consistently add request ID to all logs for a request.
Best Practice
expert
2:30remaining
Which logging practice improves observability in distributed Express apps?
In a distributed Express app with multiple services, which logging practice best improves observability and troubleshooting?
AUse structured JSON logs with consistent fields including trace IDs propagated across services.
BLog only error messages as plain text to reduce log size.
CUse different log formats per service to optimize for local readability.
DDisable logging in production to improve performance.
Attempts:
2 left
💡 Hint
Think about how to correlate logs across multiple services.