intermediate
Cloud Logging
Centralize logs and metrics with sinks, retention, structured payloads, trace correlation, and alerting integrations.
Cloud Logging centralizes logs from Cloud Run, GKE, Cloud Functions, load balancers, and audit trails. Structured JSON logs from Node.js (`pino`, `winston`) map cleanly to filters, sinks, and log-based metrics without fragile regex parsing.
import pino from 'pino';
const logger = pino({
level: process.env.LOG_LEVEL ?? 'info',
base: { service: 'checkout-api', revision: process.env.K_REVISION },
});
logger.info({ orderId, latencyMs }, 'checkout completed');
| Tool | Purpose | |------|---------| | Log sink | Export to BigQuery or Cloud Storage for analysis | | Log-based metric | Turn error patterns into alerts | | Trace integration | Tie logs to Cloud Trace spans via `trace` field |
Set retention per log bucket, redact PII at the source, and avoid high-cardinality labels in metrics derived from logs.
On interviews: structured logging versus plain text; correlation with trace IDs; cost of verbose debug in production; alerting on symptoms not raw log volume.
Common pitfalls: logging tokens or card data; printf-style strings that break search; no request ID across services; paying for 90-day retention on noisy debug logs.
The trade-off is rich observability versus ingestion cost, retention compliance, and alert noise.
Checklist:
- Emit JSON logs with request, trace, and service fields.
- Redact secrets and PII before write.
- Route long-term analytics through sinks, not infinite retention.
- Alert on SLO symptoms using log-based metrics sparingly.