JavaScript
Collect logs from both Node.js backend and your frontend.

Example

It's very easy to start sending logs to Logtail:
1
const { Logtail } = require("@logtail/node");
2
​
3
const logtail = new Logtail("<source-token>");
4
​
5
logtail.info("logtail integration is ready");
6
logtail.warn("log structured data", {
7
item: {
8
url: "https://fictional-store.com/item-123",
9
price: 100.00
10
}
11
});
Copied!
As a result, the following JSON rows will be ingested:
1
{
2
"dt": "2021-03-29T11:24:54.788Z",
3
"level": "info",
4
"message": "logtail integration is ready"
5
}
6
​
7
{
8
"dt": "2021-03-29T11:24:54.788Z",
9
"level": "warn",
10
"message": "log structured data",
11
"item": {
12
"url": "https://fictional-store.com/item-123",
13
"price": 100.00
14
}
15
}
Copied!

Installation

Node.js
Frontend (NPM)
Frontend (UMD)
Simply install the library from NPM:
1
npm install @logtail/node
Copied!
Then you can require it or import in your codebase:
1
// start by loading the Logtail class
2
const { Logtail } = require("@logtail/node");
3
​
4
// ... or if you're using ES modules:
5
import { Logtail } from "@logtail/node";
Copied!
If your project contains both Node.js and frontend code, you can use our universal package @logtail/js which combines the node and browser packages:
1
// in your backend code:
2
const { Node: Logtail } = require("@logtail/js");
3
​
4
// in your frontend code:
5
import { Browser as Logtail } from "@logtail/js";
Copied!
​
You can simply install the library from NPM:
1
npm install @logtail/browser
Copied!
... and import it into your JavaScript code as you're used to:
1
import { Logtail } from "@logtail/browser";
Copied!
You can use the UMD build directly from a CDN:
1
<script src="https://unpkg.com/browse/@logtail/[email protected]/dist/umd/logtail.js"></script>
Copied!

Integrations

We provide integrations with several existing frameworks and logging solutions.
Koa
Winston
Bunyan
Install the following @logtail/koa NPM package and then attach the Logtail instance as a middleware to your Koa instance:
1
const Koa = require("koa");
2
const { Logtail } = require("@logtail/koa");
3
​
4
// Create a new Koa instance
5
const koa = new Koa();
6
​
7
// Create a new Logtail client
8
const logtail = new Logtail("logtail-source-token");
9
​
10
// Attach Koa to enable HTTP request logging
11
logtail.attach(koa);
Copied!

How the middleware works

Successful requests

A successful request is one that returns a non-4xx or 5xx status code, and doesn't throw any uncaught errors while handling the requests. These are logged to Logtail using LogLevel.Info​

4xx status codes

These are not considered errors but warnings and log with the same message using LogLevel.Warn​

5xx status codes

Responses that contain a 5xx status code are considered errors and are logged with LogLevel.Error​

Additional logging

Since this Koa plugin extends the regular @logtail/node logger, you can use the .log|info|warn|error functions as normal to handle logging anywhere in your app.
Install the @logtail/winston NPM package and set up the Logtail transport according to the following example:
1
const winston = require("winston");
2
const { Logtail } = require("@logtail/node");
3
const { LogtailTransport } = require("@logtail/winston");
4
​
5
// Create a Logtail client
6
const logtail = new Logtail("logtail-source-token");
7
​
8
// Create a Winston logger - passing in the Logtail transport
9
const logger = winston.createLogger({
10
transports: [new LogtailTransport(logtail)],
11
});
12
​
13
// Log as normal in Winston - your logs will sync with Logtail.com!
14
logger.log({
15
level: "info", // <-- will use Logtail's `info` log level,
16
message: "Some message", // <-- will also be passed to Logtail
17
});
Copied!
Install the @logtail/bunyan NPM package and set up the Logtail stream according to the following example:
1
const bunyan = requrie("bunyan");
2
const { Logtail } = require("@logtail/node");
3
const { LogtailStream } = require("@logtail/bunyan");
4
​
5
// Create a Logtail client
6
const logtail = new Logtail("logtail-source-token");
7
​
8
// Create a Bunyan logger - passing in the Logtail stream
9
const logger = bunyan.createLogger({
10
name: "Example logger",
11
level: "debug",
12
streams: [
13
{
14
stream: new LogtailStream(logtail),
15
},
16
],
17
});
18
​
19
// Log as normal in Bunyan - your logs will be sent to Logtail.com
20
logger.info("Hello from Bunyan");
Copied!

Usage

Setting up the integration is very easy. All you need is a source-token that you'll get after you create a Source in your logtail.com account. With the source token at hand, you can create a Logtail instance and start logging:
1
const logtail = new Logtail("<your-source-token>");
2
​
3
logtail.info("simple log message");
4
logtail.warn("warning with additional information", {
5
resource: {
6
type: "store-item",
7
id: 123456,
8
inStock: false
9
}
10
});
Copied!
The first argument is either a string message or an instance of Error. The second argument is an optional object, which can contain a tree structure with string, numeric, boolean, and Date values.
There are four levels of log messages: debug, info, warn, and error. You can either log by calling the respective functions on the Logtail object, or you can pass the level as an additional parameter to the generic log function:
1
import { LogLevel } from "@logtail/types";
2
// note: you don't need to install any additional packages
3
​
4
logtail.log("this is important", LogLevel.Error);
Copied!

Middleware

You can intercept every logged item and modify it before it's pushed to Logtail. This could be useful for example for adding the current user's ID to the log:
1
async function enrichLogs(log) {
2
return {
3
...log,
4
userId: getCurrentUserId()
5
};
6
}
7
​
8
logtail.use(enrichLogs);
Copied!

TypeScript support

If you're using Logtail in a TypeScript codebase, you can take advantage of our types. The custom middleware function from the previous example could look like this:
1
import { ILogtailLog } from "@logtail/types";
2
​
3
async function enrichLogs(log: ILogtailLog): Promise<ILogtailLog> {
4
return {
5
...log,
6
userId: getCurrentUserId()
7
};
8
}
Copied!
Last modified 7mo ago