Explore documentation
Installing Logtail JavaScript client
Logtail allows you to collect logs directly from both backend and frontend code, of your Node.js application. Supported language versions are:
- Node.js 12 or newer
Installing Logtail Node.js client
Both backend and frontend
If your project contains both Node.js backend code and frontend code as well, you can use universal package @logtail/js
which combines the node
and browser
packages:
First, install the Logtail library using the npm
package manager:
npm install @logtail/js
Then import or require it in your code:
// in your backend code:
const { Node: Logtail } = require("@logtail/js");
// in your frontend code:
import { Browser as Logtail } from "@logtail/js";
Backend only
Install Logtail library for your Node.js backend code using the npm
package manager.
npm install @logtail/node
Then you can either require it or import it into your codebase
// require logtail class
const { Logtail } = require("@logtail/node");
// or import if you're using ES modules:
import { Logtail } from "@logtail/node";
Frontend only
Install Logtail library for your frontend code using the npm package manager:
npm install @logtail/browser
Then import the Logtail class:
// import logtail class
import { Logtail } from "@logtail/browser";
Another option is to use Content Delivery Network. You can import Logtail using the CDN by adding the following line into a page header.
<script src="https://unpkg.com/browse/@logtail/[email protected]/dist/umd/logtail.js"></script>
How it works in action
Here is an example of how to send logs from your Node.js application to Logtail.
For a more detailed example, please see the JavaScript + Node.js example project on GitHub .
const { Logtail } = require("@logtail/node");
const logtail = new Logtail("<source-token>");
logtail.info("logtail integration is ready");
logtail.warn("log structured data", {
item: {
url: "https://fictional-store.com/item-123",
price: 100.00
}
});
{
"dt": "2021-03-29T11:24:54.788Z",
"level": "info",
"message": "logtail integration is ready"
}
{
"dt": "2021-03-29T11:24:54.788Z",
"level": "warn",
"message": "log structured data",
"item": {
"url": "https://fictional-store.com/item-123",
"price": 100.00
}
}