A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's.
- 6.5 kB JS 4.9 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 14 tests
Live3 lines
import { Subscription, UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema, Stream } from "effect"import { LogViewer, type LogLine } from "./ui/uicontext/components/LogViewer.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "100%" } })const start = Date.parse("2026-12-05T09:14:00.000Z")// a deploy, written a step at a time; the times and steps are fixed so every visit reads the sameconst script: ReadonlyArray<Omit<LogLine, "id" | "time"> & { readonly after: number }> = [ { after: 0, level: "info", source: "deploy", message: "Deploy 3f9a2c to production started by Ada Okafor" }, { after: 220, level: "debug", source: "build", message: "Restoring cache key node-modules-8e1f… hit" }, { after: 1400, level: "info", source: "build", message: "Installing dependencies (1,204 packages)" }, { after: 9800, level: "info", source: "build", message: "Compiled 412 modules in 6.2s" }, { after: 10120, level: "warn", source: "build", message: "Bundle web/app.js is 812 KB, over the 750 KB budget" }, { after: 11800, level: "info", source: "migrate", message: "Running 2 migrations" }, { after: 12950, level: "info", source: "migrate", message: "0042_invoice_due_dates.sql applied in 1.1s" }, { after: 14010, level: "info", source: "migrate", message: "0043_drop_legacy_plans.sql applied in 0.2s" }, { after: 15200, level: "info", source: "rollout", message: "Shifting 10% of traffic to the new version" }, { after: 21300, level: "debug", source: "rollout", message: "Health check /healthz → 200 in 41ms (3 of 3 regions)" }, { after: 27400, level: "error", source: "api", message: "POST /v1/invoices/send 500: ledger timeout after 30000ms\n at LedgerClient.post (ledger.ts:88)\n at sendInvoice (invoices.ts:214)" }, { after: 27600, level: "warn", source: "rollout", message: "Error rate 0.8% on the new version, under the 2% limit; carrying on" }, { after: 33500, level: "info", source: "rollout", message: "Shifting 50% of traffic to the new version" }, { after: 45200, level: "info", source: "rollout", message: "Shifting 100% of traffic to the new version" }, { after: 46100, level: "info", source: "deploy", message: "Deploy 3f9a2c finished in 46.1s" },]const lineAt = (i: number): LogLine => { const step = script[i]! return { id: String(i), time: new Date(start + step.after).toISOString(), level: step.level, ...(step.source === undefined ? {} : { source: step.source }), message: step.message }}const Message = UiSchema.TaggedEnum({ Wrote: {} })export const DeployLog = Html.component({ name: "DeployLog", model: Schema.Struct({ count: Schema.Number }), message: Message,}).make({ init: () => ({ count: 3 }), // stands in for the server's stream: the next line, a beat later, until the deploy is done subscriptions: ($) => [$.model.count < script.length && Subscription.make("log-tick", Stream.tick("700 millis").pipe(Stream.drop(1), Stream.as(Message.Wrote())))], update: { Wrote: ($) => $.evolve({ count: Math.min(script.length, $.model.count + 1) }), }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( LogViewer(h, { key: "deploy", props: { label: "Deploy log", lines: Array.from({ length: model.count }, (_, i) => lineAt(i)), live: model.count < script.length, timeZone: "UTC", maxHeight: 300, }, }), ),})When to use
A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's. Use Terminal instead when people type commands, not only read lines as they arrive.
Usage
import { LogViewer, type LogLine } from "./ui/uicontext/components/LogViewer.ts"export const BuildLog = <M>(h: Html.H<M>, lines: ReadonlyArray<LogLine>, running: boolean) => LogViewer(h, { props: { label: "Build log", lines, live: running } })Examples
Finding the failures in a busy log
Type in the filter to narrow the lines and mark the words. The level buttons hide what's noise.
640 lines
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { LogViewer, type LogLine } from "./ui/uicontext/components/LogViewer.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ wrap: { inlineSize: "100%" } })const routes = ["GET /v1/projects", "GET /v1/issues?status=open", "POST /v1/comments", "PATCH /v1/issues/ENG-2391", "GET /v1/me"]const workers = ["api-1", "api-2", "worker-1"]const start = Date.parse("2026-12-05T14:00:00.000Z")// an hour of a server's lines, fixed so every visit reads the same: mostly requests, some noise, a few failuresconst lines: ReadonlyArray<LogLine> = Array.from({ length: 640 }, (_, i) => { const time = new Date(start + i * 5_413).toISOString() const source = workers[i % workers.length]! if (i % 97 === 41) return { id: String(i), time, level: "error", source, message: `POST /v1/comments 503: search index unavailable (attempt ${(i % 3) + 1} of 3)` } if (i % 53 === 7) return { id: String(i), time, level: "warn", source, message: `Slow query on issues (${820 + (i % 7) * 113}ms): SELECT … WHERE project_id = $1 ORDER BY updated_at` } if (i % 5 === 3) return { id: String(i), time, level: "debug", source, message: `cache ${i % 2 === 0 ? "hit" : "miss"} issues:list:${i % 11}` } const route = routes[i % routes.length]! return { id: String(i), time, level: "info", source, message: `${route} 200 in ${12 + (i * 7) % 180}ms` }})// a finished log: filter for "503" or "slow", hide Debug and Info to see only what went wrongexport const ServerLog = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( LogViewer(h, { key: "server", props: { label: "api server log", lines, timeZone: "UTC", maxHeight: 360 } }), )API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | What the log is, for screen readers: "Deploy log". |
linesrequired | LogLine[] | — | Every line so far, oldest first. |
live | boolean | — | Lines are still coming: a Live mark shows beside the count. |
timeZone | string | — | The reader's time zone for the times; the browser's by default. |
locale | string | — | For the counts; the browser's by default. |
limit | number | 2000 | The most lines drawn; 2000 by default. |
maxHeight | number | 420 | The log's tallest, in px, before it scrolls; 420 by default. |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/log-viewer