Monitoring & Logging
Know when things break before your users do — uptime monitoring, structured logging, and error tracking with Sentry
Skills taught: deployment.monitoring, deployment.structured-logging, deployment.sentry, deployment.uptime, deployment.alerting
The monitoring stack
Production monitoring has three layers: uptime monitoring (is the site responding?), error tracking (what errors are users hitting?), and logging (what happened before the error?). You need all three — a site can be 'up' (health check passes) while throwing errors on specific endpoints. And logs tell you the context around errors that error tracking alone can't capture. Most production incidents are diagnosed through logs.
Uptime monitoring — BetterStack & UptimeRobot
BETTERSTACK (recommended):
Free: 10 monitors, 3-minute check interval
Checks your URL from multiple regions (Australia, Europe, US)
Alerts via: email, SMS, Slack, PagerDuty
Status page: status.myapp.com (public or private)
Incident management built in
Free tier is sufficient for most apps
UPTIMEROBOT (popular free option):
Free: 50 monitors, 5-minute intervals
Simpler than BetterStack
Good starting point
WHAT TO MONITOR:
/ (homepage)
/health (app health check)
/api/products (critical API endpoint)
/login (authentication flow)
ALERT FLOW:
Check from Australia: https://myapp.com/health → 200?
→ YES: All good
→ NO (3 consecutive failures):
1. Email: 'Alert: myapp.com is down'
2. SMS to on-call engineer
3. Slack: #incidents channel
4. Auto-create incident in BetterStack
5. Start incident timer
GOAL:
You should know your site is down BEFORE your users tell you
Structured logging — JSON logs
// Pino is the fastest Node.js logger (100,000+ ops/sec)
// 'pino' and 'pino-pretty' packages
import pino from 'pino';
const logger = pino({
level: process.env.LOG_LEVEL ?? 'info',
// In production: JSON output (easy to parse)
// In development: use pino-pretty for human-readable output
transport: process.env.NODE_ENV === 'development'
? { target: 'pino-pretty', options: { colorize: true } }
: undefined,
// Base fields added to every log line
base: {
pid: process.pid,
service: 'myapp-api',
version: process.env.APP_VERSION,
},
});
// Use structured data (NOT string interpolation)
// Bad: hard to query
logger.info(`User ${userId} purchased product ${productId} for ${price}`);
// Good: queryable JSON fields
logger.info(
{ userId, productId, price, orderId: newOrder.id, event: 'purchase' },
'Order created'
);
// JSON output (one line per log):
// {"level":30,"time":1234567890,"service":"myapp-api",
// "userId":42,"productId":123,"price":49.99,
// "orderId":"ord_abc","event":"purchase","msg":"Order created"}
// Log levels:
logger.trace('Very detailed debugging'); // level: 10
logger.debug('Debugging information'); // level: 20
logger.info('Normal operation'); // level: 30
logger.warn('Something unexpected, not critical'); // level: 40
logger.error({ err }, 'Request failed'); // level: 50 (include error object)
logger.fatal('Unrecoverable error, crashing'); // level: 60
Sentry — error tracking
// Install: npm install @sentry/node @sentry/react
// === Backend (src/instrument.ts — import FIRST, before anything else) ===
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV, // 'production', 'staging'
release: process.env.APP_VERSION, // e.g. 'v1.2.3' from git tag
tracesSampleRate: 0.1, // trace 10% of requests (performance)
// profilesSampleRate: 0.1, // optional: CPU profiling
});
// Add Sentry to Express (must be after all routes)
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
// ... all your routes ...
app.use(Sentry.Handlers.errorHandler()); // captures errors thrown in routes
// Manual error capture:
try {
await processPayment(order);
} catch (error) {
Sentry.captureException(error, {
extra: { orderId: order.id, userId: order.userId },
tags: { component: 'payment' },
});
throw error; // re-throw to let the error handler respond to user
}
// Add user context (shows which user experienced the error):
Sentry.setUser({ id: String(req.user.id), email: req.user.email });
// === Frontend (src/main.tsx) ===
import * as Sentry from '@sentry/react';
Sentry.init({
dsn: import.meta.env.VITE_SENTRY_DSN,
integrations: [Sentry.browserTracingIntegration()],
tracesSampleRate: 0.1,
// Add source maps for readable stack traces:
// sentry.io/settings/myorg/myapp/source-maps/
});
Request logging middleware
import { randomUUID } from 'crypto';
// Add request ID to every request for correlation
app.use((req, res, next) => {
req.id = randomUUID();
res.setHeader('X-Request-ID', req.id);
next();
});
// Log every request with timing
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
const level = res.statusCode >= 500 ? 'error'
: res.statusCode >= 400 ? 'warn'
: 'info';
logger[level]({
requestId: req.id,
method: req.method,
path: req.path,
statusCode: res.statusCode,
durationMs: duration,
userAgent: req.get('user-agent'),
// Never log: Authorization header, passwords, payment data
}, `${req.method} ${req.path} ${res.statusCode} ${duration}ms`);
});
next();
});
// Output:
// {"level":30,"requestId":"abc-123","method":"GET","path":"/api/products",
// "statusCode":200,"durationMs":42,"msg":"GET /api/products 200 42ms"}
Tip: Never log sensitive data. Authorization headers, passwords, full credit card numbers, SSNs, health data. Once it's in your logs, it's hard to remove — logs are often aggregated, shipped to third-party services, and retained for months. Add a log sanitizer that scrubs known-sensitive field names before writing. A common pattern: const SENSITIVE = new Set(['password', 'token', 'authorization', 'secret']) — replace any object value whose key is in this set with '[REDACTED]'.
Frequently Asked Questions
What logging service should I use for log aggregation?
For small apps: BetterStack Logs (free tier: 1GB/month, 3-day retention) or Logtail. For larger apps: Datadog (expensive but powerful), Grafana Loki (self-hosted, free), or AWS CloudWatch Logs. The key is centralized storage — logs on your server are lost when you replace it, and you can't query across multiple servers. Any service that accepts log shipping (via Pino transports or a log shipper like Vector) works.