Expert · Performance and reliability · topic 43 of 54 · 30 min
Logging and monitoring
Explanation
Three ways to see inside a running app
| Signal | What it is | Answers questions like |
|---|---|---|
| Logs | Lines of text or JSON, one for each event | What exactly happened to order 9001 at 14:02? |
| Metrics | Numbers measured over time | How many requests per second? How many errors? |
| Traces | The path of one request through all services, with timings | Which step made this checkout take 4 seconds? |
Log levels
- debug: very detailed, for developers. Usually off in production.
- info: normal events. "Order 9001 placed".
- warn: something odd but handled. "Payment retry 2 of 3".
- error: something failed. "Could not reach SMS provider".
- fatal: the app cannot continue and is stopping.
Structured logs and request ids
A structured log is a log line written as JSON with named fields, instead of a free sentence. Machines can search and filter it: "show all error lines for userId 42 in the last hour".
{"level":"error","time":"2026-03-14T08:21:05.120Z","msg":"payment failed","requestId":"req-7f3a","orderId":9001,"provider":"upi","durationMs":5020}A request id is a unique id given to each incoming request, often sent back in a response header too. Every log line written while handling that request includes it. When a user reports a problem, you search one id and see the whole story.
Metrics that matter
Three numbers tell you most of what you need. Requests per second (traffic): how busy the app is. Error rate: the share of requests that fail, usually counted as 5xx responses. Latency: how long responses take.
For latency, an average hides problems. p95 latency means 95% of requests were faster than this number, and 5% were slower. If p95 is 2 seconds, one user in twenty waits 2 seconds or more, even if the average looks fine. To find it, sort all the times and pick the value at the 95% position.
Health checks, alerts and on-call
app.get('/health', async (req, res) => {
try {
await db.query('SELECT 1'); // can we reach the database?
res.json({ status: 'ok' });
} catch {
res.status(503).json({ status: 'error', db: 'down' });
}
});An alert is an automatic message (phone call, SMS, chat) sent when a metric crosses a line, for example "error rate above 2% for 5 minutes" or "/health failing". On-call means one team member is responsible for answering alerts during a period, taking turns. Alert only on things that need a human, or people start ignoring alerts.
Watch the lesson
Logging and monitoring · 1/7
When your app is live, you need ways to see inside it. There are three: logs, metrics and traces.
Prefer a video by a teacher? Find free videos on “logging monitoring observability explained logs metrics traces” on YouTube ↗
In real life: Slow UPI payments on a Saturday
On a Saturday evening, a shopping app's p95 checkout latency jumped from 800 ms to 6 seconds, and an alert called the on-call developer. The average was only 1.2 seconds, so the dashboard looked almost normal, but p95 showed one user in twenty was waiting a long time. She searched the logs for error and warn lines in the checkout route, picked one requestId, and saw the whole journey: the call to the payment provider was timing out and retrying. The trace confirmed it. The team switched to the backup provider and posted a status update, all within 20 minutes. No passwords or card numbers were in any log line.
Practice (0/3 done)
Write percentile(values, p) using the nearest-rank method: sort a COPY of the numbers ascending, rank = Math.ceil(p * n / 100), and return the value at position rank (counting from 1). If rank is 0 use 1. If values is empty return null. Do not change the original array.
Write logLine(level, msg, context, now) that returns ONE JSON line (a string). Keys in this order: level, time (now().toISOString()), msg, requestId (from context), then every other field of context in its order, EXCEPT fields whose lower-case name contains any word in SECRET_WORDS.
Plan monitoring for a food delivery API before launch. List what you will log, which metrics you will watch, what /health checks, and two alerts with their thresholds.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.