# Log stream

> A live tail you can actually read: scroll up and it lets go while a "↓ 128 new" pill counts what arrives, with pause, level counts, search and lines that open into their fields.

Most log views either yank you back to the bottom every time a line arrives, or stop following and never tell you what you missed. This one renders new lines once per frame however fast they come and stays pinned to the newest line until you scroll up. Then it lets go: lines keep arriving underneath, and a "↓ 128 new" pill rises with its count rolling; one tap takes you back down. Live → Paused holds lines back without losing them ("Paused · 42 waiting"), and Live plays them in. The level chips count as lines arrive and hide what you don't need; search marks every match and "/" focuses it. A line with data opens into its fields under it, with Copy. Reach the top and older lines load in above without moving the line you're reading.

## Install

```bash
npx shadcn@latest add @hairline/log-stream
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`
- Docs and live preview: https://hairlineui.com/ui/log-stream
- Registry item (source): https://hairlineui.com/r/log-stream.json

## Usage

```tsx
import { LogStream } from "@/components/log-stream";

<LogStream
  subscribe={(push) => {
    const source = new EventSource("/api/logs/stream");
    source.onmessage = (e) => push(JSON.parse(e.data));
    return () => source.close();
  }}
/>
```

## A live tail over server-sent events

A route streams new log lines as server-sent events; the browser's EventSource reconnects by itself. loadEarlier pages back through the same table by time. Feeding it from Axiom, Better Stack, Supabase Realtime or a WebSocket is the same subscribe function.

```tsx
// app/api/logs/stream/route.ts
import { db } from "@/lib/db";

export async function GET(req: Request) {
  const project = await requireProject(req);
  let since = new Date();
  const encoder = new TextEncoder();
  const stream = new ReadableStream({
    async start(controller) {
      while (!req.signal.aborted) {
        const rows = await db.log.findMany({ where: { projectId: project.id, at: { gt: since } }, orderBy: { at: "asc" }, take: 500 });
        if (rows.length) {
          since = rows[rows.length - 1].at;
          controller.enqueue(encoder.encode(`data: ${JSON.stringify(rows)}\n\n`));
        }
        await new Promise((r) => setTimeout(r, 1000));
      }
      controller.close();
    },
  });
  return new Response(stream, { headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" } });
}

// app/logs/page.tsx
"use client";
import { LogStream, type LogLine } from "@/components/log-stream";

export function Logs({ recent }: { recent: LogLine[] }) {
  return (
    <LogStream
      title="Production logs"
      defaultValue={recent}
      subscribe={(push) => {
        const source = new EventSource("/api/logs/stream");
        source.onmessage = (e) => push(JSON.parse(e.data));
        return () => source.close();
      }}
      loadEarlier={async (before) => {
        const res = await fetch(`/api/logs?before=${encodeURIComponent(String(before.at))}&limit=200`);
        return res.json();
      }}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `subscribe` | `(push) => () => void` | – | Starts the tail. Call push with one line or many, as often as you like; return a function that stops it. |
| `defaultValue` | `LogLine[]` | – | Lines to start with, oldest first: { id, at, level, message, source?, data? }. |
| `loadEarlier` | `(before: LogLine) => Promise<LogLine[]>` | – | Loads lines older than the first one, oldest first. Return an empty list when there are no more. |
| `max` | `number` | `1000` | Lines kept while the view is pinned to the bottom. While you're reading further up nothing is dropped. |
| `height` | `number \| string` | `420` | The scrolling area's height. |
| `title` | `string` | `"Logs"` | Names the list and the search field for screen readers. |

## States

- **tail**: Pushed lines are collected and rendered once per animation frame. Up to 12 new lines fade in from a 2px blur (260ms); bigger bursts simply appear. Each commit ends on the newest line while the view is pinned.
- **reading**: A scroll you make (wheel, touch, keys or the scrollbar) lets go; content changing size never does. New lines keep arriving and the pill rises 10px into place (420ms) with its count rolling. Tapping it scrolls down smoothly and pins the view again; so does scrolling back to the bottom.
- **paused**: The green pinging dot turns grey and Live morphs to Paused; lines are held, and "· 42 waiting" opens beside it, rolling. Live plays them in as one batch.
- **filter**: Level chips count every line in memory (the numbers roll) and hide their level when switched off. Search marks every match in the message and keeps only lines that match the message or source.
- **open**: A line with data is a button; it opens by height in 320ms into a hairline box of its fields (numbers in the primary colour, strings quoted) with a Copy that morphs to Copied.
- **earlier**: With loadEarlier, reaching the top loads older lines above; the scroll position is corrected in the same frame, so the line you were reading doesn't move. An empty answer says "Start of the log".

## What it handles

Most log views either pull you back to the bottom every time a line arrives, or stop following and never say what you missed. This stays pinned until you scroll up, then counts what arrived.

- **Pinned until you scroll up**: New lines are drawn once per frame however fast they arrive, and only your own scroll lets go of the bottom.
- **It counts what you missed**: A “↓ 128 new” pill rises with its count rolling, and one tap takes you back down.
- **Pause without losing lines**: Live becomes Paused and holds lines back (“Paused · 42 waiting”). Live plays them in again.
- **Levels and search**: Level chips count lines as they arrive, search marks every match, and / focuses it.
- **Lines open into their fields**: A line with data opens under itself into its fields, with a Copy button.
- **Older lines on demand**: Reaching the top loads earlier lines above without moving the line you are reading.

## Replaces

a log viewer, auto-scroll logic, pause and resume, level filters, log search, a “new logs” pill

## Where it fits

- **Developer platforms**: Runtime and build logs for deployed apps.
- **Audit and event logs**: A live feed of what is happening in an account or workspace.
- **Background jobs and workflows**: Follow a run while it executes.
- **API and agent monitoring**: Request logs and agent tool calls as they arrive.

## Notes

- The scrolling area is focusable and scrolls with the keyboard. Lines are a list; lines with data are buttons with aria-expanded. The list is not a live region, so a busy tail never floods a screen reader; pausing and resuming are announced instead.
- "/" focuses search while focus is inside the stream; Escape clears it.
- Rows use content-visibility, so a thousand lines lay out only what's on screen, and each row is memoised on its own line: a new batch renders only the new rows.
- Levels are told by word as well as colour: "err" and "warn" in red and amber, with a 2px tick at the line's edge.
- Reduced motion drops the fades, the pill's rise, the ping and the smooth scroll; lines still arrive and the counts still change.

License: MIT (https://hairlineui.com/license#components)
