Prompt
I'm building a SaaS app and need logs searchable by tenant and request ID
Latest observation
Oct 27, 2026
OpenAI APIWeb search: off
A good way to do this is to make tenant ID and request ID first-class fields on every log event, then index them in whatever log backend you use.
What to log
At minimum, include:
tenant_idrequest_idtimestamplevelservicemessage
Optional but useful:
user_idtrace_id/span_idroutemethodstatus_codeerror_typeenvironment
Recommended approach
1. Use structured logs
Emit logs as JSON instead of plain text so you can query them easily.
Example:
{
"timestamp": "2026-10-27T12:34:56.789Z",
"level": "info",
"service": "api",
"tenant_id": "tenant_123",
"request_id": "req_abc123",
"message": "Created invoice",
"user_id": "user_456",
"route": "/invoices",
"status_code": 201
}
2. Attach context per request
When a request enters your app:
- read/generate a
request_id - determine the
tenant_idfrom auth/subdomain/header/session - store both in request context
- ensure all logs produced during that request automatically include them
3. Index those fields in your log storage
Pick a backend that supports structured querying:
- Elasticsearch / OpenSearch
- Loki
- Datadog
- Splunk
- CloudWatch Logs Insights
- BigQuery / ClickHouse if you want analytics-style querying
Then make sure tenant_id and request_id are indexed or searchable labels/fields.
Query examples
You want searches like:
- all logs for a tenant
- all logs for a specific request
- logs for tenant + request together
Examples:
tenant_id:"tenant_123"
request_id:"req_abc123"
tenant_id:"tenant_123" AND request_id:"req_abc123"
Implementation tips
- Generate a request ID at the edge if the client doesn’t provide one.
- Propagate it downstream via headers, e.g.
X-Request-Id. - If you use distributed tracing, also include
trace_id. - Never log sensitive tenant data or secrets.
- Consider a retention policy and per-tenant access controls if tenants can view logs.
Suggested minimal schema
{
"timestamp": "...",
"level": "info",
"tenant_id": "...",
"request_id": "...",
"service": "...",
"message": "..."
}
If you want, I can also give you:
- a language-specific logging setup (Node, Python, Go, etc.),
- a database schema for storing logs, or
- a multi-tenant log search API design.