Using the Pages Router?
See legacy docs for v0.1.*, or upgrade to the App Router.
Install Logtail Next.js NPM package:
See legacy docs for v0.1.*, or upgrade to the App Router.
Set environment variables, e.g. in your console before running the server:
Set up Better Stack Next.js client in next.config.ts or next.config.js:
Use structured logging in client, edge, or server-side files.
You should see your logs in Better Stack → Live tail.
For Next.js version 14, use @logtail/next in 0.2.*. For slight differences in syntax, see the "in Next.js v14" tab in examples below.
Older versions 12.1.4 and above are supported in @logtail/next version 0.1.*. See legacy docs.
Wrap your route handlers in withBetterStack to add a logger to your request and log exceptions automatically:
To send logs from client components, add useLogger to your component:
To send logs from server components, add Logger to your component, and call flush before returning:
If your Next.js application uses authentication middleware (like Clerk or NextAuth.js), it may block telemetry requests sent from the browser. These requests, sent to the /_betterstack/* proxy route, are essential for collecting client-side logs and Web Vitals.
To prevent issues, you must configure your middleware to treat /_betterstack/(.*) as a public or ignored route.
Here are configuration examples for common middleware libraries:
By correctly configuring your middleware, you ensure that all telemetry data from the browser is successfully collected.
By default, withBetterStack proxies browser telemetry to Better Stack using a Next.js rewrite. If the ingest endpoint is slow or unreachable, Next.js turns the failed rewrite into a 500 error on your own domain.
To make delivery failures invisible to the browser, serve the proxy path with the route handler included in @logtail/next version 0.4.0 or higher. It forwards telemetry with a 5-second timeout and always responds with 204, logging delivery failures on the server only.
Pick a proxy path that can host a route handler. The default /_betterstack can't, as folders prefixed with _ are private in the App Router:
Export the route handler for both proxied endpoints:
Static route files take precedence over the rewrite, so nothing else changes. Use one file per endpoint, as a [...path] catch-all would be shadowed by the rewrite.
Using authentication middleware? Allow your new proxy path instead of /_betterstack in the middleware configuration.
Please let us know at hello@betterstack.com.
We're happy to help! 🙏
To capture traffic requests, create a middleware.ts file in the root folder of your Next.js app:
The log level defines the lowest level of logs sent to Better Stack. Choose one of the following levels (from lowest to highest):
debug is the default setting. It means that you send all logs to Better Stack.infowarnerror means that you only send the highest-level logs to Better Stack.off means that you don't send any logs to Better Stack.For example, to send all logs except for debug logs to Better Stack:
To send Web Vitals to Better Stack, add the BetterStackWebVitals component to the app/layout.tsx file:
To capture routing errors, use the error handling mechanism of Next.js:
app folder.error.tsx file.useLogger to send the error to Better Stack. For example:You can use log.with to create an intermediate logger, for example:
Send traces, metrics, and logs from your Next.js app to Better Stack using OpenTelemetry with the @vercel/otel package.
Install the packages:
Create an instrumentation.ts file in the root of your project, or in the src folder if you use one:
Next.js runs register on server start. You should see your data in Better Stack → Live tail.
For more instrumentation options, see our OpenTelemetry guide.
@logtail/next switched to support the App Router starting with version 0.2.0.
If you are upgrading a Pages Router app with @logtail/next v0.1.x to the App Router, you will need to make the following changes:
@logtail/next to version 0.2.0 or higherNEXT_PUBLIC_ prefix, e.g: NEXT_PUBLIC_BETTER_STACK_SOURCE_TOKENuseLogger hook in client components instead of log propLogger and flush the logs before component returns.reportWebVitals() and instead add the BetterStackWebVitals component to your layout.We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.