@diso.io

bark @diso.io/bark

Reference

collectors

resetCollectors

function resetCollectors(): void

Uninstall collectors and reset state (tests only).

config

pretty Pretty formatter: readable in browser devtools and wrangler dev.
json JSON-lines formatter: one line per record, for Workers Logs / Logpush.
consoleSink Default sink: write to the appropriate console method.
BarkConfig
configure Set global config (call once at app init).
resetConfig Reset all config to defaults (primarily for tests).

pretty

const pretty: Formatter = (record)

Pretty formatter: readable in browser devtools and wrangler dev.

json

const json: Formatter = (record)

JSON-lines formatter: one line per record, for Workers Logs / Logpush.

consoleSink

const consoleSink: Sink = (record, formatted)

Default sink: write to the appropriate console method.

BarkConfig

interface BarkConfig
level?: string | string[] Filter grammar (name|level, *, -name) — or a bare level like 'info'.
format?: "pretty" | "json" | Formatter 'pretty' (default), 'json', or a custom formatter.
sink?: Sink Where formatted records go. Default: console.
timing?: boolean Master switch for timing collection (observers + spans). Default on.
timingAllowOrigin?: string Value for Timing-Allow-Origin on finish() responses (e.g. '*').
time?: () => string Override the timestamp source (testing).

configure

function configure(config: BarkConfig): void

Set global config (call once at app init). Merges over previous calls.

resetConfig

function resetConfig(): void

Reset all config to defaults (primarily for tests).

context

Span A completed (or open) timing span, recorded on the scope's context.
Context Per-request/interaction state shared by a scope and all its branches: correlation id, accumulated fields, and timing spans.
parseTraceparent
makeTraceparent
createContext
serverTimingHeader Render spans as a Server-Timing header value: name;dur=12.3;desc="...".

Span

interface Span

A completed (or open) timing span, recorded on the scope's context.

name: string
start: number
duration?: number Milliseconds; set on end().
description?: string
fields?: Fields

Context

interface Context

Per-request/interaction state shared by a scope and all its branches: correlation id, accumulated fields, and timing spans.

traceId: string
fields: Fields
spans: Span[]
start: number Epoch ms when the scope started (drives total request duration).

parseTraceparent

function parseTraceparent(header: string | null | undefined): string | null

makeTraceparent

function makeTraceparent(traceId: string): string

createContext

function createContext(traceId?: string | null): Context

serverTimingHeader

function serverTimingHeader(spans: Span[]): string

Render spans as a Server-Timing header value: name;dur=12.3;desc="...".

Logger

SpanHandle Handle returned by span().
Bark A Bark scope: created by Bark.start() (per request/interaction) or branch() (named sub-scope sharing the same context).

SpanHandle

interface SpanHandle {
  end(fields?: Fields): void
}

Handle returned by span().

Bark

class Bark

The default export.

A Bark scope: created by Bark.start() (per request/interaction) or branch() (named sub-scope sharing the same context). All records emitted through a scope carry its context fields and traceId.

static configure(config: BarkConfig): void
static serializers = serializers The field-serializer registry (request, response, error, …).
static levels = LEVELS
static reset(): void Reset global config (primarily for tests).
static start(fields: Fields = {}, name = "app"): Bark Start a scope. On the server pass { request } — it is serialized and the traceparent header (if any) becomes the traceId. In the browser the first start() also installs the passive collectors (performance + errors).
readonly name: string
constructor(name: string, context?: Context, fields: Fields = {})
get traceId(): string
get traceparent(): string A traceparent header value for propagating this trace to subrequests.
set(fields: Fields): this Merge fields into the scope's context (serializing known keys).
branch(name: string, fields: Fields = {}): Bark Named sub-scope: bark.branch('checkout', { cart }). Shares the context.
trace(message?: string | Error, fields?: Fields): void
debug(message?: string | Error, fields?: Fields): void
info(message?: string | Error, fields?: Fields): void
log(message?: string | Error, fields?: Fields): void Alias for info.
warn(message?: string | Error, fields?: Fields): void
error(message?: string | Error, fields?: Fields): void
fatal(message?: string | Error, fields?: Fields): void
span(name: string, description?: string): SpanHandle Opt-in timing span (wall-clock; on Workers time only advances across I/O). const s = bark.span('db'); …; s.end(). Spans feed Server-Timing on finish.
instrument<T extends object>(target: T, label = "client"): T Opt-in client instrumentation: returns a proxy whose async method calls are timed as spans (label.method). Call sites stay untouched.
finish(fields: Fields = {}): Response | undefined Finish the scope: serialize final fields (e.g. { response }), attach Server-Timing (+ optional Timing-Allow-Origin) when a Response is given, emit the request-summary record, and return the (possibly copied) Response.

matcher

Rules
parseRules Parse the filter grammar: comma-separated name|level rules with * wildcards and -name excludes.
makeMatcher Memoized name+level filter check against parsed rules.

Rules

interface Rules {
  includes: Array<{ name: RegExp; level: Level }>
  excludes: RegExp[]
}

parseRules

function parseRules(config: string | string[]): Rules

Parse the filter grammar: comma-separated name|level rules with * wildcards and -name excludes. A bare level name (info) means *|info; a bare name means name|error.

makeMatcher

function makeMatcher(rules: Rules): (level: Level, name: string) => boolean

Memoized name+level filter check against parsed rules.

record

LEVELS
Level
Fields Arbitrary structured fields attached to scopes and records.
RecordKind What kind of record this is — logs, timing spans, and errors share one pipe.
BarkRecord The one record shape everything emits: logs, timings, and errors are all BarkRecords flowing through the same sink, correlated by traceId.
Formatter Formats a record into the argument list passed to the sink.
Sink Receives formatted output.

LEVELS

const LEVELS

Level

type Level = (typeof LEVELS)[number];

Fields

type Fields = Record<string, unknown>;

Arbitrary structured fields attached to scopes and records.

RecordKind

type RecordKind = "log" | "timing" | "error";

What kind of record this is — logs, timing spans, and errors share one pipe.

BarkRecord

interface BarkRecord

The one record shape everything emits: logs, timings, and errors are all BarkRecords flowing through the same sink, correlated by traceId.

kind: RecordKind
level: Level
name: string Logger/scope name (:-joined through branch()); used for filtering.
message?: string
time: string ISO timestamp.
traceId?: string Correlation id for the request/interaction this record belongs to.
fields: Fields Merged context + call-site fields (serialized).

Formatter

type Formatter = (record: BarkRecord) => unknown[];

Formats a record into the argument list passed to the sink.

Sink

type Sink = (record: BarkRecord, formatted: unknown[]) => void;

Receives formatted output. Default writes to console.

serialize

FieldSerializer A field serializer: turns a known field's raw value (e.g.
serializeRequest Serialize a fetch-API Request (Cloudflare-aware: captures select cf data).
serializeResponse Serialize a fetch-API Response.
serializeError Serialize an Error: message/name/stack, own props, cause chain, fingerprint.
serializers The registry: known field keys → serializers, with Cloudflare-sensible defaults.

FieldSerializer

type FieldSerializer = (value: unknown) => unknown;

A field serializer: turns a known field's raw value (e.g. a Request) into plain structured fields. Returning an object under the same key keeps records JSON-safe without the caller thinking about it.

serializeRequest

function serializeRequest(value: unknown): unknown

Serialize a fetch-API Request (Cloudflare-aware: captures select cf data).

serializeResponse

function serializeResponse(value: unknown): unknown

Serialize a fetch-API Response.

serializeError

function serializeError(value: unknown, depth = 0): unknown

Serialize an Error: message/name/stack, own props, cause chain, fingerprint.

serializers

const serializers

The registry: known field keys → serializers, with Cloudflare-sensible defaults. Extend or override with serializers.set(key, fn).

This page as Markdown