# Investigating logs

This guide investigates your organisation’s logs with the API alone, the way you would in the console: counts by a field instead of paging through every line, a search of several terms that can leave lines out and match whole words or patterns, one line opened from a console link, the exact time of every line, counts of the message patterns, the lines around a line, the systemd unit of a journal line, and the status, path and duration of a web server’s access lines. Windows take `period`, or `from` and `to`, as everywhere in the API ([times and windows](https://developer.watchtower15.com/api/times-and-windows/)): the last hour by default, and any window **back to retention (30 days)**.

## What the key needs

Every read below needs `logs.view`, besides the same permission on the role of the key’s owner. See [authentication and keys](https://developer.watchtower15.com/api/authentication/#scopes). The lines are those of your servers and log sources only.

## Counting by a field

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs/groups \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d by=http_path \
  -d 'http_statuses[]=502' \
  -d 'streams[]=access.log' \
  -d period=1h
```

```json
{
  "data": {
    "window": { "from": 1795000000000, "to": 1795003600000 },
    "groups": [
      { "value": "/api/ingest/logs", "count": 612 },
      { "value": "/api/agent/telemetry", "count": 401 },
      { "value": null, "count": 3 }
    ]
  }
}
```

`by` is required and one of `stream`, `level`, `source`, `unit`, `identifier`, `http_status`, `http_path` and `pattern` ([below](https://developer.watchtower15.com/api/investigating-logs/#message-patterns)). The groups come **largest first**, `limit` of them: 1 to 100, 50 by default. The filters are the search’s (`source_uuids[]`, `levels[]`, `streams[]`, `tag_uuids[]`, `search` and the [search terms](https://developer.watchtower15.com/api/investigating-logs/#searching-with-several-terms) below), so the same call counts what a search would list. `value` is a string for every `by` (a status is `"502"`), and **`null` counts the lines that do not have the field**: a file line has no unit, and a line the backend did not parse has no status. `window` is the window read.

## Opening a line from a console link

A console link to a line, `<console address>/logs/<uuid>?at=<time>`, carries the line’s `uuid` and its `time`. Read that line with them:

```bash
curl -sS -G "https://rest.watchtower15.com/v1/logs/$LINE_UUID" \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d at=2026-11-18T10:35:05.946083800Z
```

`at` is required, and it is the link’s `at` exactly as written (an RFC 3339 time of up to nine fraction digits), or a `logged_at` in epoch milliseconds. The line is looked for within 5 seconds of it, so a millisecond `logged_at` finds the line as well as the link’s exact time. The answer is one line, shaped as a search item, with the `raw_line` besides: the line as a **log source** received it, and `null` for a server’s line. Each search and context item carries `console_url`, the link back to that line in the console.

-   A line that is not found, one of another organisation, or a `uuid` that is no uuid is `422 invalid_request` on `line_uuid`, never a 404.
-   The path takes the line’s `uuid` only after the fixed paths (`/v1/logs/facets`, `/groups`, `/histogram`, `/context`), which keep their meaning.
-   It needs `logs.view`, as every log read does.

## Times: `time`, `time_source` and `received_at`

Besides `logged_at` (epoch milliseconds), every search, context and single-line item answers:

| Field | What it says |
| --- | --- |
| `time` | The line’s time as RFC 3339 in UTC, **always with nine fraction digits** (`2026-11-18T10:35:05.946083800Z`): two lines of the same millisecond still sort by it, and it is the string a console link’s `at` carries. |
| `time_source` | Where the time comes from, one of the seven values below. |
| `received_at` | When WatchTower15 received the line, in epoch milliseconds; `null` for a line stored without it. |

| `time_source` | The time is |
| --- | --- |
| `read` | a server line’s time as the agent read it: for a live file line, the moment it was read, not the moment the event happened |
| `parsed` | the time written in the line, read by the agent |
| `late` | a server line the agent sent late, with the time it carried |
| `journal` | the journal’s own entry time |
| `event_log` | the Windows Event Log’s own record time |
| `sender` | the time a log source wrote on its line |
| `received` | the moment WatchTower15 received a log source’s line, because the source sent no time, an unreadable one or one in the future |

Treat `time_source` as a clue, not a guarantee: a `read` line may have happened well before the agent read it. The search windows count the millisecond, so `from=…08.900Z&to=…09.000Z` returns the lines of that 100 ms and no others.

## Message patterns

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs/groups \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d by=pattern \
  -d 'source_uuids[]=<server uuid>' \
  -d period=1h \
  -d limit=10
```

`by=pattern` groups the lines by their message with the numbers, addresses, times and ids replaced by placeholders (`<N>`, `<IP4>`, `<UUID>`, `<TIME>`), so ten thousand lines that differ only in a number are one group: a count of **kinds of message** rather than lines. The groups come largest first, up to 100 (`limit` as for any `by`), and a pattern is cut at 1,000 characters. The filters are the search’s. The filter `levels[]=unknown` selects the lines that have **no level**, and `GET /v1/logs/facets` answers `unlevelled_count` for them, which its `levels` leave out.

## Searching with several terms

`search` is one plain, case-sensitive text the line contains. A search can instead hold **up to 10 terms**, each a text, a whole word or a regular expression, each one the line must match (**include**) or must not match (**exclude**). Every list is a plain list of strings, `name[]=value` once per value, on `GET /v1/logs`, `/v1/logs/facets`, `/v1/logs/histogram` and `/v1/logs/groups` alike:

| Parameter | What it does |
| --- | --- |
| `include[]` | Lines that contain each of these texts, read as plain text. |
| `include_words[]` | Lines that contain each of these whole words. |
| `include_regex[]` | Lines that match each of these RE2 patterns. |
| `exclude[]` | Lines that contain **none** of these texts: up to 5, each up to 200 characters. |
| `exclude_words[]` | Lines that contain none of these whole words. |
| `exclude_regex[]` | Lines that match none of these RE2 patterns. |
| `match` | `all` (the default): a line matches every included term; `any`: at least one. The excluded terms always apply. |
| `search_case` | `sensitive` (the default) or `insensitive`: the case of **every** term, `search` included. |
| `search_mode` | `substring` (the default) or `regex`: how `search` itself is read. |

`search` stays a term like the others: one more included text (or pattern, with `search_mode=regex`), so `search=a&match=any` is a valid call. A search whose terms are all excluded matches every line none of them matches.

Leave the smoke tests’ lines out of the errors:

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d 'include[]=production.ERROR' \
  -d 'exclude[]=wt15-smoke-test' \
  -d period=1h
```

Lines that hold either of two errors, whatever the case:

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs/facets \
  -H "Authorization: Bearer $WT15_API_KEY" \
  --data-urlencode 'include[]=cURL error 28' \
  -d 'include_words[]=timeout' \
  -d match=any \
  -d search_case=insensitive \
  -d period=1h
```

Access lines with a 5xx status, by a pattern, without the health checks:

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d 'streams[]=access.log' \
  --data-urlencode 'include_regex[]=HTTP/1\.1" 5\d\d ' \
  -d 'exclude_words[]=health' \
  -d per_page=500
```

**The limits.** A search holds at most **10 terms**, `search`, `exclude[]` and every list counted together, and at most **5 regular expressions** (`include_regex[]`, `exclude_regex[]` and a `search` with `search_mode=regex`). Each text is 1 to 500 characters, except `exclude[]`’s, which are at most 200. Over a limit the answer is `422 invalid_request` on the first list sent. `match` with no included term at all (no `search`, `include[]`, `include_words[]` or `include_regex[]`) is `422` on `match`.

**Whole words.** A whole word is bounded with RE2’s `\b` at each end whose character is a letter, a digit or an underscore, and only there: `denied!` is bounded before the `d` only, so `undenied!` does not match and `denied!!` does; `-x` is bounded after the `x` only. RE2’s `\b` knows **ASCII letters only**, so `café` is bounded at `caf`. A word with no letter, digit or underscore (`--`) is refused with `422`.

**Patterns.** A pattern is an **RE2** pattern, the store’s own: lookahead and lookbehind (`(?=`, `(?!`, `(?<=`, `(?<!`), backreferences (`\1`), possessive quantifiers (`a++`) and atomic groups (`(?>`) do not exist in it. A pattern that uses one, or that is not a valid expression, answers `422 invalid_request` on its own entry (`include_regex.0`, `exclude_regex.2`, or `search`). A pattern that passes these checks and that the store still cannot run answers `422` on `search`, since the store does not say which term it refused; never a server error.

**Case, per term.** `search_case` sets the case of every term. To ignore case in **one** term only, write it as a pattern that starts with `(?i)`: `include_regex[]=(?i)timeout` beside case-sensitive texts. A pattern that already starts with `(?i)` is used as written.

**Long searches.** Every term travels in the request’s URL, and a request line over **8 KB** is refused by the server with `414 URI Too Long` before the API reads it. Ten terms of 500 characters, URL-encoded, can pass that: keep long texts short, or split the search.

## Around a line

```bash
curl -sS -G https://rest.watchtower15.com/v1/logs/context \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d uuid=$LINE_UUID \
  -d at=$LINE_LOGGED_AT \
  -d before=5 \
  -d after=5
```

`uuid` is the `uuid` of a line the search returned, and `at` its `logged_at` (epoch milliseconds): the time narrows the lookup of the line. `before` and `after` count lines: 0 to 100, **20 each** by default.

```json
{
  "data": {
    "items": [
      { "uuid": "<newer line>", "logged_at": 1795000000900, "level": "error", "stream": "journald", "message": "…", "source": { "type": "server", "uuid": "<server uuid>", "name": "web-1" }, "unit": "cloudflared.service", "identifier": "cloudflared", "http": null, "is_anchor": false },
      { "uuid": "<the line>", "logged_at": 1795000000500, "level": "error", "stream": "journald", "message": "…", "source": { "type": "server", "uuid": "<server uuid>", "name": "web-1" }, "unit": "cloudflared.service", "identifier": "cloudflared", "http": null, "is_anchor": true }
    ],
    "has_newer_more": true,
    "has_older_more": true
  }
}
```

-   The lines are of the **same source and stream** as the line asked for, **newest first**: the newer ones, the line itself (`is_anchor` is `true` on it and on no other), then the older ones. Each is shaped as a search item.
-   `has_newer_more` and `has_older_more` say whether more lines exist beyond those returned.
-   A line that is not found, or that is not one of your server or log source lines, is `422 invalid_request` on `uuid`. The answer holds no file path: a file’s path is on the server and is not returned.

## Journal units

A line a server’s journal produced carries two more fields on every search and context item, **`unit`** (the systemd unit, such as `cloudflared.service`) and **`identifier`** (the syslog identifier, such as `sshd`). Both are `null` for a line that did not come from a journal, and for a line from an agent too old to report them: the fields appear once the agent is updated, and stored lines never gain them.

`units[]` and `identifiers[]` filter on them, exactly: up to 20 values each. `GET /v1/logs/groups?by=unit` counts a window’s lines by unit.

A journal line’s **level** follows the line’s own text and, when the text says nothing, the journal’s priority (`emergency` to `debug`), except for a service’s output to standard output or error, which the journal rates `info` by default: that default says nothing about the line, so such a line keeps no level unless the service sets one.

### From an incident on a unit to its logs

An incident on a systemd unit keeps the state the unit was in when it opened: `unit_state` on `GET /v1/incidents`, `GET /v1/incidents/{incident_uuid}` and `GET /v1/servers/{server_uuid}/incidents` (`active_state`, `sub_state`, `result`, `exit_code`, and `state_changed_at`, `active_entered_at` and `inventory_at` in epoch milliseconds; null for any other incident). To read what the unit logged around it:

1.  Read the incident: its `server.uuid`, its `entity.name` (the unit, such as `nginx.service`) and `triggered_at`.
2.  Search the server’s logs for that unit in a window around the trigger: `GET /v1/logs?source_uuids[]=<server uuid>&units[]=nginx.service&from=<triggered_at − 15 minutes>&to=<triggered_at + 15 minutes>`.
3.  Open one line’s neighbours with `GET /v1/logs/context` when a line is the one you want.

`units[]` matches the lines an agent of version 1.1.87 or later reported; older lines carry no unit.

## Web access fields

The backend can read a web server’s access log lines into fields, for **server log files only**. A log file entry of a server profile takes an optional `format`:

| `format` | Reads |
| --- | --- |
| `nginx_combined` | nginx’s combined format, `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"`; whatever follows it is ignored. No duration. |
| `wt15_timed` | That prefix, then three quoted client-address fields and `$request_time`; the duration is `$request_time`. |
| `wt15_relay` | A reverse proxy’s format, `$msec $status $request_method $remote_addr $realip_remote_addr $request_time "$upstream_status" "$upstream_response_time" $request_length $bytes_sent "$uri"`. Only this format has the upstream status. |

Set it with the profile’s update, in the entry: `{"source_type": "file", "path": "/var/log/nginx/access.log", "format": "wt15_timed"}`. `format` is refused (`422`) on a `journald` entry and for any other value; an update that leaves it out of an entry **keeps** the entry’s current `format`, and `"format": null` clears it. A profile read shows it (null when none).

A parsed line carries **`http`** on its search and context items: `status` (an integer), `method`, `path` (the query string is dropped, so a token in a URL is never stored), `upstream_status` (as the web server wrote it: `502, 502` after a retry; null when there was none and for every format but `wt15_relay`) and `duration_ms` (null for `nginx_combined`). It is `null` on every other line.

-   `http_statuses[]` filters on the status: a code (`502`) or a class (`5xx`), up to 10. `GET /v1/logs/groups?by=http_status` and `by=http_path` count by them.
-   A parsed line that the agent sent with no level gets one from its status: `error` for a 5xx, `warning` for a 4xx, `info` otherwise.
-   **Only lines collected after the format is set are parsed**: stored lines never gain the fields.

### When a file is not parsed

A file is parsed only when **exactly one** file entry of the profile names it and **no folder entry also accepts it** by its file name. A second file entry for the same file name (`/var/log/nginx/access.log` and `/var/log/apache2/access.log` both ship as `access.log`), or a folder entry such as “all `.log` files” of a folder that holds the file, turns the parsing off for it, and nothing in the API says so: the lines simply have no `http`. Log sources are never parsed, and a line that does not match its format keeps its record as it was.

## Levels from the agent

The agent reads a severity word only when it stands alone near the start of the line (`[error]`, `production.ERROR:`, `ERR` of a Go logger), or as a `level=` or `"level":` field anywhere. A URL such as `/api/error-page` or a query such as `?q=error` does not make a line an `error`.

## Bigger pages

`GET /v1/logs` takes `per_page` up to **2,000** (100 by default; `limit` is the same): an hour of one busy server in a few calls. A page of very long lines stops once its lines’ text passes 10 MB, so it can hold fewer than `per_page` lines; `next_cursor` then continues after the last line returned, as on every page of a log search, which pages by time ([pagination](https://developer.watchtower15.com/api/pagination/)). The 60 calls a minute of a key and the 10-second read budget are unchanged.

Source: https://developer.watchtower15.com/api/investigating-logs/
