# Log to the console (https://effetch.monarcode.dev/docs/plugins/console-logging)

While you develop, a failed call is easier to fix when you can see what was
sent and what came back. `logger` writes one structured entry per call for
production logs, and leaves headers, query strings and bodies out.
`consoleLogger` is for development: it prints everything about a call to the
console, as a group that devtools can expand. This page shows how to add it,
how to read what it prints, and how to tune it.

## Add the console logger

Add `consoleLogger` to the client's plugins. This example turns it on only in
development, flags calls slower than a second, and redacts an extra body key:

```ts
import { Fetch } from "@monarcode/effetch";
import { consoleLogger } from "@monarcode/effetch/plugins";

declare const isDevelopment: boolean;

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [
    consoleLogger({
      enabled: isDevelopment,
      slow: "1 second",
      redact: { body: ["otp"] },
    }),
  ],
}) {}
```

## Read the output

Each call prints a summary line. With `detail: "full"`, the default, that
line opens a group with the call's URL, headers, bodies and error. Here's
what Node prints for six calls, with `slow: "1 second"`. The first five ran
with `detail: "summary"`, so only the last one opens a group. The fourth was
interrupted by `Effect.timeout`, and the fifth died because its `bearer`
token function threw:

```text
GET /todos/:id  200 OK  18 ms  24 B
✗ GET /todos  2 ms  → NetworkError: fetch failed
⚠ GET /reports/:id  200 OK  1.2 s  8 B  (slow)
⊘ GET /slow/:id  1.0 s  interrupted  (slow)
✗ GET /broken  0 ms  → defect: Error: No token store
✗ POST /users/:id/roles  400 Bad Request  2 ms  48 B  → InvalidRole: No such role
  POST https://api.example.com/users/7/roles
  request headers  {
    'content-type': 'application/json',
    'content-length': '16',
    accept: 'application/json',
    authorization: '<redacted>',
    b3: '…',
    traceparent: '…'
  }
  request body     { role: 'owner' }
  response headers { 'content-length': '48', 'content-type': 'application/json' }
  response body    { code: 'INVALID_ROLE', message: 'No such role' }
  error            { _tag: 'InvalidRole', message: 'No such role', code: 'INVALID_ROLE' }
```

The summary line starts with a mark for how the call ended:

- no mark: it succeeded in time;
- `✗`: it failed, and the line ends with the error. A call that died shows
  `→ defect:` and the defect;
- `⚠`: it succeeded, but took at least as long as `slow`, and the line ends
  with `(slow)`;
- `⊘`: it was interrupted, such as by `Effect.timeout`.

It prints each call after it's over, with the error it ended with, after
[`statusErrors`](https://effetch.monarcode.dev/docs/errors/status-errors) and decoding. Values are printed
as objects, so devtools can expand them. In browser devtools, the group
starts collapsed under its summary line. Node, as the output shows, prints
it expanded. The `b3` and `traceparent` headers are the tracing headers
that Effect's `HttpClient` adds, with their IDs cut here.

## Options

`consoleLogger` takes the following options:

| Option          | Default                         | What it does                                                                                                                                               |
| --------------- | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `enabled`       | `true`                          | `false` prints nothing, and keeps the plugin's type, so it can stay in the list in production.                                                             |
| `detail`        | `"full"`                        | `"summary"` prints the line alone. `"headers"` adds the URL, headers and error. `"full"` adds the bodies.                                                  |
| `only`          | `"all"`                         | `"errors"` prints failed calls only. An interrupted call isn't a failed one.                                                                               |
| `collapsed`     | `true`, `false` in React Native | Whether groups start collapsed. Metro's terminal drops collapsed groups, so React Native defaults to `false`.                                              |
| `slow`          | `false`                         | A duration, such as `"1 second"`. Calls that take at least this long are flagged.                                                                          |
| `maxBodyLength` | `10_000`                        | Body text longer than this many characters is cut. A JSON body within it is printed as an object.                                                          |
| `redact`        | Built-in names (see Details)    | `{ headers, query, body }`: more names to redact, as strings (ignoring case, `-` and `_`) or regular expressions. `false` redacts nothing.                 |
| `filter`        | None: every call is printed     | `(call) => boolean`: prints only the calls it accepts. `call` has the `route`, the `request`, the `response`, the `exit` and the `durationMs`.             |
| `color`         | `"auto"`                        | `"css"` (browser devtools and React Native DevTools), `"ansi"` (terminals), or `false`. `"auto"` uses `"css"` where there's a `document`, else plain text. |

## Change the output for one call

A call can change `detail`, `only`, `collapsed`, `slow` and `maxBodyLength`,
or skip the plugin with `false`:

```ts
import * as Effect from "effect/Effect";

import { Fetch } from "@monarcode/effetch";
import { consoleLogger } from "@monarcode/effetch/plugins";

class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [consoleLogger()],
}) {}

export const program = Effect.gen(function* () {
  const api = yield* Api;
  yield* api.get("/reports/export", {
    plugins: { consoleLogger: { detail: "summary" } },
  });
  yield* api.get("/health", { plugins: { consoleLogger: false } });
});
```

## Redaction, bodies, platforms and timing

Expand the following sections for what `consoleLogger` redacts, which bodies
it reads, how it behaves on each platform, and when it runs:

**Details: what it redacts**

It replaces these with `<redacted>`, matching names ignoring case, `-` and
`_`:

- headers in Effect's `Headers.CurrentRedactedNames` (`authorization`,
  `cookie`, `set-cookie`, `x-api-key`), `proxy-authorization`,
  `x-auth-token`, `x-access-token`, `x-refresh-token`, `x-session-token`,
  `x-csrf-token`, `x-xsrf-token`, `x-client-secret` and
  `x-amz-security-token`;
- keys in JSON and form bodies, at any depth: `token`, `accessToken`,
  `refreshToken`, `idToken`, `authToken`, `apiToken`, `sessionToken`,
  `csrfToken`, `xsrfToken`, `secret`, `clientSecret`, `apiSecret`,
  `apiKey`, `privateKey`, and any name containing `password`;
- query parameters with the same names, `key`, `sig`, `signature` and the
  signed-URL ones (`X-Amz-Signature`, `X-Goog-Signature`, …), values passed
  as `Redacted`, and credentials in the URL (`https://user:password@…`).

They're exact names rather than patterns, so fields such as `total_tokens`
stay visible. Plain-text bodies are printed as they are.

A call's error is printed as a copy of its fields, with its `_tag` and
`message`, redacted like a body, since errors can carry body data. The copy
leaves out an `HttpError`'s `text` and a `DecodeError`'s `issue`, which the
response body line shows. Defects are printed as they are, with their stack.

**Details: which bodies it reads**

- It reads a response body only with `detail: "full"`, and only when it's
  text: JSON, `text/*` other than event streams, XML or a form. A body over
  1 MB, or over 4 × `maxBodyLength` if that's larger, is shown by its type
  and size instead.
- Effect caches the body, so reading it here doesn't change what the call
  decodes.
- A call without `output` returns the response itself, so `consoleLogger`
  doesn't read its body, which stays yours to stream. It doesn't read an
  interrupted call's body either.
- For `api.sse`, `api.ndjson` and stream endpoints, the entry is printed
  when the response headers arrive, without the body. An error while the
  stream reads, such as a `DecodeError`, isn't printed.
- A `HEAD` request and a `204`, `205` or `304` response have no body to
  print.

**Details: platforms**

- It writes through Effect's `Console`, so you can provide your own. It calls
  only `console.log` and the group methods, never `console.error`, which
  React Native turns into an error screen.
- In React Native, groups start expanded: Metro's terminal silently drops
  `console.groupCollapsed` and everything logged inside it. React Native is
  detected by `navigator.product`, which it sets to `"ReactNative"`.
- Node, Bun and Deno print collapsed groups expanded.

**Details: timing and what it shows**

- It runs from an `onExit` hook after the call is over, so where it sits in
  the plugin list doesn't matter. A retried call is one entry, and its
  duration covers every attempt, until the status is checked and the body
  decoded. `logger`'s duration stops earlier, at the response headers.
- The size comes from `content-length`, or from the body when it's read.
- When a plugin fails on a response, such as an auth plugin on a `401`, that
  response is shown with the plugin's error.
- The request shown is the one that was sent, after plugins such as `bearer`
  changed it. When no response came back, it's the one the call built,
  before plugins ran.
- A `slow` that isn't a duration throws when the plugin is made. A call's own
  `slow` that isn't a duration flags nothing.

## What's next

- [Built-in plugins](https://effetch.monarcode.dev/docs/plugins#logger): compare `consoleLogger` with
  `logger` for production logs.
- [Change plugins for one call](https://effetch.monarcode.dev/docs/plugins/per-call): skip or adjust any
  plugin on a single call.
- [Write a plugin](https://effetch.monarcode.dev/docs/plugins/writing): build your own logging or metrics
  on the `onExit` hook that `consoleLogger` uses.