Skip to main content
  1. All Posts/

cabin

Tools JavaScript



Cabin is the best self-hosted JavaScript and Node.js logging service.

Supports Node v14+, Browser Environments, Express, Koa, and Lad

Cabin is compatible with Sentry, Airbrake, Papertrail, Loggly, and Bugsnag.

Table of Contents

Foreword

Please defer to Axe’s Foreword for more insight.
Cabin is a layer on top of Axe that provides automatic logging for route middleware requests and errors.

Quick Start

npm install express axe cabin signale
const express = require('express');
const Axe = require('axe');
const Cabin = require('cabin');
const app = express();
const { Signale } = require('signale');

// initialize a new instance of Axe (see below TODO's that appeal to you)
const logger = new Axe({
  logger: new Signale()
});

// TODO: if you want to send logs to an HTTP endpoint then follow this guide:
// https://github.com/cabinjs/axe/#send-logs-to-http-endpoint

// TODO: if you want to send logs to Slack then follow this guide:
// https://github.com/cabinjs/axe/#send-logs-to-slack

// TODO: if you want to send logs to Sentry then follow this guide:
// https://github.com/cabinjs/axe/#send-logs-to-sentry

// TODO: if you want to send logs to Datadog then follow this guide:
// https://github.com/cabinjs/axe/#send-logs-to-datadog

// TODO: if you want to send logs to Papertrail then follow this guide:
// https://github.com/cabinjs/axe/#send-logs-to-papertrail

// TODO: if you want to suppress specific log metadata then follow this guide:
// https://github.com/cabinjs/axe/#suppress-logger-data

// initialize a new instance of Cabin with an Axe logger instance
const cabin = new Cabin({ logger });

//
// initialize route logging middleware
//
// NOTE: this will automatically log route middleware requests and errors
//
app.use(cabin.middleware);

app.get('/', (req, res, next) => res.send('OK'));

// start the server
app.listen(3000);
curl http://localhost:3000

Features

Security, Privacy, and Business Focused

Cabin will automatically detect and mask the following list of extremely sensitive types of data in your logs:

  • 1600+ Sensitive Field Names
  • Credit Card Numbers*
  • BasicAuth Headers
  • Social Security Numbers
  • JSON Web Tokens (“JWT”)
  • API Keys, CSRF Tokens, and Stripe Tokens
  • Passwords, Salts, and Hashes
  • Bank Account Numbers and Bank Routing Numbers

*Credit card numbers from the following providers are automatically detected and masked: Visa, Mastercard, American Express, Diners Club, Discover, JCB, UnionPay, Maestro, Mir, Elo, Hiper, Hipercard

Reduce Disk Storage Costs

Reduce your disk storage costs through Cabin’s automatic conversion of Streams, Buffers, and ArrayBuffers to simplified, descriptive-only objects that otherwise would be unreadable (and obviously pollute your log files and disk storage).

Before:

{
  "request": {
    "body": {
      "file": {
        "type": "Buffer",
        "data": [
          76,
          111,
          114,
          101,
          109,
          32,
          105,
          112,
          115,
          117,
          109,
          32,
          100,
          111,
          108,
          111,
          114,
          32,
          115,
          105,
          116,
          '...'
        ]
      }
    }
  }
}

After

{
  "request": {
    "body": {
      "file": {
        "type": "Buffer",
        "byteLength": 2787
      }
    }
  }
}

Cross-Platform and Cross-Browser Compatible

Cabin works with the most popular Node.js HTTP frameworks (e.g. Express and Koa), request body handling packages (e.g. multer and body-parser), and the passport authentication framework.
It supports Node v14+ and modern browsers out of the box (its browser-ready bundle is only 20 KB).

npx browserslist
and_chr 107
and_ff 106
and_qq 13.1
and_uc 13.4
android 107
chrome 107
chrome 106
chrome 105
edge 107
edge 106
edge 105
firefox 106
firefox 105
firefox 102
ios_saf 16.1
ios_saf 16.0
ios_saf 15.6
ios_saf 15.5
ios_saf 14.5-14.8
kaios 2.5
op_mini all
op_mob 64
opera 91
opera 90
safari 16.1
safari 16.0
safari 15.6
samsung 18.0
samsung 17.0

Send Logs to an HTTP endpoint

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#send-logs-to-http-endpoint.

Send Logs to Slack

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#send-logs-to-slack.

Send Logs to Sentry

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#send-logs-to-sentry.

Send Logs to Datadog

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#send-logs-to-datadog.

Send Logs to Papertrail

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#send-logs-to-papertrail.

Suppress Logger Data

See the Quick Start section above and our guide at https://github.com/cabinjs/axe/#suppress-logger-data.

Install

Note that as of v11.0.0 Cabin requires a peer dependency of Axe to be installed.
npm:

npm install cabin axe

Usage

Logging

const Cabin = require('cabin');
const cabin = new Cabin({
  // ... see the Quick Start and Options sections
});

cabin.info('hello world');
cabin.error(new Error('oops!'));

Route Logging Middleware

app.use(cabin.middleware);

See either the Node or Browser instructions below for further route middleware usage and proper setup.

Node

The examples below show how to use Cabin in combination with Axe, Signale (instead of console), and how to add an accurate X-Response-Time response time metric to your logs and response headers automatically.

Koa

  1. Install required and recommended dependencies:

    npm install koa cabin signale request-received koa-better-response-time koa-better-request-id
  2. Implement the example code below (also found here):

    const Koa = require('koa');
    const Cabin = require('cabin');
    const Router = require('koa-router');
    const requestReceived = require('request-received');
    const responseTime = require('koa-better-response-time');
    const requestId = require('koa-better-request-id');
    const { Signale } = require('signale');
    

    const app = new Koa(); const router = new Router(); const cabin = new Cabin({ logger: new Signale() });

    // adds request received hrtime and date symbols to request object // (which is used by Cabin internally to add request.timestamp to logs app.use(requestReceived);

    // adds X-Response-Time header to responses app.use(responseTime());

    // adds or re-uses X-Request-Id header app.use(requestId());

    // use the cabin middleware (adds request-based logging and helpers) app.use(cabin.middleware);

    // add your user/session management middleware here (e.g. passport) // …

    // an example home page route router.get(’/’, ctx => { ctx.logger.info(‘visited home page’); ctx.body = ‘hello world’; });

    // this assumes that you are using passport which // exposes ctx.logout to log out the logged in user router.get(’/logout’, ctx => { ctx.logger.warn(‘Logged out’); ctx.logout(); ctx.redirect(’/’); });

    app.use(router.routes()); app.use(router.allowedMethods());

    app.listen(3000, () => { cabin.info(‘app started’); });

  3. See Koa convenience methods below for helper utilities you can use while writing code.

Express

  1. Install required and recommended dependencies:

    npm install express cabin signale request-received response-time express-request-id
  2. Implement the example code below (also found here):

    const express = require('express');
    const Cabin = require('cabin');
    const requestReceived = require('request-received');
    const responseTime = require('response-time');
    const requestId = require('express-request-id');
    const { Signale } = require('signale');
    

    const app = express(); const cabin = new Cabin({ logger: new Signale() });

    // adds request received hrtime and date symbols to request object // (which is used by Cabin internally to add request.timestamp to logs app.use(requestReceived);

    // adds X-Response-Time header to responses app.use(responseTime());

    // adds or re-uses X-Request-Id header app.use(requestId());

    // use the cabin middleware (adds request-based logging and helpers) app.use(cabin.middleware);

    // add your user/session management middleware here (e.g. passport) // …

    // an example home page route app.get(’/’, (req, res) => { req.logger.info(‘visited home page’); res.send(‘hello world’); });

    // this assumes that you are using passport which // exposes req.logout to log out the logged in user app.get(’/logout’, (req, res) => { req.logger.warn(’logged out’); req.logout(); res.redirect(’/’); });

    app.listen(3000, () => { cabin.info(‘app started’); });

  3. See Express convenience methods below for helper utilities you can use while writing code.

Convenience Methods

In order to easily interact and use the logger utility function exposed by app.use(cabin.middleware), we expose convenient helper methods in Express and Koa:

Express
  • req.log
  • req.logger
  • res.log
  • res.logger
Koa
  • ctx.log
  • ctx.logger
  • ctx.request.log
  • ctx.request.logger
  • ctx.response.log
  • ctx.response.logger

Browser

This package requires Promise support, therefore you will need to polyfill if you are using an unsupported browser (namely Opera mini).
We no longer support IE as of Cabin v10.0.0+.

VanillaJS

This is the solution for you if you’re just using <script> tags everywhere!

<script src="https://polyfill.io/v3/polyfill.min.js?features=Promise"></script>
<script src="https://unpkg.com/cabin"></script>
<script type="text/javascript">
  (function() {
    var cabin = new Cabin();
    cabin.setUser({
      id: '1',
      email: 'test@example.com',
   ...