---
title: "Logging in Next.js is hard (But it doesn't have to be)"
description: "Learn how to capture trace-connected logs across all Next.js runtimes (Edge, Node.js, and browser) using LogTape or the Sentry SDK."
url: "https://blog.sentry.io/logging-in-next-js-is-hard-but-it-doesnt-have-to-be/"
canonical_url: "https://blog.sentry.io/logging-in-next-js-is-hard-but-it-doesnt-have-to-be/"
authors: ["Kyle Tryon"]
published: "2026-03-30"
category: "Product Updates"
---

# Logging in Next.js is hard (But it doesn't have to be)

A typical [Next.js](https://sentry.io/for/nextjs/) deployment can execute code in up to three different runtimes: [Edge](https://vercel.com/docs/functions/runtimes/edge), Node.js, and the browser.

You may already be capturing logs from server-side code, but if you are not capturing the full request from middleware through server rendering to the browser, you are missing critical debugging information when problems arise.

**TL;DR**: A typical Next.js deployment can run in up to three environments: Node, Edge, and the browser. Most JavaScript logging libraries target Node; far fewer are compatible with Edge and the browser. LogTape and Sentry both provide runtime-agnostic logging in JavaScript.

## Why logging in Next.js is hard

![Diagram of Next.js application architecture showing a GET /app-page request flowing from User through Edge middleware to Node.js server and Browser, with logging at each layer: middleware, server, and browser.](https://blog.sentry.io/_vercel/image?url=_astro%2Fnextjs-application-architecture-diagram.BxAJ-I2s.png&w=1920&q=100)

### Problem 1: Most Loggers Assume Node.js

Most loggers were built specifically for Node.js, relying on APIs like `AsyncLocalStorage` or `fs` that aren’t available in the browser or Edge runtimes.

You’ll often see [Pino](https://blog.sentry.io/javascript-logging-library-definitive-guide/#pino-3) (or wrappers such as [Next-Logger](https://github.com/sainsburys-tech/next-logger)) suggested as the best logger for Next.js, but neither is actually a good choice for Next.js.

Pino, and by extension Next-Logger, is designed for Node.js, and uses a polyfill to work in the browser. But that polyfill means surrendering the performance benefits the library has in Node, and you _still_ cannot capture logs in Edge functions or middleware (running on Edge).

### Problem 2: Missing out on client-side logging

It’s easy to assume you don’t even need to capture client-side logs, because your “frontend code” is all server-side.

By default, Next.js uses [Server Components](https://nextjs.org/docs/app/getting-started/server-and-client-components) for all pages and components. So by default, any logs emitted from your “frontend code” will actually be captured in your server-side logs.

However, once you add a [`use client` boundary](https://nextjs.org/docs/app/getting-started/server-and-client-components#using-client-components) for interactive components, _that_ code will be executed in the browser.

Your “frontend code” is really a mix of [Server and Client Components](https://nextjs.org/docs/app/getting-started/server-and-client-components) that work together to render a single page and log to two different places.

We have to solve that fragmentation and make sure all frontend code, no matter where it runs, is captured and logged to the same place.

### Problem 3: Trace-connected structured logging

Logging is only one part of observability; on its own, it is most useful when you are debugging locally. In production, once you’re collecting logs from dozens, hundreds, or even thousands of requests, you need a way to tie related logs together for querying and aggregation.

Tracing adds a unique ID to each request in your app and appends that ID as structured data to every log you send. Then later, you can query logs based on that ID to find every related log from that same request, along with other telemetry in your monitoring platform, such as [errors in Sentry](https://docs.sentry.io/product/#error-monitoring).

Adding tracing to Next.js is actually easy, but it is still a step you have to take, and there are several ways to do it.

We’re going to pair a JavaScript logging library with Sentry to instrument Next.js with trace-connected logs. In a future post, we’ll cover and compare another way to instrument Next.js with tracing, using OpenTelemetry.

## What to look for in a Next.js logger

What should that logger do? I recently compared all of the current [popular JavaScript logging libraries](https://blog.sentry.io/javascript-logging-library-definitive-guide/) and broke down _why_ you should be using a logging library in the first place. The same holds mostly true for Next.js, but we need to be even more specific.

When evaluating a logging solution for Next.js, consider:

-   **Runtime Match:** Needs to run on Node, Browser, and Edge runtimes (if using Edge).
-   **Tracing Support:** Next.js apps are multi-service by default. Tracing connects logs from multiple sources under a single trace.
-   **Production Features:** Filtering for data redaction and noise reduction; context management and child loggers to improve structured logging and make later querying and aggregation easier.

As you might expect, feature-wise, libraries have been coalescing and imitating one another’s good practices, to the point where they have become similar.

Still, the biggest difference to keep an eye out for is runtime support and performance.

There are two practical fits that cover the full scope of a Next.js app, and they are not mutually exclusive:

-   [LogTape](https://blog.sentry.io/trace-connected-structured-logging-with-logtape-and-sentry/) with the [Sentry sink](https://logtape.org/sinks/sentry)
-   `Sentry.logger` with the [Sentry Next.js SDK](https://docs.sentry.io/platforms/javascript/guides/nextjs/logs/)

### LogTape

Mentioned in my [other post](https://blog.sentry.io/javascript-logging-library-definitive-guide/#logtape-6), LogTape is one of the newest libraries on the scene and is quickly becoming a favorite dedicated logging library. It’s built from the ground up with no dependencies and runs natively in _all_ JavaScript runtimes.

LogTape’s [context management](https://logtape.org/manual/contexts) and [categories](https://logtape.org/manual/categories) are especially useful for tagging and organizing your logs for more efficient querying later.

### Configure categories and the Sentry sink

```javascript
import { getSentrySink } from "@logtape/sentry";
import { configure } from "@logtape/logtape";

await configure({
  sinks: {
    sentry: getSentrySink(),
  },
  loggers: [
    {
      category: ["next-app"],
      lowestLevel: "info",
      sinks: ["sentry"],
    },
    {
      category: ["next-app", "middleware"],
      lowestLevel: "info",
      sinks: ["sentry"],
    },
    {
      category: ["next-app", "client"],
      lowestLevel: "info",
      sinks: ["sentry"],
    },
  ],
});
```

Each logger you define can be filtered in Sentry (for example `category: next-app.client`) to fetch only the logs from a particular category.

### Explicit context in a client component

You can also add [contexts](https://logtape.org/manual/contexts) to loggers to automatically append data:

```javascript
"use client";

import { useEffect, useMemo } from "react";
import { getLogger } from "@logtape/logtape";

const logger = getLogger(["next-app", "client"]);

export function OrderConfirmation({ orderId }: { orderId: string }) {
  // Automatically includes the orderId in all future logs from this component
  const ctx = useMemo(() => logger.with({ orderId }), [orderId]);

  useEffect(() => {
    const fromQuery = new URLSearchParams(window.location.search).get("orderId");
    if (fromQuery && fromQuery !== orderId) {
      ctx.with({ fromQuery }).warn(
        "Confirmation orderId {orderId} does not match URL query {fromQuery}; check redirects and rewrites.",
      );
    }
  }, [orderId, ctx]);

  return <p>Thanks for your order.</p>;
}
```

You can read the full [Structured Logging with LogTape](https://blog.sentry.io/trace-connected-structured-logging-with-logtape-and-sentry/) post to get a deeper look at best practices for structured logging with LogTape and Sentry.

### Sentry Next.js SDK

But, you might not need an additional logging library at all.

[Sentry’s Next.js SDK](https://docs.sentry.io/platforms/javascript/guides/nextjs/) includes [**Sentry Logs**](https://sentry.io/product/logs/), a logging library built into Sentry’s SDKs for multiple platforms, not just JavaScript.

Sentry’s [Logger for Next.js](https://docs.sentry.io/platforms/javascript/guides/nextjs/logs/) is also runtime-agnostic, providing logging everywhere your Next.js app can run. And if you are already using Sentry, or plan to use Sentry for capturing errors and tracing anyway, you can add logging without adding any new dependencies.

```javascript
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  enableLogs: true,
});

Sentry.logger.info("Checkout completed", {
  orderId: order.id,
  userId: user.id,
  userTier: user.subscription,
  cartValue: cart.total,
  itemCount: cart.items.length,
  paymentMethod: "stripe",
});
```

[Scopes](https://docs.sentry.io/platforms/javascript/guides/nextjs/logs/#scope-attributes) and contexts work a little differently than in LogTape, but the functionality is similar.

You can use `Sentry.withScope` to set context data that will automatically be included on every log emitted inside the callback.

```javascript
"use client";

import * as Sentry from "@sentry/nextjs";

function setGlobalAndIsolationScopes() {
  Sentry.getGlobalScope().setAttributes({ service: "checkout", version: "2.1.0" });
  Sentry.getIsolationScope().setAttributes({ org_id: "org_demo_001", user_tier: "pro" });
}

function calcShipping() {
  Sentry.logger.info("calcShipping: rate lookup", { carrier: "demo_carrier" });
  return 12.5;
}

function checkout() {
  const shipping = calcShipping();
  Sentry.logger.info("checkout: shipping computed", { shipping_usd: shipping });
}

function onCheckout() {
  setGlobalAndIsolationScopes();
  Sentry.withScope((scope) => {
    scope.setAttribute("checkout_id", crypto.randomUUID());
    checkout(); // nested logs inherit global + isolation + checkout_id
  });
}
```

In the Sentry Log Explorer, opening the `checkout: shipping computed` entry shows the fields passed to that log call (`shipping_usd` at `12.5`) and the merged attributes applied to the same scope up till that point (`service`, `version`, `org_id`, `user_tier` and `checkout_id`).

![Sentry log entry detail for the 'checkout: shipping computed' event showing shipping_usd of 12.5, payload size 459 bytes, Chrome version 146, and sentry.javascript.nextjs SDK version 10.45.0.](https://blog.sentry.io/_vercel/image?url=_astro%2Fsentry-log-entry-checkout-shipping.fjtMBiSa.png&w=1200&q=100)

## Getting trace-connected logs end to end

Finally, we want more than just messages from our logs. We want structured data with useful debugging information. When we see a log, we want to know where it came from, what triggered it, and what else happened as a part of that request.

[Tracing](https://sentry.io/product/tracing/) monitors the execution and timing of requests throughout an app. If there is a function or service that ends up causing slowdowns or even errors for users of the app, tracing data is how we collect information and ultimately discover the problem.

When we configure Sentry, every request will be assigned a unique “Trace ID” that will link all data connected to that request together.

Use [Sentry’s setup wizard](https://docs.sentry.io/platforms/javascript/guides/nextjs/) to automatically instrument your Next.js app with tracing and logs.

```bash
npx @sentry/wizard@latest -i nextjs
```

You should end up with three files similar to the following.

```javascript
import * as Sentry from "@sentry/nextjs";
Sentry.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: process.env.NODE_ENV === "development" ? 1.0 : 0.1,
  enableLogs: true,
});
```

They’ll be named `instrumentation-client.ts`, `sentry.server.config.ts`, and `sentry.edge.config.ts`. One entry point per runtime.

Then use `Sentry.logger` as above, or integrate with an existing logger, like LogTape.

If your app was already instrumented with `console.log`, you should try to upgrade to structured logging, but you can still [forward console output to Sentry](https://docs.sentry.io/platforms/javascript/guides/nextjs/logs/#console) with the `consoleLoggingIntegration`.

That’s all you need to do. Every request will now contain a trace ID that will follow through the entire app, across runtimes, allowing you to query for all of the logs and traces related to that request.

## Querying the logs

With logs throughout the application and connected with a trace ID, you can start querying for logs for debugging, custom dashboards, alerts, and more.

![Sentry Logs Explorer for the javascript-nextjs project showing a bar chart of log counts over 30 days and an expanded HydrationDemoDate log entry in Chrome version 146.](https://blog.sentry.io/_vercel/image?url=_astro%2Fsentry-logs-explorer-count-chart.BsKGD6HD.png&w=1200&q=100)

In [Explore > Logs](https://sentry.io/explore/logs/) you can search for logs based on any of the structured data properties.

Sentry will automatically inject several useful attributes, like the `environment`, which is showing us this was from the development server. In this log, there was also `browser` attribute present which was automatically applied and shows us that this request came from Chrome. You’ll also notice the Chrome icon on the right. Server-side logs won’t contain either of these.

If we wanted to filter the logs down to include only the logs from the browser, we could search `has: browser` or `browser.name: Chrome` if we wanted to see a specific browser.

It’s not uncommon to add a `service` or `component` attribute to logs, to make it more clear where a log was emitted from. You can use scopes with the Sentry logger or categories with LogTape to broadly append a queryable attribute like this to all logs in a stack.

Read my post about [structured logging](https://blog.sentry.io/trace-connected-structured-logging-with-logtape-and-sentry/) to get a better idea about the type of data you might want to append to your logs.

Every attribute shown here, including any additional data you append to the logs, is queryable. To see the other logs that were a part of this same request, we just need to click on the `trace` ID.

![Sentry Logs tab showing a ui.interaction.click event trace with an expanded HydrationDemoDate log entry, displaying Chrome version 146 metadata in a development environment.](https://blog.sentry.io/_vercel/image?url=_astro%2Fsentry-logs-event-detail-hydration.DICr35M1.png&w=1920&q=100)

## Next steps

Setting up a logger that captures the full surface area of your Next.js app is the first major step, but how you instrument your logs, and make use of that data is what really matters.

-   Implement [structured logs](https://blog.sentry.io/trace-connected-structured-logging-with-logtape-and-sentry/) with a lot of high cardinality data.
-   Add contextual data, like the name of the service or component that triggered the log.
-   Audit your existing logs and start [replacing old `console.log` statements](https://sentry.io/cookbook/structured-logging-logtape/).
-   Learn more about [how to query logs](https://docs.sentry.io/product/logs/).

With [structured logs](https://sentry.io/product/logs/), packed with useful high-cardinality data to query, you (or your LLM) will be able to quickly debug new issues, as they come in. You can write queries yourself, and configure dashboards to visualize aggregate data. To go further, see how to [build a dashboard and alerts from custom metrics in Next.js](https://sentry.io/cookbook/custom-metrics-dashboard-alerts-nextjs/).

![Sentry AI assistant response showing 'checkout: shipping computed' log counts aggregated by browser — Chrome with 8 hits and Electron with 4, all occurring on March 22.](https://blog.sentry.io/_vercel/image?url=_astro%2Fsentry-ai-log-aggregation-by-browser.Bwa4UN0J.png&w=640&q=100)

Try using [Sentry’s Seer AI](https://sentry.io/product/seer/) to query logs with natural language. You can use the [Sentry MCP server](https://mcp.sentry.dev/), or click the “Ask Seer” button on the log explorer page. Rolling out now, you can even ask Seer to create custom dashboard widgets for you from your log data, or other data you might correlate with logs.

Add logs now, cover all of your surfaces, and tomorrow’s bugs will be much more approachable.
