Log Strategy
Most teams either log too little to investigate anything or too much to find anything (and end up storing PII they didn't mean to). This skill is the middle path: enough to support incident response, not so much that the log becomes a liability of its own.
Pairs with incident-response (where logs get used) and gdpr-technical-controls (where log privacy lives).
When to invoke
- Starting a new service or workflow
- Investigation revealed missing fields ("we don't know what request triggered this")
- Logs are leaking PII or secrets
- Log volume is becoming expensive / unwieldy
- Consolidating to a central logging stack (Loki, ELK, OpenObserve, hosted)
- After an incident where the log was insufficient
Three classes of log, three retention tiers
Treat your logs as three distinct streams. Conflating them is where the trouble starts.
| Class |
Purpose |
Typical retention |
Storage tier |
| Operational |
Debugging, performance, errors |
7–30 days |
Hot index |
| Access |
Who hit what, when (webserver + app request log) |
30–90 days |
Hot + warm |
| Audit |
Security-relevant events (auth, permission changes, sensitive actions) |
12 months+ |
Append-only, immutable where possible |
Costs and tools differ per class. Mixing them gives the audit log's slow-and-expensive retention to the operational stream and is the most common reason logging gets killed by finance.
Operational logs — what to capture
Structured logging beats freeform every time. Choose JSON (or logfmt) and a single library for the whole app.
// pino example — Node
import pino from 'pino';
const log = pino({
level: process.env.LOG_LEVEL ?? 'info',
redact: {
paths: ['password', 'token', 'authorization', '*.password', '*.token', 'req.headers.authorization', 'req.headers.cookie'],
censor: '[REDACTED]',
},
serializers: {
req: (req) => ({
method: req.method,
url: req.url,
requestId: req.id,
// explicitly NOT: headers (except a few safe ones), body
}),
err: pino.stdSerializers.err,
},
});
log.info({ orderId, userId: anonymize(userId) }, 'order created');
log.warn({ requestId, latencyMs }, 'slow query');
log.error({ requestId, err }, 'unhandled error in handler');
Patterns:
- One event per line, structured. Greppable, parseable by any log system.
- A
requestId / traceId on every log line within a request — lets you reconstruct a request's full path. Generate at the edge (Nginx / Cloudflare / app middleware), pass through to downstream services.
- Log levels mean something.
error = an unhandled exception or a degraded user outcome; warn = a recoverable surprise; info = state changes worth knowing about; debug = noisy diagnostics that ship off by default.
- No secrets, no PII — redact in the serializer, not at the call site (humans forget; serializers don't).
- Bounded volume per request — a handler logging 50 lines per request creates a $/month problem. Tighten before launch.
Access logs — webserver + app
Webserver-level access logs (nginx, Caddy, Cloudflare logs) are usually fine with their defaults plus a few tweaks:
# nginx — log format that includes useful diagnostic + minimal PII
log_format main escape=json '{'
'"time":"$time_iso8601",'
'"remote_addr":"$remote_addr",' # consider hashing — see below
'"request_id":"$request_id",'
'"method":"$request_method",'
'"path":"$request_uri",'
'"status":$status,'
'"body_bytes_sent":$body_bytes_sent,'
'"referer":"$http_referer",'
'"user_agent":"$http_user_agent",'
'"request_time":$request_time,'
'"upstream_response_time":"$upstream_response_time"'
'}';
access_log /var/log/nginx/access.log main;
Considerations:
- IPs in DACH/EU — full IP storage is widely treated as personal data. Options:
- Hash with HMAC + rotating key (anonymous but consistent within rotation window)
- Truncate to
/24 for IPv4, /64 for IPv6 (lossy but acceptable for most analytics)
- Keep full only for the abuse-investigation tier with shorter retention (e.g. 7 days)
- No bodies in access logs. Bodies leak passwords on login routes, payment data on checkout, PII on profile updates. The webserver default doesn't log bodies; don't be tempted by "let's just log the request body for debugging".
- Cookies and
Authorization header stripped — they include session identifiers and tokens.
Audit logs — security-relevant events
These are the events you want to find six months from now during an investigation.
What deserves an audit-log entry:
- Authentication: successful login, failed login, MFA challenge result, password change, password reset request, password reset use, account locked, email change request, email change confirm, account deletion
- Authorization: role/permission granted or revoked, admin actions affecting other users, sensitive data export (SAR), data deletion
- Configuration: feature-flag flips, security-setting changes, API key issued/revoked, integration enabled/disabled
- Sensitive data access: who viewed which PII record, who exported the database, who downloaded an attachment containing PII
- Money-touching: refunds issued, manual ledger adjustments, fee waivers, balance transfers
Per entry, capture:
{
"ts": "2026-05-12T14:23:00Z",
"actor": { "type": "user", "id": "u_abc", "via": "session" },
"action": "user.password.reset.request",
"target": { "type": "user", "id": "u_abc" },
"context": {
"ip_hash": "...",
"user_agent": "...",
"request_id": "...",
"result": "ok"
}
}
Patterns:
- Append-only. Use a write-only database role, an append-only S3 bucket with object lock, or a managed audit-log service. The app's normal role must not be able to delete or update audit rows.
- Tamper-evidence for high-stakes contexts: hash-chain each entry against the previous one, or write to a service that does (e.g. AWS QLDB, Tigris).
- Separate storage from operational logs — different retention, different access controls.
- Indexed by actor, target, and action for investigation queries.
- Visible to the user for their own audit trail ("recent activity" view) — surfaces own compromises faster than you do.
What NEVER to log
A handful of values that turn the log into a new liability:
- Passwords, password hashes, password reset tokens
- Session cookies /
Authorization headers / API keys / JWTs / OAuth tokens
- Credit card numbers, CVVs, bank account numbers (PCI scope explodes if you do)
- Private keys, signing keys, encryption keys
- Full social security / national-ID numbers
- Full email or phone of users when an identifier alone would do
- Raw request/response bodies on any auth, payment, or profile endpoint
If a regex / structured-log redactor catches these, you're protected when a future code change accidentally tries to log one.
Centralization choices
Pick a stack and stick to it. Mixing is operational hell.
Self-hosted options:
- Grafana Loki + Promtail / Vector — store JSON logs in object storage, query in Grafana. Cheap, simple, fine for small/medium scale.
- OpenSearch / ELK — full text + dashboarding. More moving parts.
- OpenObserve — newer, integrates logs+metrics+traces, lower operational footprint than ELK.
Hosted options:
- Datadog / New Relic / Honeycomb — turn-key, expensive at volume, easiest pickup
- Better Stack / Axiom / Baselime — affordable mid-tier
- Cloudflare Logpush — if you're already on Cloudflare, ship to R2/S3 cheaply
For audit logs specifically, consider a separate destination (immutable bucket, or a service like AWS CloudTrail / Google Cloud Audit Logs equivalent).
Alerts vs logs
Logs are for investigation; alerts are for getting woken up. They should not be the same thing.
- From operational logs: 5xx rate per service, error-class spike, slow-query rate, queue depth
- From access logs: spike in 4xx per route (especially 401/403/404), spike in 429, unusual ASNs
- From audit logs: admin actions on other users, sensitive data exports, password-reset bursts on a single account, MFA-disabled events
Route each alert class to a different channel — paged for ops, weekly digest for audit anomalies, etc. Reduces alert fatigue.
Cost control
Logs scale linearly with traffic; cost surprises are universal.
- Cap volume at the source. A handler emitting 50 log lines per request is the most common cause of bills.
- Sample debug-level logs in production. Keep 1–10% of
debug and trace.
- Drop bot traffic at the ingest layer if it's clean (otherwise it dominates the access log).
- Retain warm tier short, archive long. Most queries hit the last 7 days; cold storage in S3/R2 is pennies vs hot indexing.
- Reduce field count. Every JSON key in every log line costs at index time. Audit which fields are queried, drop the rest.
Privacy checklist (DACH / EU)
What this skill will not do
- Recommend logging request bodies on auth or payment routes
- Help build a logging stack for systems you do not own
- Replace a regulated-environment (HIPAA / PCI / SOC 2) audit-log requirement — those have specifics this doesn't cover
1---
2name: log-strategy
3description: Design logging that supports investigations without becoming a privacy liability. Covers what to log and what never to log (PII, secrets), structured logging, retention tiers, centralization choices, alert routing, and the operational-versus-access-versus-audit log split. Invoke when starting a new service, when investigation revealed missing log fields, or when log volume is becoming expensive.
4metadata:
5 source: https://skillmd.com/skills/yanacuti1121/log-strategy
6---
7
8# Log Strategy
9
10Most teams either log too little to investigate anything or too much to find anything (and end up storing PII they didn't mean to). This skill is the middle path: enough to support incident response, not so much that the log becomes a liability of its own.
11
12Pairs with [`incident-response`](../incident-response/SKILL.md) (where logs get used) and [`gdpr-technical-controls`](../gdpr-technical-controls/SKILL.md) (where log privacy lives).
13
14## When to invoke
15
16- Starting a new service or workflow
17- Investigation revealed missing fields ("we don't know what request triggered this")
18- Logs are leaking PII or secrets
19- Log volume is becoming expensive / unwieldy
20- Consolidating to a central logging stack (Loki, ELK, OpenObserve, hosted)
21- After an incident where the log was insufficient
22
23## Three classes of log, three retention tiers
24
25Treat your logs as three distinct streams. Conflating them is where the trouble starts.
26
27| Class | Purpose | Typical retention | Storage tier |
28|---|---|---|---|
29| **Operational** | Debugging, performance, errors | 7–30 days | Hot index |
30| **Access** | Who hit what, when (webserver + app request log) | 30–90 days | Hot + warm |
31| **Audit** | Security-relevant events (auth, permission changes, sensitive actions) | 12 months+ | Append-only, immutable where possible |
32
33Costs and tools differ per class. Mixing them gives the audit log's slow-and-expensive retention to the operational stream and is the most common reason logging gets killed by finance.
34
35## Operational logs — what to capture
36
37Structured logging beats freeform every time. Choose JSON (or logfmt) and a single library for the whole app.
38
39```ts
40// pino example — Node
41import pino from 'pino';
42
43const log = pino({
44 level: process.env.LOG_LEVEL ?? 'info',
45 redact: {
46 paths: ['password', 'token', 'authorization', '*.password', '*.token', 'req.headers.authorization', 'req.headers.cookie'],
47 censor: '[REDACTED]',
48 },
49 serializers: {
50 req: (req) => ({
51 method: req.method,
52 url: req.url,
53 requestId: req.id,
54 // explicitly NOT: headers (except a few safe ones), body
55 }),
56 err: pino.stdSerializers.err,
57 },
58});
59
60log.info({ orderId, userId: anonymize(userId) }, 'order created');
61log.warn({ requestId, latencyMs }, 'slow query');
62log.error({ requestId, err }, 'unhandled error in handler');
63```
64
65Patterns:
66
67- **One event per line, structured**. Greppable, parseable by any log system.
68- **A `requestId` / `traceId` on every log line** within a request — lets you reconstruct a request's full path. Generate at the edge (Nginx / Cloudflare / app middleware), pass through to downstream services.
69- **Log levels mean something**. `error` = an unhandled exception or a degraded user outcome; `warn` = a recoverable surprise; `info` = state changes worth knowing about; `debug` = noisy diagnostics that ship off by default.
70- **No secrets, no PII** — redact in the serializer, not at the call site (humans forget; serializers don't).
71- **Bounded volume per request** — a handler logging 50 lines per request creates a $/month problem. Tighten before launch.
72
73## Access logs — webserver + app
74
75Webserver-level access logs (nginx, Caddy, Cloudflare logs) are usually fine with their defaults plus a few tweaks:
76
77```nginx
78# nginx — log format that includes useful diagnostic + minimal PII
79log_format main escape=json '{'
80 '"time":"$time_iso8601",'
81 '"remote_addr":"$remote_addr",' # consider hashing — see below
82 '"request_id":"$request_id",'
83 '"method":"$request_method",'
84 '"path":"$request_uri",'
85 '"status":$status,'
86 '"body_bytes_sent":$body_bytes_sent,'
87 '"referer":"$http_referer",'
88 '"user_agent":"$http_user_agent",'
89 '"request_time":$request_time,'
90 '"upstream_response_time":"$upstream_response_time"'
91'}';
92access_log /var/log/nginx/access.log main;
93```
94
95Considerations:
96
97- **IPs in DACH/EU** — full IP storage is widely treated as personal data. Options:
98 - Hash with HMAC + rotating key (anonymous but consistent within rotation window)
99 - Truncate to `/24` for IPv4, `/64` for IPv6 (lossy but acceptable for most analytics)
100 - Keep full only for the abuse-investigation tier with shorter retention (e.g. 7 days)
101- **No bodies in access logs**. Bodies leak passwords on login routes, payment data on checkout, PII on profile updates. The webserver default doesn't log bodies; don't be tempted by "let's just log the request body for debugging".
102- **Cookies and `Authorization` header** stripped — they include session identifiers and tokens.
103
104## Audit logs — security-relevant events
105
106These are the events you want to find six months from now during an investigation.
107
108What deserves an audit-log entry:
109
110- **Authentication**: successful login, failed login, MFA challenge result, password change, password reset request, password reset use, account locked, email change request, email change confirm, account deletion
111- **Authorization**: role/permission granted or revoked, admin actions affecting other users, sensitive data export (SAR), data deletion
112- **Configuration**: feature-flag flips, security-setting changes, API key issued/revoked, integration enabled/disabled
113- **Sensitive data access**: who viewed which PII record, who exported the database, who downloaded an attachment containing PII
114- **Money-touching**: refunds issued, manual ledger adjustments, fee waivers, balance transfers
115
116Per entry, capture:
117
118```json
119{
120 "ts": "2026-05-12T14:23:00Z",
121 "actor": { "type": "user", "id": "u_abc", "via": "session" },
122 "action": "user.password.reset.request",
123 "target": { "type": "user", "id": "u_abc" },
124 "context": {
125 "ip_hash": "...",
126 "user_agent": "...",
127 "request_id": "...",
128 "result": "ok"
129 }
130}
131```
132
133Patterns:
134
135- **Append-only.** Use a write-only database role, an append-only S3 bucket with object lock, or a managed audit-log service. The app's normal role must not be able to delete or update audit rows.
136- **Tamper-evidence** for high-stakes contexts: hash-chain each entry against the previous one, or write to a service that does (e.g. AWS QLDB, Tigris).
137- **Separate storage** from operational logs — different retention, different access controls.
138- **Indexed by actor, target, and action** for investigation queries.
139- **Visible to the user** for their own audit trail ("recent activity" view) — surfaces own compromises faster than you do.
140
141## What NEVER to log
142
143A handful of values that turn the log into a new liability:
144
145- Passwords, password hashes, password reset tokens
146- Session cookies / `Authorization` headers / API keys / JWTs / OAuth tokens
147- Credit card numbers, CVVs, bank account numbers (PCI scope explodes if you do)
148- Private keys, signing keys, encryption keys
149- Full social security / national-ID numbers
150- Full email or phone of users when an identifier alone would do
151- Raw request/response bodies on any auth, payment, or profile endpoint
152
153If a regex / structured-log redactor catches these, you're protected when a future code change accidentally tries to log one.
154
155## Centralization choices
156
157Pick a stack and stick to it. Mixing is operational hell.
158
159**Self-hosted options**:
160
161- **Grafana Loki + Promtail / Vector** — store JSON logs in object storage, query in Grafana. Cheap, simple, fine for small/medium scale.
162- **OpenSearch / ELK** — full text + dashboarding. More moving parts.
163- **OpenObserve** — newer, integrates logs+metrics+traces, lower operational footprint than ELK.
164
165**Hosted options**:
166
167- **Datadog / New Relic / Honeycomb** — turn-key, expensive at volume, easiest pickup
168- **Better Stack / Axiom / Baselime** — affordable mid-tier
169- **Cloudflare Logpush** — if you're already on Cloudflare, ship to R2/S3 cheaply
170
171For audit logs specifically, consider a separate destination (immutable bucket, or a service like AWS CloudTrail / Google Cloud Audit Logs equivalent).
172
173## Alerts vs logs
174
175Logs are for investigation; alerts are for getting woken up. They should not be the same thing.
176
177- **From operational logs**: 5xx rate per service, error-class spike, slow-query rate, queue depth
178- **From access logs**: spike in 4xx per route (especially 401/403/404), spike in 429, unusual ASNs
179- **From audit logs**: admin actions on other users, sensitive data exports, password-reset bursts on a single account, MFA-disabled events
180
181Route each alert class to a different channel — paged for ops, weekly digest for audit anomalies, etc. Reduces alert fatigue.
182
183## Cost control
184
185Logs scale linearly with traffic; cost surprises are universal.
186
187- **Cap volume at the source.** A handler emitting 50 log lines per request is the most common cause of bills.
188- **Sample debug-level logs in production.** Keep 1–10% of `debug` and `trace`.
189- **Drop bot traffic at the ingest layer** if it's clean (otherwise it dominates the access log).
190- **Retain warm tier short, archive long.** Most queries hit the last 7 days; cold storage in S3/R2 is pennies vs hot indexing.
191- **Reduce field count.** Every JSON key in every log line costs at index time. Audit which fields are queried, drop the rest.
192
193## Privacy checklist (DACH / EU)
194
195- [ ] No bodies of POST/PUT/PATCH in access logs
196- [ ] IPs hashed/truncated, or kept full only on a separate stream with short retention
197- [ ] `Authorization`, `Cookie`, `Set-Cookie` headers stripped
198- [ ] Known-sensitive JSON fields redacted in structured logs by key name
199- [ ] Retention period documented per log stream, matches privacy policy
200- [ ] Sub-processor list updated if logs ship to a hosted service
201- [ ] Audit logs separate from operational, with separate retention and access controls
202- [ ] Log queries by employees are themselves audited (who searched what)
203
204## What this skill will not do
205
206- Recommend logging request bodies on auth or payment routes
207- Help build a logging stack for systems you do not own
208- Replace a regulated-environment (HIPAA / PCI / SOC 2) audit-log requirement — those have specifics this doesn't cover