Installing Logtail JavaScript client

154085622-59997d5a-3f91-4bc9-a815-3b8ead16d28d.jpeg

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
Copied!

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";
Copied!

Backend only

Install Logtail library for your Node.js backend code using the npm package manager.

npm install @logtail/node
Copied!

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";
Copied!

Frontend only

Install Logtail library for your frontend code using the npm package manager:

npm install @logtail/browser
Copied!

Then import the Logtail class:

// import logtail class
import { Logtail } from "@logtail/browser";
Copied!

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>
Copied!

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  .

Example: Snippet added to your app
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
    }
});
Copied!
Example: JSON rows sent to Logtail
{
    "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
    }
}
Copied!