- The two options can collect different event types.
Event type Axiom Vercel app next-axiom library Application logs Yes Yes Web Vitals No Yes HTTP logs Yes Soon Build logs Yes No - If you already use Vercel for deployments, the Axiom Vercel app can be easier to integrate into your existing experience.
- The cost of these options can differ widely depending on the volume of data you transfer. The Axiom Vercel app depends on Vercel Log Drains, a feature that’s only available on paid plans. For more information, see the blog post on the changes to Vercel Log Drains.
Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to ingest data to the dataset.
- A new or existing Next.js app.
Install next-axiom
- In your terminal, go to the root folder of your Next.js app, and then run
npm install --save next-axiom
to install the latest version of next-axiom. - Add the following environment variables to your Next.js app:
NEXT_PUBLIC_AXIOM_DATASET
is the name of the Axiom dataset where you want to send data.NEXT_PUBLIC_AXIOM_TOKEN
is the Axiom API token you have generated.
- In the
next.config.ts
file, wrap your Next.js configuration inwithAxiom
:
Capture traffic requests
To capture traffic requests, create amiddleware.ts
file in the root folder of your Next.js app:
Web Vitals
To send Web Vitals to Axiom, add theAxiomWebVitals
component from next-axiom to the app/layout.tsx
file:
Logs
Send logs to Axiom from different parts of your app. Each log function call takes a message and an optionalfields
object.
Route handlers
Wrap your route handlers inwithAxiom
to add a logger to your request and log exceptions automatically:
Client components
To send logs from client components, adduseLogger
from next-axiom to your component:
Server components
To send logs from server components, addLogger
from next-axiom to your component, and call flush before returning:
Log levels
The log level defines the lowest level of logs sent to Axiom. Choose one of the following levels (from lowest to highest):debug
is the default setting. It means that you send all logs to Axiom.info
warn
error
means that you only send the highest-level logs to Axiom.off
means that you don’t send any logs to Axiom.
Capture errors
To capture routing errors, use the error handling mechanism of Next.js:- Go to the
app
folder. - Create an
error.tsx
file. - Inside your component function, add
useLogger
from next-axiom to send the error to Axiom. For example:
Extend logger
To extend the logger, uselog.with
to create an intermediate logger. For example: