What if your logs could talk back and tell you exactly what went wrong, instantly?
Why Logging with structured formats in Node.js? - Purpose & Use Cases
Imagine you run a busy online store. When something goes wrong, you check your logs, but they are just long lines of text with no clear order or details. Finding the exact problem feels like searching for a needle in a haystack.
Manual logs are slow to read and easy to misunderstand. Without structure, you waste time guessing what each line means. Errors get missed, and fixing issues takes longer, frustrating both you and your customers.
Logging with structured formats organizes your logs into clear, consistent pieces like JSON. This makes it easy to search, filter, and understand what happened, so you can fix problems faster and keep your system running smoothly.
console.log('Error at user login: invalid password');console.log(JSON.stringify({event: 'login_error', reason: 'invalid_password', userId: 123}));Structured logging lets you quickly find and fix issues by turning messy text into clear, searchable data.
A developer notices many failed logins. With structured logs, they filter all login errors in seconds and spot a bug causing password checks to fail, fixing it before customers complain.
Manual logs are hard to read and slow to debug.
Structured formats organize logs for easy searching and understanding.
This speeds up problem-solving and improves system reliability.
Practice
Solution
Step 1: Understand structured logging
Structured logging means saving logs in a format like JSON that is easy to read and parse.Step 2: Identify the benefit
This format helps both humans and tools quickly understand log data.Final Answer:
Logs are saved in a clear, organized JSON format for easy reading and processing. -> Option DQuick Check:
Structured logging = JSON format logs [OK]
- Thinking logs are encrypted by default
- Confusing plain text logs with structured logs
- Assuming logs are only stored in memory
pino library in Node.js?Solution
Step 1: Recall pino import syntax
The pino library is imported using require('pino'), which returns a function.Step 2: Create logger instance
Calling this function with () creates the logger instance: require('pino')()Final Answer:
const logger = require('pino')(); -> Option AQuick Check:
pino logger created by calling require('pino')() [OK]
- Using createLogger() which does not exist
- Trying to use new keyword with pino
- Calling pino.create() which is invalid
const pino = require('pino');
const logger = pino();
logger.info({ user: 'alice', action: 'login' }, 'User action');Solution
Step 1: Understand pino.info() output format
pino logs messages as JSON with fields like level, time, msg, and any extra data.Step 2: Analyze given code
Calling logger.info with an object and message logs JSON with level 30 (info), timestamp, msg, and object keys.Final Answer:
{"level":30,"time":<timestamp>,"msg":"User action","user":"alice","action":"login"} -> Option BQuick Check:
pino logs JSON with level and message [OK]
- Expecting plain text output instead of JSON
- Confusing message key name
- Thinking log level is missing
const pino = require('pino');
const logger = pino();
logger.info('User logged in', { user: 'bob' });Solution
Step 1: Check pino.info argument order
pino expects the first argument as an object with extra data, second as message string.Step 2: Analyze given code
The code passes message string first, then object, which is reversed and causes wrong output.Final Answer:
The order of arguments is incorrect; the object should come first. -> Option AQuick Check:
pino.info(object, message) argument order [OK]
- Swapping message and object arguments
- Thinking semicolon is mandatory for correctness
- Assuming pino() needs config always
Solution
Step 1: Ensure correct argument order and timestamp format
pino expects object first, then message string. Timestamp should be ISO string for readability.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.Final Answer:
logger.info({ userId: 123, event: 'click', time: new Date().toISOString() }, 'User event'); -> Option CQuick Check:
Object first with ISO timestamp, then message [OK]
- Swapping message and object arguments
- Using Date.now() without formatting
- Concatenating message and timestamp in string
Ready to prove it?
Pass the test and this pattern moves unknown
