What if your logs could talk to you clearly instead of whispering secrets in a jumble?
Why Structured logging with JSON in Express? - Purpose & Use Cases
Imagine you run a busy web server using Express. You write plain text logs by hand to track errors and requests. When something goes wrong, you have to dig through long, messy text files to find clues.
Manual text logs are hard to read and search. They mix different info in one line, making it easy to miss important details. When your app grows, logs become huge and confusing, slowing down troubleshooting.
Structured logging with JSON organizes log data into clear, consistent fields. Each log entry is a neat JSON object, making it easy to search, filter, and analyze automatically. This saves time and reduces mistakes.
console.log('User login failed at 10:05 PM for userId=123');console.log(JSON.stringify({ event: 'login_failed', time: '22:05', userId: 123 }));Structured JSON logs let you quickly find and fix issues by searching specific fields, even in huge log files.
A developer notices many login failures. With JSON logs, they filter all 'login_failed' events and spot a pattern causing the problem fast.
Manual logs are messy and hard to search.
JSON structured logs organize data clearly.
This makes debugging faster and more reliable.
Practice
Solution
Step 1: Understand structured logging purpose
Structured logging formats logs in a clear, consistent way using JSON.Step 2: Identify benefits of JSON logs
JSON logs are easy for both humans and machines to read and analyze quickly.Final Answer:
It makes logs easy to read and parse by both humans and machines. -> Option AQuick Check:
Structured logging = clear, readable logs [OK]
- Confusing structured logging with hiding logs
- Assuming it fixes code bugs automatically
- Thinking it slows server for security
Solution
Step 1: Check JSON object syntax in JavaScript
JSON objects in JS use curly braces with key-value pairs, keys as strings or identifiers, values as strings or variables.Step 2: Identify correct console.log usage
console.log accepts an object directly to print it as JSON-like output, so console.log({ level: 'info', message: 'Server started' }); is correct.Final Answer:
console.log({ level: 'info', message: 'Server started' }); -> Option BQuick Check:
Use object syntax inside console.log [OK]
- Passing JSON as a string instead of object
- Using incorrect syntax with equals inside console.log
- Logging plain strings without JSON structure
const log = (level, message) => {
console.log(JSON.stringify({ level, message, timestamp: new Date().toISOString() }));
};
log('error', 'Failed to connect');What will be the output format?
Solution
Step 1: Understand JSON.stringify output
JSON.stringify converts the object into a JSON string with keys and values in double quotes.Step 2: Analyze the log function output
The log function prints a JSON string with keys level, message, and timestamp in ISO format.Final Answer:
{"level":"error","message":"Failed to connect","timestamp":"2024-06-01T12:00:00.000Z"} -> Option DQuick Check:
JSON.stringify outputs JSON string [OK]
- Expecting plain text instead of JSON string
- Confusing JSON string with object literal
- Ignoring timestamp format
console.log(JSON.stringify(level: 'info', message: 'Started'));
What is the error and how to fix it?
Solution
Step 1: Identify JSON.stringify argument syntax
JSON.stringify expects a single object or value; here, the argument uses invalid syntax without braces.Step 2: Correct the argument to an object
Wrap the key-value pairs inside curly braces to form a valid object: { level: 'info', message: 'Started' }.Final Answer:
SyntaxError due to incorrect JSON.stringify argument; fix by passing an object inside curly braces. -> Option CQuick Check:
JSON.stringify needs an object argument [OK]
- Passing key-value pairs without braces
- Assuming JSON.stringify accepts multiple arguments
- Ignoring syntax errors in object literals
Solution
Step 1: Check correct JSON object syntax inside JSON.stringify
app.use((req, res, next) => { console.log(JSON.stringify({ level: 'info', message: 'Request received', timestamp: new Date().toISOString(), requestId: req.id })); next(); }); correctly passes an object with keys and values inside curly braces to JSON.stringify.Step 2: Verify inclusion of all required fields and middleware usage
app.use((req, res, next) => { console.log(JSON.stringify({ level: 'info', message: 'Request received', timestamp: new Date().toISOString(), requestId: req.id })); next(); }); includes level, message, timestamp (ISO string), and requestId from req.id, then calls next() properly.Final Answer:
app.use((req, res, next) => { console.log(JSON.stringify({ level: 'info', message: 'Request received', timestamp: new Date().toISOString(), requestId: req.id })); next(); }); -> Option AQuick Check:
Middleware logs JSON object with all fields [OK]
- Passing invalid syntax to JSON.stringify
- Logging plain strings instead of JSON
- Forgetting to call next() in middleware
Ready to prove it?
Pass the test and this pattern moves unknown
