Skip to documentation content

Audit Log

Show who did what and when as a feed grouped by day, with each field change written out and the event's details one click away.

audit-log-demo

Overview

AuditLog lists events newest first, grouped under Today, Yesterday, and dated headings. Each event reads as a sentence, "Ana Ruiz changed the plan of Northwind Labs", with the time beside it. When an event changed fields, the change is written out under the sentence: "Plan: Team โ†’ Business". Facts about the event, such as the IP address or request id, sit behind a Details button.

Use it for a workspace's security log, a record's history beside its form, or a team's activity feed. People and activity-type filters narrow the list, and "Load older activity" fetches the next page.

AuditLog shows the entries you pass. It does not fetch or record anything, and it never decides what is worth logging.

Anatomy

AuditLog
  • HeaderTitle and event count, with the filters when they are on
    • People filterEveryone, or one actor
    • Activity filterAll activity, or one category
  • DayA heading, then that day's events
    • EventAvatar, sentence, and time
      • ChangesEach field's old and new value, or the items added and removed
      • DetailsA disclosure with the exact time and any facts you pass
  • Load moreFetches older events; shown while hasMore is true

Installing audit-log brings Avatar, Button, Menu, and Tooltip along as registry dependencies.

Behavior

Entries

Each entry has an actor, an action, an optional target, and a timestamp. The sentence is the actor's name, the action, and the target, so write the action as the words between them: "changed the plan of", "invited", "rotated the signing key for". A target with an href is a link.

Actors show their initials, or an image from avatarUrl. An actor with kind: "system" or kind: "api" shows an icon instead, so automated events are told apart from people at a glance.

Days and times

Entries are sorted newest first and grouped by calendar day. Today's events show relative time, such as "12 min ago", which refreshes each minute. Older events show the clock time, since the heading already names the day. Hovering a time shows the full date and time, and screen readers hear it in full.

Pass timeZone to group days in a fixed zone, such as "UTC" for a log that a whole company reads. Pass now when the times must not move, as in tests and screenshots. Without now, times appear once the log mounts in the browser, because a server does not know the reader's clock.

Changes

changes lists what an event changed. A value change has a label, before, and after, and reads "Plan: Team โ†’ Business". An empty old value reads "Not set". A list change has kind: "list" with added and removed items, each with a plus or minus.

The first three changes are shown. When there are more, a button shows the rest. Change the number with maxInlineChanges. A change's render, or renderValue on the log, formats values such as money.

Details

details is a list of { label, value, mono? }. They sit behind a Details button with the exact time, out of the way until someone investigates an event. Set mono for ids, addresses, and keys.

Filters

filterable shows two menus in the header. The people menu lists each actor once. The activity menu lists each entry's category, and is hidden when no entry has one. Filtering is done for you on the entries you passed; to filter on a server, control filter and fetch new entries in onFilterChange.

Paging

Pass hasMore and onLoadMore to show "Load older activity" at the bottom. When onLoadMore returns a promise, the button reads "Loadingโ€ฆ" until it settles. Append the new entries to entries.

Accessibility

The log is a region named by its title. Each day is a group named by its heading, and its events are an ordered list. The avatars are decorative, since each sentence starts with the actor's name.

A change is read as "Plan, was Team, now Business": the words are in the markup, and the strikethrough and arrow are decorative. Added and removed list items are read as "Added" and "Removed" before the item. The Details and more-changes buttons report whether they are expanded.

The event count in the header is a polite status region, so filtering announces how many events remain. Filter menus are named for what they filter and what is chosen, such as "People: Dee Park".

KeyAction
TabMove through filters, target links, and each event's buttons
Enter / SpaceOpen a filter menu, show more changes, or toggle details
โ†“ / โ†‘Move through a filter menu's choices

Installation

npx honestui@latest add audit-log

Usage

Map your stored events to entries, and append each page as it loads:

import { AuditLog, type AuditLogEntry } from "@/components/ui/audit-log/audit-log"

const entries: AuditLogEntry[] = events.map((event) => ({
  id: event.id,
  actor: { id: event.userId, name: event.userName },
  action: event.summary,
  target: { label: event.objectName, href: `/accounts/${event.objectId}` },
  timestamp: event.createdAt,
  category: event.area,
  changes: event.fields.map((field) => ({
    label: field.label,
    before: field.from,
    after: field.to,
  })),
  details: [{ label: "IP address", value: event.ip, mono: true }],
}))

<AuditLog
  entries={entries}
  filterable
  hasMore={page.hasMore}
  onLoadMore={() => fetchNextPage()}
/>

For one record's history, set the title and leave the filters off:

<AuditLog entries={history} labels={{ title: "Invoice INV-2041 history" }} />

Pass header={null} to remove the header, or your own element to replace it.

Don't do this

Logging secrets in changes

// Bad. Anyone who can read the log can read the old and new key.
{ label: "API key", before: oldKey, after: newKey }
// Good. The event says the key changed without showing it.
{ id, actor, action: "rotated the API key for", target: { label: "Production" }, timestamp }

Writing actions as codes

// Bad. "Ana Ruiz billing.plan.update Northwind Labs" is not a sentence.
action: event.type

Map event types to phrases people read, such as "changed the plan of", and put the raw type in details for people who need it.

Examples

One record's history

A narrower log beside a record, with an API actor, formatted money, and older days shown by date.

audit-log-record

API reference

AuditLog

Accepts native section props beyond those listed.

PropTypeDefaultDescription
entriesAuditLogEntry[]RequiredEvents in any order; they are sorted newest first.
nowDate | numberMount time, each minuteThe time relative times count from.
localestringThe browser'sFormats dates and times.
timeZonestringThe viewer'sGroups days and formats times in this zone.
filterablebooleanfalseShows the people and activity filters.
filterAuditLogFilterNoneControlled filter, { actor?, category? }.
defaultFilterAuditLogFilter{}Initial filter when uncontrolled.
onFilterChange(filter) => voidNoneRuns when a filter changes.
hasMorebooleanfalseShows Load older activity.
onLoadMore() => void | Promise<void>NoneFetches the next page.
maxInlineChangesnumber3Changes shown before the rest fold.
headerReactNodeBuilt-in headerReplaces the header; null removes it.
renderValue(value, change) => ReactNodeNoneDisplay for change values without their own render.
labelsPartial<AuditLogLabels>EnglishReplacement text, including the relative-time phrases.

AuditLogEntry

PropertyTypeDescription
idstringStable key.
actor{ id?; name; avatarUrl?; kind? }Who did it. kind is "person", "system", or "api".
actionstringThe words between the actor and the target.
target{ label; href? }What it was done to.
timestampDate | string | numberWhen it happened.
categorystringGroups entries for the activity filter.
changesAuditLogChange[]{ label; before?; after?; render? } or { kind: "list"; label; added?; removed?; itemLabel? }
detailsAuditLogDetail[]{ label; value; mono? } behind the Details button.
tone"default" | "danger"Tints a system or API actor's icon for destructive events.

Functions

ExportSignatureDescription
groupEntriesByDay(entries, timeZone?) => AuditLogDay[]The sorting and grouping the log uses.
filterEntries(entries, filter) => AuditLogEntry[]The built-in filter.
formatEntryTime(date, now, labels, options?) => stringThe short time beside an event.