Challenge - 5 Problems
Structured Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1: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();
});Attempts:
2 left
💡 Hint
Remember JSON.stringify converts an object to a JSON string with double quotes.
✗ Incorrect
The middleware creates an object with method, url, and userAgent keys, then logs it as a JSON string. The output is a JSON string with double quotes around keys and values.
❓ Configuration
intermediate1:30remaining
Correct JSON logging configuration for Winston in Express
Which Winston logger configuration correctly outputs logs in JSON format with timestamp and level fields?
Attempts:
2 left
💡 Hint
JSON format outputs structured logs as JSON objects.
✗ Incorrect
Using winston.format.json() ensures logs are output as JSON strings with all info fields. Other formats produce plain text or colored output.
❓ Troubleshoot
advanced2: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);
Attempts:
2 left
💡 Hint
Think about what kind of data structures JSON.stringify can handle.
✗ Incorrect
Express request objects have circular references (e.g., req.connection points back to req), so JSON.stringify fails or produces incomplete JSON, causing JSON.parse to throw errors.
🔀 Workflow
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how to consistently add request ID to all logs for a request.
✗ Incorrect
Middleware runs for every request and can attach a unique ID to req object. A custom logger can then include this ID in all logs, ensuring consistent tracking.
✅ Best Practice
expert2: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?
Attempts:
2 left
💡 Hint
Think about how to correlate logs across multiple services.
✗ Incorrect
Structured JSON logs with consistent fields and trace IDs allow logs from different services to be correlated, improving observability and troubleshooting in distributed systems.