This page explains how to send data from your Next.js app to Axiom.
Event type | Axiom Vercel app | next-axiom library | @axiomhq/nextjs library |
---|---|---|---|
Application logs | Yes | Yes | Yes |
Web Vitals | No | Yes | Yes |
HTTP logs | Yes | Soon | Yes |
Build logs | Yes | No | No |
Tracing | Yes | No | Yes |
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.next.config.ts
file, wrap your Next.js configuration in withAxiom
:
middleware.ts
file in the root folder of your Next.js app:
AxiomWebVitals
component from next-axiom to the app/layout.tsx
file:
fields
object.
withAxiom
to add a logger to your request and log exceptions automatically:
useLogger
from next-axiom to your component:
Logger
from next-axiom to your component, and call flush before returning:
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.app
folder.error.tsx
file.useLogger
from next-axiom to send the error to Axiom. For example:log.with
to create an intermediate logger. For example:
lib/axiom
to store configurations for Axiom.
axiom.ts
file in the lib/axiom
folder with the following content:
lib/axiom
folder, create a server.ts
file with the following content:
createAxiomRouteHandler
is a builder function that returns a wrapper for your route handlers. The wrapper handles successful responses and errors thrown within the route handler. For more information on the logger, see the @axiomhq/logging library.
lib/axiom
folder, create a client.ts
file with the following content:
middleware.ts
file in the root folder of your Next.js app with the following content:
WebVitals
component to the app/layout.tsx
file:
fields
object.
withAxiom
function exported from the setup file in lib/axiom/server.ts
to wrap your route handlers.
useLogger
to your component:
onRequestError
option. Create an instrumentation.ts
file in the src
or root folder of your Next.js app (depending on your configuration) with the following content:
onRequestError
function:
error.tsx
file in the app
folder.
useLogger
to send the error to Axiom. For example:
client.ts
file in the lib/axiom
folder with the following content:
/app/api/axiom
folder, create a route.ts
file with the following content. This example uses /api/axiom
as the Axiom proxy path.
onError
and onSuccess
functions that the createAxiomRouteHandler
function accepts in the configuration object.
In the lib/axiom/server.ts
file, use the transformRouteHandlerErrorResult
and transformRouteHandlerSuccessResult
functions to customize the data sent to Axiom by adding fields to the report object:
transformSuccessResult()
or transformErrorResult()
functions can change the shape of your data. This can affect dashboards (especially auto-generated dashboards) and other integrations.Axiom recommends you add fields on top of the ones returned by the default transformSuccessResult()
or transformErrorResult()
functions, without replacing the default fields.transformSuccessResult()
or transformErrorResult()
functions:
redirect()
function are logged as info
.forbidden()
, notFound()
and unauthorized()
functions are logged as warn
.logLevelByStatusCode()
function when logging errors from your route handler:
transformErrorResult()
function using a getNextErrorStatusCode()
function. To compose these functions yourself, create your own getNextErrorStatusCode()
function and inject the result into the transformErrorResult()
report.
serverContextFieldsFormatter
function included in the nextJsFormatters
adds the server execution context to the logs, this is useful to have information about the scope where the logs were generated.
By default, the createAxiomRouteHandler
function adds a request_id
field to the logs using this server context and the server context fields formatter.
createAxiomRouteHandler
accepts a store
field in the configuration object. The store can be a map, an object, or a function that accepts a request and context. It returns a map or an object.
The fields in the store are added to the fields
object of the log report. For example, you can use this to add a trace_id
field to every log report within the same function execution in the route handler.