JavaScript quick start guide

Where do you want to collect your JavaScript logs from?

Logging from Node.js

Collect logs from the backend code of your application.

1. Install

Install the @logtail/node npm package:

Install @logtail/node
npm install @logtail/node

2. Set up

Set up the Better Stack client:

Set up the client
const { Logtail } = require("@logtail/node");
const logtail = new Logtail("$SOURCE_TOKEN", {
  endpoint: 'https://$INGESTING_HOST',
});


3. Start logging

Send logs to Better Stack
logtail.error("Something bad happend.");
logtail.info("Log message with structured data.", {
    item: "Orange Soda",
    price: 100.00
});

// Ensure that all logs are sent to Logtail
logtail.flush()

You should see your logs in Better Stack → Live tail.

Node.js 12 or higher is required

Logging from browser

Collect logs from your frontend code.

1. Install

Install the @logtail/browser npm package:

Install @logtail/browser
npm install @logtail/browser

2. Set up

Set up the Better Stack client:

Set up the client
import { Logtail } from "@logtail/browser";
const logtail = new Logtail("$SOURCE_TOKEN", {
  endpoint: 'https://$INGESTING_HOST',
});


Prefer using a Content Delivery Network?
Add the Better Stack import to your HTML page:

Import Better Stack
<script src="https://cdnjs.cloudflare.com/ajax/libs/logtail-browser/0.4.19/dist/umd/logtail.min.js" integrity="sha512-EYdnWL7Lrn+96R4BDaOxCgCWWOoZQiMSsfy+3NKYSJxK/CAt6y7cPLvGHRWONYO8Y0SsGuk5Y+lC1w3up7f7OA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

3. Start logging

Send logs to Better Stack
logtail.error("Something bad happend.");
logtail.info("Log message with structured data.", {
    item: "Orange Soda",
    price: 100.00
});

// Ensure that all logs are sent to Logtail
logtail.flush()

You should see your logs in Better Stack → Live tail.

Logging from browser and Node.js

Collect logs from both backend and frontend code of your Node.js application.

1. Install

Install the @logtail/js npm package:

Install @logtail/js
npm install @logtail/js

2. Set up

Set up the Better Stack client in backend and frontend code:

Backend code Frontend code
const { Node: Logtail } = require("@logtail/js");
const logtail = new Logtail("$SOURCE_TOKEN", {
  endpoint: 'https://$INGESTING_HOST',
});
import { Browser as Logtail } from "@logtail/js";
const logtail = new Logtail("$SOURCE_TOKEN", {
  endpoint: 'https://$INGESTING_HOST',
});


3. Start logging

Log the same way in backend and frontend:

Send logs to Better Stack
logtail.error("Something bad happend.");
logtail.info("Log message with structured data.", {
    item: "Orange Soda",
    price: 100.00
});

// Ensure that all logs are sent to Logtail
logtail.flush()

You should see your logs in Better Stack → Live tail.

Node.js 12 or higher is required

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! 🙏

Additional information

NPM packages

Package @logtail/js provides a convenient way to install and manage both @logtail/node and @logtail/browser NPM packages. You can install the packages separately if you wish to.