# Logs and log drains
Source: https://sovaria.in/docs/logs-and-log-drains

Request, function, database and audit logs in the console, and log drains that forward request and function logs to Datadog, Grafana Loki or your own HTTPS endpoint.

## Where to look

| Log | Console | What it shows |
|---|---|---|
| API requests | **API logs** | Every request to your project: method, path, status, latency, service. |
| Edge functions | **Edge logs** and each function's logs | Each run: status, duration, error, output preview. |
| Postgres | **Postgres logs** | Your database's own log: errors, slow statements, connections. |
| Audit trail | **Audit Trail** | Sign-ins, key and policy changes, deploys, migrations — tamper-evident. |

CLI:
```bash
sov logs requests
sov logs api
```

## Metrics and the live request log

**Observability** shows, for the chosen time range, requests, 4xx/5xx errors and error rate, response time (average, p50, p95, p99) and data transferred — for the whole project and per product (Data API, Auth, Storage, Edge Functions, Realtime). **API Gateway** breaks it down per service, so a slow or failing service stands out.

**Observability → Request logs** lists your latest requests (method, path, status, service, duration) and updates live: the last 1,000 on paid plans, the last 200 on Free. Only the path is stored, never the query string, so tokens in URLs don't end up in logs. A refused or failed request has a **Trace** link: it opens [Request traces: why did this request fail?](https://sovaria.in/docs/request-traces) at that request, with the check or service that said no.

## Latency per route

**Observability → Routes** ranks your API's routes by traffic with their p50, p95 and p99 response time and their 4xx and 5xx counts. A route is the method and the path with ids collapsed — `GET /rest/v1/orders`, `POST /api/v1/data/doc/_query/users/:id/orders`, `GET /api/v1/store/objects/avatars/:path` — so a project has a few dozen routes, not one per row. Pick **All environments**, **Production** or one staging environment; click a route for its requests and p95 over time. Routes with a p95 over a second or 1 % server errors are highlighted.

- Only your app's traffic counts (API keys and app users); people clicking around the console don't.
- Latency is measured at the gateway, from the request arriving to the last byte of the answer.
- Route data is kept in 15-minute points for a day and hourly points for up to 7 days (never longer than the plan's metrics history). Up to 40 routes per 15 minutes are tracked; the rest are summed as `other`.

CLI:
```bash
sov metrics routes --hours 168
sov metrics routes --route "GET /rest/v1/orders" --json
```

JavaScript:
```ts
const { routes } = await sov.api.observability.routes({ hours: 24 })
routes[0] // { route: 'GET /rest/v1/orders', requests: 1840, p50_ms: 25, p95_ms: 250, p99_ms: 1000, errors_5xx: 2, … }
```

HTTP:
```bash
curl -s "$URL/api/v1/observability/routes?hours=24&env=main" \
  -H "apikey: $SERVICE_KEY"
```

To be told when a route gets slow or starts failing, add an alert: [Alerts: latency, errors, function failures and connections](https://sovaria.in/docs/slo-alerts).

|  | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Metrics history | 1 day | 7 days | 14 days | 28 days | 90 days |
| Metrics resolution | 15-minute averages | 1 minute | 1 minute | 1 minute | 1 minute |
| Request log history | 30 minutes | 7 days | 14 days | 28 days | 90 days |
| Requests kept in the log | 200 | 1,000 | 1,000 | 1,000 | 1,000 |

## Log drains

A log drain forwards your project's logs to a tool you already use: every API request and every function run. Each line has a `source` of `request` or `function`. New lines are sent in batches within about 10 seconds, oldest first, and never twice. Add drains under **Observability → Log drains**.

| Destination | You provide | Notes |
|---|---|---|
| HTTPS endpoint | URL, optional auth header | Body `{ "project_id", "drain_id", "logs": [...] }`, signed with `X-Sovaria-Signature: sha256=<hex>` |
| Datadog | Site and API key | Function runs arrive as service `sovaria-functions` |
| Grafana Loki | URL, user and token | One stream per source (`log_type`) |

Destinations must be public HTTPS hosts. After repeated failed deliveries (about 20 minutes) a drain pauses itself; fix the destination and resume it. **Send test** delivers one sample line.

> **Best effort, not an audit trail:** Request and function lines wait for delivery in a memory buffer that drops the oldest entries when it is full, so a long destination outage or a sudden flood can lose lines. For records you must keep, use the audit log ([Audit log](https://sovaria.in/docs/audit-logs)), which is stored in the database and hash-chained.

Verify a signature (Node):
```ts
import crypto from 'node:crypto'

export function verify(rawBody: Buffer, header: string | undefined, secret: string): boolean {
  const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex')
  return !!header && header.length === expected.length && crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected))
}
```

## Plans

|  | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Log drains per project | — | 1 | 3 | 10 | 50 |

Every limit is in [Plan limits](https://sovaria.in/docs/plan-limits).
