This page explains how to send data from a JavaScript app to Axiom.
JavaScript is a versatile, high-level programming language primarily used for creating dynamic and interactive web content.
To send data from a JavaScript app to Axiom, use one of the following libraries of the Axiom JavaScript SDK:
The choice between these options depends on your individual requirements:
Capabilities | @axiomhq/js | @axiomhq/logging |
---|---|---|
Send data to Axiom | Yes | Yes |
Query data | Yes | No |
Capture errors | Yes | No |
Create annotations | Yes | No |
Transports | No | Yes |
Structured logging by default | No | Yes |
Send data to multiple places from a single function | No | Yes |
The @axiomhq/logging
library is a logging solution that also serves as the base for other libraries like @axiomhq/react
and @axiomhq/nextjs
.
The @axiomhq/js and the @axiomhq/logging libraries are part of the Axiom JavaScript SDK, an open-source project and welcomes your contributions. For more information, see the GitHub repository.
The @axiomhq/logging library is currently in public preview. For more information, see Feature states.
In your terminal, go to the root folder of your JavaScript app and run the following command:
Configure the environment variables in one of the following ways:
Export the API token as AXIOM_TOKEN
.
Pass the API token to the constructor of the client:
Install the Axiom CLI, and then run the following command:
The following example sends data to Axiom:
Replace DATASET_NAME
with the name of the Axiom dataset where you want to send data.
The client automatically batches events in the background. In most cases, call flush()
only before your application exits.
The following example queries data from Axiom:
Replace DATASET_NAME
with the name of the Axiom dataset where you want to send data.
For more examples, see the examples in GitHub.
To capture errors, pass a method onError
to the client:
By default, onError
is set to console.error
.
The following example creates an annotation:
In your terminal, go to the root folder of your JavaScript app and run the following command:
The following example sends data to Axiom:
The @axiomhq/logging
library includes the following transports:
ConsoleTransport
: Logs to the console.
AxiomJSTransport
: Sends logs to Axiom using the @axiomhq/js library.
ProxyTransport
: Sends logs the proxy server function that acts as a proxy between your application and Axiom. It’s particularly useful when your application runs on top of a server-enabled framework like Next.js or Remix.
Alternatively, create your own transports by implementing the Transport
interface:
The @axiomhq/logging
library includes the following logging levels:
debug
: Debug-level logs.info
: Informational logs.warn
: Warning logs.error
: Error logs.Formatters are used to change the content of a log before sending it to a transport. For example:
To send data to Axiom from JavaScript libraries and frameworks, see the following:
While the Axiom JavaScript SDK works on both the backend and the browsers, Axiom provides transports for some of the popular loggers: