# bark › Reference

## collectors

### `resetCollectors`

```ts
function resetCollectors(): void
```

Uninstall collectors and reset state (tests only).

## config

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

### `pretty`

```ts
const pretty: Formatter = (record)
```

Pretty formatter: readable in browser devtools and `wrangler dev`.

### `json`

```ts
const json: Formatter = (record)
```

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

### `consoleSink`

```ts
const consoleSink: Sink = (record, formatted)
```

Default sink: write to the appropriate console method.

### `BarkConfig`

```ts
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`

```ts
function configure(config: BarkConfig): void
```

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

### `resetConfig`

```ts
function resetConfig(): void
```

Reset all config to defaults (primarily for tests).

## context

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

### `Span`

```ts
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`

```ts
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`

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

### `makeTraceparent`

```ts
function makeTraceparent(traceId: string): string
```

### `createContext`

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

### `serverTimingHeader`

```ts
function serverTimingHeader(spans: Span[]): string
```

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

## Logger

| | |
| --- | --- |
| [`SpanHandle`](#spanhandle) | Handle returned by `span()`. |
| [`Bark`](#bark) | A Bark scope: created by `Bark.start()` (per request/interaction) or `branch()` (named sub-scope sharing the same context). |

### `SpanHandle`

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

Handle returned by `span()`.

### `Bark`

```ts
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`](#rules) |  |
| [`parseRules`](#parserules) | Parse the filter grammar: comma-separated `name\|level` rules with `*` wildcards and `-name` excludes. |
| [`makeMatcher`](#makematcher) | Memoized name+level filter check against parsed rules. |

### `Rules`

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

### `parseRules`

```ts
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`

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

Memoized name+level filter check against parsed rules.

## record

| | |
| --- | --- |
| [`LEVELS`](#levels) |  |
| [`Level`](#level) |  |
| [`Fields`](#fields) | Arbitrary structured fields attached to scopes and records. |
| [`RecordKind`](#recordkind) | What kind of record this is — logs, timing spans, and errors share one pipe. |
| [`BarkRecord`](#barkrecord) | The one record shape everything emits: logs, timings, and errors are all `BarkRecord`s flowing through the same sink, correlated by `traceId`. |
| [`Formatter`](#formatter) | Formats a record into the argument list passed to the sink. |
| [`Sink`](#sink) | Receives formatted output. |

### `LEVELS`

```ts
const LEVELS
```

### `Level`

```ts
type Level = (typeof LEVELS)[number];
```

### `Fields`

```ts
type Fields = Record<string, unknown>;
```

Arbitrary structured fields attached to scopes and records.

### `RecordKind`

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

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

### `BarkRecord`

```ts
interface BarkRecord
```

The one record shape everything emits: logs, timings, and errors are all
`BarkRecord`s 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`

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

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

### `Sink`

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

Receives formatted output. Default writes to console.

## serialize

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

### `FieldSerializer`

```ts
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`

```ts
function serializeRequest(value: unknown): unknown
```

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

### `serializeResponse`

```ts
function serializeResponse(value: unknown): unknown
```

Serialize a fetch-API Response.

### `serializeError`

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

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

### `serializers`

```ts
const serializers
```

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