# Device log

`device.logs`: the device log since the attach, and the steps that tie your own record to it.
Guide: [Device logs](../../guide/logs.md).

## <kbd>class</kbd> `DeviceLog`
The device log since the attachment opened (``Device.logs``): every logcat line of the main, system and crash buffers, with the actions Tap ran and the markers a test wrote in between, in the order they happened. 

The server captures it from the moment the device is attached, unless the attachment turned that off (``attach_device(capture_logs=False)``; every call here then raises ``ServerError`` with ``FailureReason.DAEMON_PRECONDITION``). It reads the log while the device is attached; the server also keeps it after detach and restart, read with ``client.history``. Secrets are replaced by ``<redacted>`` before the server stores a line and again on read: see ``redact``. 

Typical use, the app's warnings during one step:
``` 

     device.logs.mark("checkout")      pay.tap()      errors = device.logs.read(since="checkout", packages=["com.shop"], min_priority="warn")      assert not errors.lines, errors.text 

### <kbd>method</kbd> `DeviceLog.__init__`

```python
__init__(device: 'Device')
```

---

### <kbd>method</kbd> `DeviceLog.follow`

```python
follow(
    since: 'str | int | LogEntry | None' = None,
    tail: 'int | None' = None,
    since_last_action: 'bool' = False,
    packages: 'Iterable[str]' = (),
    min_priority: 'LogPriority | str | None' = None,
    tags: 'Iterable[str]' = (),
    grep: 'str | None' = None,
    actions: 'bool' = True
) → LogFollower
```

The entries from the starting point and then every new one as it is logged, until the returned ``LogFollower`` is closed or the device detached. Same starting points and filters as ``read``, but the entries before now come from the newest 16 MiB the server keeps in memory (``tail=1`` starts near now); ``client.history`` reads older ones. Read it on its own thread, or to wait for a line:
``` 

     with device.logs.follow(since="checkout", tags=["Payments"]) as log:          done = next(e for e in log if e.line and "settled" in e.line.message) 

```
A follower that reads too slowly is ended with ``ServerError`` (``RESOURCE_EXHAUSTED``); follow again ``since`` the last entry it got. 

---

### <kbd>method</kbd> `DeviceLog.mark`

```python
mark(name: 'str', timeout: 'float' = 30.0) → int
```

Writes a named marker into the log, at the time the server gets the call, and returns its cursor. Read from it with ``read(since=name)``; a later marker with the same name replaces it as the starting point. ``name`` is 1 to 200 characters on one line. The server answers only once it has stored the marker. 

---

### <kbd>method</kbd> `DeviceLog.read`

```python
read(
    since: 'str | int | LogEntry | None' = None,
    tail: 'int | None' = None,
    since_last_action: 'bool' = False,
    packages: 'Iterable[str]' = (),
    min_priority: 'LogPriority | str | None' = None,
    tags: 'Iterable[str]' = (),
    grep: 'str | None' = None,
    actions: 'bool' = True,
    step: 'str | None' = None,
    until: 'str | int | LogEntry | None' = None,
    timeout: 'float' = 30.0
) → LogEntries
```

The entries so far, oldest first. 

Where to start (at most one; none = the oldest entry the server still has): ``since`` a marker's name (from the newest marker with that name, raising ``ServerError`` with ``FailureReason.UNKNOWN_LOG_MARKER`` when there is none), a cursor or a ``LogEntry`` (what came after it); ``tail`` the newest N entries that pass the filters; ``since_last_action`` from the start of the newest action. 

Filters, applied to logcat lines only (markers, actions and gaps always pass): ``packages`` keeps the lines of these apps, ``min_priority`` lines at least this severe (``LogPriority`` or a name such as ``"warn"``), ``tags`` lines with one of these tags, ``grep`` lines whose message contains a match of this regular expression (Java syntax, tested after redaction). ``actions=False`` leaves Tap's action entries out. 

``step`` narrows the read to one step (the id ``tap_e2e.step`` yielded): the entries from the step's first action to its last, nothing when it ran no action. 

``until`` ends the read there instead of now, that entry included: a marker's name (the newest marker with it among the entries read), a cursor or a ``LogEntry``. A marker that is not among them, or a cursor before the first entry read, raises ``ValueError``; so ``read(since="checkout", until="paid")`` is the stretch between two markers. 

---

### <kbd>method</kbd> `DeviceLog.redact`

```python
redact(*patterns: 'str', timeout: 'float' = 30.0) → None
```

Adds regular expressions (Java syntax) whose matches are replaced by ``<redacted>`` in everything read from now on, lines already captured included: by ``read`` and ``follow``, ``Device.device_logs()`` and the failure artifacts. A match is replaced whole; a pattern with a capturing group has only its first group replaced (``r"session=(\w+)"`` keeps ``session=``). 

Redacted without any call: ``Authorization`` / ``Cookie`` style headers, ``password=`` / ``token=`` style values, bearer tokens, JWTs, and the text of every ``sensitive`` ``set_text`` / ``type_text`` on this device (literals of four characters or longer). At most 64 patterns per attachment. A new pattern also rewrites what the server has already stored of this attachment, its calls and crashes included (a failed rewrite is an error); it cannot erase copies you saved or exported before. 

## <kbd>class</kbd> `LogEntries`
What ``DeviceLog.read`` returns: the entries, oldest first. 

---

#### <kbd>property</kbd> LogEntries.lines

The logcat lines among the entries. 

---

#### <kbd>property</kbd> LogEntries.messages

The message of each logcat line. 

---

#### <kbd>property</kbd> LogEntries.text

The entries as text, one per line. 

## <kbd>class</kbd> `LogEntry`
One entry of the device log: exactly one of ``line``, ``marker``, ``action`` and ``gap`` is set. ``str(entry)`` is the entry as one line of text, led by ``at`` in the local time zone (``MM-DD HH:MM:SS.mmm``, the clock of every other Tap record; the device's own clock can be far off); a line then has uid, pid, tid, priority, tag and message, like ``logcat -v threadtime``. 

### <kbd>method</kbd> `LogEntry.__init__`

```python
__init__(
    cursor: 'int',
    at: 'float',
    line: 'LogLine | None' = None,
    marker: 'str | None' = None,
    action: 'LogAction | None' = None,
    gap: 'str | None' = None,
    host_monotonic_ns: 'int' = 0
) → None
```

---

#### <kbd>property</kbd> LogEntry.summary

A marker, gap or action as ``── marker NAME``, ``── gap WHY`` or ``── tap  TARGET  → ok  243 ms``; a line as ``P Tag: message``. No time. 

## <kbd>class</kbd> `LogLine`
One logcat line. A message of several lines arrives as several entries. 

### <kbd>method</kbd> `LogLine.__init__`

```python
__init__(
    device_time: 'float',
    uid: 'int | None',
    pid: 'int',
    tid: 'int',
    priority: 'LogPriority | None',
    tag: 'str',
    message: 'str',
    packages: 'tuple[str, ...]' = (),
    packages_observed_at: 'float | None' = None
) → None
```

## <kbd>class</kbd> `LogAction`
Something Tap did on the device: one entry when it starts and one when it finishes. 

A command the driver runs (a tap, typed text, a wait) is logged by the driver itself, so its entries are ordered exactly among the app's lines; queries (``exists``, ``count``, a snapshot) are left out. A call the host runs over ADB (an app launch, a device setting) is written by the server when the call starts and ends. 

### <kbd>method</kbd> `LogAction.__init__`

```python
__init__(
    operation: 'str',
    target: 'str | None',
    finished: 'bool',
    outcome: 'str | None',
    duration: 'float | None',
    request_id: 'int | None',
    step: 'str | None' = None
) → None
```

## <kbd>class</kbd> `LogPriority`
A logcat priority; a higher value is more severe. 

## <kbd>class</kbd> `LogFollower`
The live device log (``DeviceLog.follow``): iterate it for entries as the server gets them. Iteration blocks until the next entry and ends when the device is detached or ``close()`` is called (from any thread); use it as a context manager to close it. 

### <kbd>method</kbd> `LogFollower.__init__`

```python
__init__(call, serial: 'str')
```

---

### <kbd>method</kbd> `LogFollower.close`

```python
close() → None
```

Stops following; an iteration in progress ends. 

## History and recordings

`client.history`: the server's kept history, also after the connection ended, and named
recordings of it. Guide: [History after the script](../../guide/logs.md#history-after-the-script).

## <kbd>class</kbd> `History`
The server's history (``client.history``). It needs no live connection or attachment: read it from a test's teardown, after a crash or from another process. 

Typical use, what one test did, with the app's warnings:
``` 

     page = client.history.read(connections=[connection], since_marker="test checkout",                                 logs=True, min_priority="warn")      print(page.text) 

```
The server keeps history under its retention policy; keep a range past it with ``start_recording`` / ``stop_recording``. 

### <kbd>method</kbd> `History.__init__`

```python
__init__(client: 'TapClient')
```

---

### <kbd>method</kbd> `History.delete_recording`

```python
delete_recording(
    recording: 'HistoryRecording | str',
    timeout: 'float' = 30.0
) → None
```

Deletes a stopped recording; retention may then remove its entries. Files saved separately are not touched. 

---

### <kbd>method</kbd> `History.pages`

```python
pages(**read_arguments: 'Any') → Iterator[HistoryPage]
```

Every page of a ``read`` with these arguments, in order. 

---

### <kbd>method</kbd> `History.read`

```python
read(
    connections: 'Iterable[TapConnection | str]' = (),
    sources: 'Iterable[HistorySource | str]' = (),
    serials: 'Iterable[str]' = (),
    recording: 'HistoryRecording | str | None' = None,
    since: 'float | datetime.datetime | str | None' = None,
    until: 'float | datetime.datetime | str | None' = None,
    since_marker: 'str | None' = None,
    until_marker: 'str | None' = None,
    step: 'str | None' = None,
    logs: 'bool' = False,
    packages: 'Iterable[str]' = (),
    min_priority: 'LogPriority | str | None' = None,
    tags: 'Iterable[str]' = (),
    grep: 'str | None' = None,
    actions: 'bool' = True,
    driver_lines: 'bool' = False,
    newest_first: 'bool' = False,
    limit: 'int' = 200,
    timeout: 'float' = 30.0
) → HistoryPage
```

One page of history, oldest first (``newest_first``: the most recent ``limit`` entries of the range first). 

Selection (each given one must match): ``connections`` (``TapConnection`` or id), ``sources``, device ``serials`` (their logs, calls and deaths) and a ``recording``'s range. Nothing selects all the server keeps. 

``since`` / ``until``: a time, as Unix seconds, an aware ``datetime`` or an ISO 8601 string with its offset (``"2026-11-21T12:00:00Z"``); ``since`` is included, ``until`` not. ``since_marker`` / ``until_marker`` instead: from or through the newest marker with that name on each selected device, the marker included (``ServerError`` ``UNKNOWN_LOG_MARKER`` when none). Select ``connections`` too to leave out other connections' markers of the same name. Without an end the read ends when it starts, so its pages never move. 

``step`` keeps what happened in that step (``tap_e2e.step``): its calls, the deaths and gaps between the start of its first call and the end of its last, and each device log from its first action to its last; a name used again after another step adds a second such stretch. The bounds still apply. 

Calls, deaths and gaps are always read; ``logs=True`` adds the device logs, which ``packages``, ``min_priority``, ``tags``, ``grep`` (Java regex), ``actions`` and ``driver_lines`` filter (they never drop gaps, calls or deaths). 

---

### <kbd>method</kbd> `History.recordings`

```python
recordings(
    connections: 'Iterable[TapConnection | str]' = (),
    timeout: 'float' = 30.0
) → tuple[HistoryRecording, ...]
```

The recordings of these connections (all when none is given), stopped or not. 

---

### <kbd>method</kbd> `History.resume`

```python
resume(cursor: 'str', limit: 'int' = 200, timeout: 'float' = 30.0) → HistoryPage
```

The page after ``cursor`` (a ``HistoryPage.next_cursor``), of the read it came from: same selection, bounds, filters and order. A cursor keeps working in another process and after a server restart, not across server versions. 

---

### <kbd>method</kbd> `History.sources`

```python
sources(
    connections: 'Iterable[TapConnection | str]' = (),
    sources: 'Iterable[HistorySource | str]' = (),
    serials: 'Iterable[str]' = (),
    recording: 'HistoryRecording | str | None' = None,
    timeout: 'float' = 30.0
) → tuple[HistorySource, ...]
```

The kept sources, of ended connections and detached devices too, selected as in ``read``. 

---

### <kbd>method</kbd> `History.start_recording`

```python
start_recording(
    connection: 'TapConnection | str',
    name: 'str',
    timeout: 'float' = 30.0
) → HistoryRecording
```

Keeps ``connection``'s history from now on, devices it attaches later included, until the recording is stopped and deleted. It stops by itself when the connection closes. A device whose capture already failed is included with an ``INCOMPLETE`` issue. ``name``: 1 to 80 ASCII letters, digits, dots, dashes and underscores, unique among the connection's recordings. 

---

### <kbd>method</kbd> `History.stop_recording`

```python
stop_recording(
    recording: 'HistoryRecording | str',
    timeout: 'float' = 30.0
) → HistoryRecording
```

Ends the recording's range, also after its connection ended; stopping it again returns it unchanged. Calls still running then are a gap at its end. 

## <kbd>class</kbd> `HistoryPage`
One page of ``History.read``: its entries in the order read, the sources it covered and the evidence that is missing. Iterating it yields the entries; ``next_page()`` reads the next page. As an ``Artifact`` its ``bytes`` are ``to_json()``. 

### <kbd>method</kbd> `HistoryPage.__init__`

```python
__init__(
    entries: 'tuple[HistoryEntry, ...]',
    sources: 'tuple[HistorySource, ...]',
    issues: 'tuple[HistoryIssue, ...]',
    next_cursor: 'str | None',
    through: 'float',
    _history: 'History | None' = None,
    _limit: 'int' = 200,
    _timeout: 'float' = 30.0
) → None
```

---

#### <kbd>property</kbd> HistoryPage.bytes

---

#### <kbd>property</kbd> HistoryPage.text

The entries one per line (``HistoryEntry.text``), then a ``WARNING:`` line per issue. 

---

### <kbd>method</kbd> `HistoryPage.next_page`

```python
next_page(limit: 'int | None' = None) → HistoryPage | None
```

The next page of the same read, or None after the last. 

---

### <kbd>method</kbd> `HistoryPage.render_text`

```python
render_text(details: 'bool' = False) → str
```

``text``; with ``details`` crash stack traces too. 

---

### <kbd>method</kbd> `HistoryPage.to_dict`

```python
to_dict() → dict[str, Any]
```

JSON-ready, as ``tap-history/2``: ``entries``, ``sources``, ``issues``, ``through``, ``next_cursor``. 

---

### <kbd>method</kbd> `HistoryPage.to_json`

```python
to_json() → str
```

## <kbd>class</kbd> `HistoryEntry`
One thing that happened: exactly one of ``line``, ``action``, ``marker`` (a device log), ``call``, ``death`` (a connection) and ``gap`` (either) is set. 

### <kbd>method</kbd> `HistoryEntry.__init__`

```python
__init__(
    source_id: 'str',
    sequence: 'int',
    at: 'float',
    monotonic_ns: 'int',
    serial: 'str | None',
    attached_device_id: 'str | None',
    line: 'LogLine | None' = None,
    action: 'LogAction | None' = None,
    marker: 'str | None' = None,
    call: 'LoggedEvent | None' = None,
    death: 'AppDeath | None' = None,
    gap: 'str | None' = None
) → None
```

---

#### <kbd>property</kbd> HistoryEntry.kind

``line``, ``action``, ``marker``, ``call``, ``death`` or ``gap``. 

---

#### <kbd>property</kbd> HistoryEntry.summary

The entry as one line without its time or device: ``W Tag: message``, ``── tap  …``, ``── marker NAME``, ``call exists text("Pay") → ok``, ``CRASH com.shop (pid 4) …``, ``GAP: why``. 

---

### <kbd>method</kbd> `HistoryEntry.text`

```python
text(details: 'bool' = False) → str
```

``AT  SERIAL  SUMMARY``; with ``details`` a crash's stack trace follows, one ``|`` line each. 

---

### <kbd>method</kbd> `HistoryEntry.to_dict`

```python
to_dict() → dict[str, Any]
```

JSON-ready: identity, times (``at`` ISO 8601 UTC, ``monotonic_ns`` a string) and the entry's own fields under its ``kind``. 

## <kbd>class</kbd> `HistorySource`
Something the server keeps history of: one attachment's device log (``attached_device_id`` and ``serial`` set), or one connection's calls and the crashes and ANRs on its devices (both None). 

### <kbd>method</kbd> `HistorySource.__init__`

```python
__init__(
    id: 'str',
    connection_id: 'str',
    attached_device_id: 'str | None',
    serial: 'str | None',
    clock_id: 'str',
    closed: 'bool',
    last_sequence: 'int',
    retained_bytes: 'int',
    issues: 'tuple[HistoryIssue, ...]' = ()
) → None
```

---

#### <kbd>property</kbd> HistorySource.is_device_log

---

### <kbd>method</kbd> `HistorySource.to_dict`

```python
to_dict() → dict[str, Any]
```

## <kbd>class</kbd> `HistoryIssue`
Evidence that is missing without a place in time. Where capture stopped and resumed is a ``gap`` entry instead. 

### <kbd>method</kbd> `HistoryIssue.__init__`

```python
__init__(
    kind: 'HistoryIssueKind',
    message: 'str',
    source_id: 'str | None',
    missing_entries: 'int' = 0
) → None
```

---

### <kbd>method</kbd> `HistoryIssue.to_dict`

```python
to_dict() → dict[str, Any]
```

## <kbd>class</kbd> `HistoryIssueKind`
What evidence is missing (``HistoryIssue.kind``). 

## <kbd>class</kbd> `HistoryRecording`
A range of one connection's history the server keeps past its retention policy, until ``History.delete_recording``. It covers the connection's devices, also those attached after it started. 

### <kbd>method</kbd> `HistoryRecording.__init__`

```python
__init__(
    id: 'str',
    connection_id: 'str',
    name: 'str',
    started_at: 'float',
    stopped_at: 'float | None',
    spans: 'tuple[HistorySpan, ...]',
    issues: 'tuple[HistoryIssue, ...]',
    retained_bytes: 'int'
) → None
```

---

#### <kbd>property</kbd> HistoryRecording.active

Still recording: not stopped yet. 

---

### <kbd>method</kbd> `HistoryRecording.to_dict`

```python
to_dict() → dict[str, Any]
```

## <kbd>class</kbd> `HistorySpan`
The part of one source a recording keeps: after ``after_sequence``, through ``through_sequence`` (None while recording). 

### <kbd>method</kbd> `HistorySpan.__init__`

```python
__init__(
    source_id: 'str',
    after_sequence: 'int',
    through_sequence: 'int | None'
) → None
```

## Steps

## <kbd>function</kbd> `step`

```python
step(step_id: 'str | None' = None) → Iterator[str]
```

Runs the block as one step and yields the step's id. 

Every call made to the server inside the block, on this thread, carries the id. The server copies it to the actions the calls cause in the device log (``LogAction.step``) and to their events (``LoggedEvent.step``), so a caller that keeps its own record of steps can find what each one did on the device:
``` 

     with tap_e2e.step() as checkout:          cart.tap()          pay.tap()      log = device.logs.read(step=checkout)   # from its first action to its last 

```
``step_id`` is 1 to 64 of ``A-Z a-z 0-9 . _ -``; without one a new id is made. A step inside a step replaces it until the inner block ends. 

## <kbd>function</kbd> `current_step`

```python
current_step() → str | None
```

The id of the step this thread (or task) is in, or None outside ``step()``. 

