Cabin is in alpha development, join us in Slack. Our web-based dashboard will be available soon!
Table of Contents
Install
npm:
npm install cabin
yarn:
yarn add cabin
Usage
Don't want to configure this yourself? You can simply use Lad which has this all built-in for you already!
-
Simply include the middleware and pass options if needed
-
Use ctx.log(level, message, meta)
(Koa) or req.log(level, message, meta)
(Express) to log messages anywhere in your app middleware functions.
-
You can also use shorthand method level calling, such as ctx.log.debug(msg, meta)
, ctx.log.info(msg, meta)
, ctx.log.warn(msg, meta)
, or ctx.log.error(msg, meta)
for Koa (and req.log
equivalents for Express), which automatically populate the level
argument with the respective level.
Koa
const Koa = require('koa');
const Cabin = require('cabin');
const app = new Koa();
const cabin = new Cabin();
app.use(cabin.middleware);
app.get('/logout', ctx => {
ctx.log.warn('Logged out');
ctx.logout();
ctx.redirect('/');
});
app.listen(3000);
Express
const express = require('express');
const Cabin = require('cabin');
const app = express();
const cabin = new Cabin();
app.use(cabin.middleware);
app.get('/logout', (req, res) => {
req.log.warn('Logged out');
req.logout();
res.redirect('/');
});
app.listen(3000);
Options
logger
(Object) - defaults to console
(you might want to use something like Lad's logger)userFields
(Array) - defaults to [ 'id', 'email', 'full_name']
- these are the default fields to store from a parsed user object
Metadata
By default all users (even ones not logged in) will have a user
object stored in the log metadata with their IP address stored as user.ip_address
.
If a logged in user is detected (e.g. if you're using Passport), then we add to the log metadata user
object the properties and user's respective values (if any) for each value specified in the userFields
option.
Also we add to log metadata by default a request
object with HTTP method
, query_string
, headers
, cookies
, body
(request body safe stringified using fast-safe-stringify), and url
properties.
Are we missing something? If so let us know by emailing niftylettuce@gmail.com or filing an issue on GitHub.
Related
Contributors
Trademark Notice
Lad, Lass, Cabin, Lipo, and their respective logos are trademarks of Niftylettuce LLC.
These trademarks may not be reproduced, distributed, transmitted, or otherwise used, except with the prior written permission of Niftylettuce LLC.
If you are seeking permission to use these trademarks, then please contact us.
License
MIT © Nick Baugh