Module 10 · Project 10 · Centralize Logs with Loki or ELK · Lesson 35 of 48
Retention, cost, privacy and log alerts
Run a log system you can afford and trust: retention periods, sampling, sensitive-data rules, audit logs, and alerts built from log patterns.
In this lesson · about 25 minutes
- Set retention and control logging cost
- Keep secrets and personal data out of logs
- Turn a log pattern into a metric and an alert
Logs cost money
A busy service can write gigabytes a day. Without limits, the log bill can overtake the app's.
- Retention: keep detailed app logs 7–30 days; keep security and audit logs longer (often a year, depending on rules you must follow). Loki and Elasticsearch delete old data automatically by policy.
- Tiers: recent logs on fast storage, older ones in cheap object storage (S3 with lifecycle rules).
- Levels:
infoin production, notdebug. Turn debug on briefly for one service when investigating. - Drop noise: health-check requests every 5 seconds from every Pod add up. Filter them in the collector.
- Sampling: for very high-volume success logs, keep 1 in 10 and every error.
Privacy and security
- Logs are copied, shipped and kept for weeks, and many people can read them. Treat them as sensitive.
- Never log passwords, tokens, API keys, full card numbers or health data. Log a user id, not their email address, where you can.
- Mask fields in the logging library, and add a redaction filter in the collector as a safety net.
- Control who can query logs, and keep audit logs (who logged in, who changed permissions, Kubernetes API audit logs) separate and tamper-resistant. Project 11 and the Cloud Security (AWS) course cover CloudTrail.
Alerts from logs
Some problems only show up in logs. Turn a pattern into a number, then alert on the number, just like Project 09:
# Loki ruler: alert when the worker logs many export failures
- alert: ExportFailures
expr: |
sum(count_over_time({namespace="notes", app="worker"} |= "export failed" [5m])) > 10
for: 5m
labels: { severity: ticket }
Better still, once you know a log pattern matters, add a real metric to the app (an export_failures_total counter). Metrics are cheaper and faster to alert on. Logs explain why.
Sorting drill
Log it or not?
Sort each item. Should it appear in a production log?
- request_id and user_id
- The user's password on a failed login
- HTTP method, route, status and duration
- The Authorization header
- Error message and stack trace
- A full credit card number
- Job id and how long the job took
0 of 7 sorted
Hands-on lab (simulated terminal)
Lab: find out why exports fail
Loki is running in the cluster and logcli is pointed at it.
Step 1 of 3. Type each command and press Enter. Nothing runs on your computer.
1. Show error lines from the worker app in the notes namespace over the last hour.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.