# React app logging and exception monitoring

Server logs can't tell you that a button throws in Safari. This guide sets up logging and exception monitoring for a React app with `@tailcove/browser`. Uncaught errors, unhandled promise rejections and render crashes are reported on their own and grouped into issues. The events you log yourself go to the same place, next to your backend logs.

## 1. Create a browser key

Browser code is public, so it must never contain an ingest token. Instead, open your source in Tailcove, go to **Settings**, and generate a **browser key**.

Set its **allowed origins** to the sites that will send logs, for example `https://app.example.com` and `http://localhost:5173` for development. Requests from any other origin are rejected, which is what keeps a public key safe.

## 2. Install the package

```bash
npm install @tailcove/browser
```

## 3. Wrap your app

The React adapter provides a provider and an error boundary:

```tsx
import { ErrorBoundary, TailcoveProvider } from '@tailcove/browser/react';

export function Root() {
    return (
        <TailcoveProvider
            options={{
                key: import.meta.env.VITE_TAILCOVE_BROWSER_KEY,
                captureWindowErrors: true,
                captureUnhandledRejections: true,
            }}
        >
            <ErrorBoundary fallback={<p>Something went wrong.</p>}>
                <App />
            </ErrorBoundary>
        </TailcoveProvider>
    );
}
```

The error boundary reports render crashes; `captureWindowErrors` and `captureUnhandledRejections` catch everything that happens outside React's render cycle, such as event handlers and failed `fetch` chains.

## 4. Log events from your React app

The exception reporter covers what you didn't expect. For the rest, log explicitly, with context:

```ts
import { init } from '@tailcove/browser';

const tailcove = init({ key: import.meta.env.VITE_TAILCOVE_BROWSER_KEY });

tailcove.info('checkout started', { cartTotal: 42.5 });
tailcove.warn('payment form retried', { attempt: 2 });
```

Every line gets the page URL, the user agent and the viewport size added for you. What's worth logging in a React app:

- Steps of a flow that earns money or loses users: sign-up, checkout, an upload.
- Failed API calls, with the route and the status code. A `fetch` that returns 500 doesn't throw, so the reporter won't see it.
- The ids you'd search for later, such as an order id or your own user id. Leave out emails, tokens and form contents.

Log the outcome, not every render. A line per click or per state change costs quota and buries the lines you need.

## 5. Decide on console capture

`captureConsoleErrors` records `console.error` calls. Third-party scripts can be noisy, so sample them:

```ts
init({
    key: '...',
    captureConsoleErrors: true,
    consoleSampleRate: 0.1,
});
```

## 6. Verify it works

Open your app with the browser's devtools console and run:

```js
setTimeout(() => {
    throw new Error('Tailcove test error');
});
```

Within a few seconds the error appears in the source's **Live tail**, and on the **Issues** tab it is grouped as a new issue.

## How delivery works

Lines are batched, 20 at a time or every 2 seconds. When the tab is hidden or the page unloads, the client switches to `navigator.sendBeacon` so the last batch is not lost.

## Good to know

- Captured errors use the same structured shape as server-side clients, so the same exception groups into the same issue.
- Passwords, tokens and keys are masked in the browser before a line is queued (from 0.2.0), including the page URL and stack traces, so `/reset?token=abc` is sent as `/reset?token=[redacted]`. Add your own field names with `secretKeys`, or drop lines with `beforeSend`: see [Secret masking](https://tailcove.dev/docs/sending-logs/browser#secret-masking).
- `location` comes from the first stack frame. For a minified production build, upload your source maps and each frame shows the original file and line ([Code links and source maps](https://tailcove.dev/docs/code-links)).
- Vue and SvelteKit have adapters too, see the [browser reference](https://tailcove.dev/docs/sending-logs/browser).

## FAQ

**How do I log errors in a React app?**

Install `@tailcove/browser`, create a browser key and wrap the app in the provider and error boundary from step 3. Uncaught errors, unhandled promise rejections and render crashes are then reported on their own, and you can log anything else yourself with a level and context, as in step 4.

**What is the difference between error tracking and logging in React?**

Logging records the events you choose to write, such as a failed checkout with its order id. Error tracking catches the exceptions you did not expect and groups each repeat of the same error into one issue, so you see which errors are new and which are growing. With Tailcove both go to the same source, so you can read the log lines around an exception.

**What does a React exception reporter capture?**

The error's message, type and stack trace, plus the page URL, the browser's user agent and the viewport size. Render crashes caught by the error boundary also carry React's component stack, so you can see which component threw.

**Is it safe to put a key in browser code?**

Yes, if it is a browser key. A browser key only accepts logs from the origins you allowed, so a copied key is useless elsewhere. Never put an ingest token in browser code.

**Do I need source maps?**

Not to get started, but you'll want them for a production build. Without them a frame reads `app-3f9a1c.js:1:23456`. Upload the build's `.map` files from CI (`tailcove sourcemaps upload ./dist/assets --source web --url-prefix https://app.example.com/assets`) and each frame shows the original file, line and function, with the code around it and a link to your repository. See [Code links and source maps](https://tailcove.dev/docs/code-links).

## Next steps

- [Alert on error spikes](https://tailcove.dev/guides/alert-on-error-spikes) for your frontend source.
- Read about [how issues group errors](https://tailcove.dev/product/error-tracking).
