npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bunyan-cassandra

v0.0.4

Published

Cassandra bunyan stream

Downloads

5

Readme

bunyan-cassandra

Bunyan stream for saving logs to Cassandra

Installation

npm install bunyan-cassandra

Example

Basic example:

var CassandraStream = require('bunyan-cassandra');

var logger = bunyan.createLogger({
  src: true,
  name: 'name',
  streams: [{
    level: 'debug',
    stream: new CassandraStream({
      hosts: ['localhost'],
      keyspace: 'mykeyspace',
      username: 'myuser',
      password: 'mypass',
      query: 'INSERT INTO log (id, message) VALUES(uuid(), ?)',
      args: ['msg']
    })
  }]
});

// this mesage will be saved to cassandra
logger.debug('some log message');

Advanced example:

var CassandraStream = require('bunyan-cassandra');

var logger = bunyan.createLogger({
  src: true,
  name: 'name',
  streams: [{
    stream: process.stdout,
    level: 'debug'
  }, {
    type: 'raw',
    level: 'debug',
    stream: new CassandraStream({
      hosts: ['localhost'],
      keyspace: 'mykeyspace',
      username: 'myuser',
      password: 'mypass',
      query: 'INSERT INTO log (id, message, ip, date, user, line, file, func) VALUES(uuid(), ?, ?, ?, ?, ?, ?, ?)',
      args: ['msg', 'ctx.ip', 'time', 'ctx.session.user.id', 'src.line', 'src.file', 'src.func'],
      callback: function (err, res) {
        // this callback is optional
        // here you can do something with query results
        // or catch possible Cassandra errors
      },
      transform: function (log) {
        // add/remove/edit log object
      }
    })
  }]
});

// ctx can be express request object for example
// it is completely up to you, you just need to configure ``query`` and ``args`` option
// to support provided object
logger.debug({
  ctx: {
    ip: '127.0.0.1',
    session: {
      user: {
        id: 1
      }
    }
  }
},'this is some log message');

Configuration

You can pass following options

hosts

Array with cassandra hosts

default ['localhost']

keyspace

Name of keyspace to use

default 'logs'

username

Cassandra username to use

default 'cassandra'

username

Cassandra password to use

default 'cassandra'

query

Query for inserting data to Cassandra. This will be executed for each log.

default 'INSERT INTO logs (id) VALUES (uuid())'

args

Array of object paths. When new log will be added to Cassandra, module will look for values on provided paths inside args option, and after that values with CQL query will be passed to Cassandra, and new record will be added.

For example if your bunyan log entry looks like this:

{
  msg: 'this is some log message',
  ctx: {
    ip: '127.0.0.1',
    session: {
      id: 2
    }
  },
  level: 20
}

Then if you have CQL query like this:

query: 'INSERT INTO logs (id, msg, ip, sessionid, level) VALUES (uuid(), ?, ?, ?, ?)'

you can populate ? with values if value of your args option is something like this:

args: ['msg', 'ctx.ip', 'ctx.session.id', 'level']

default []

callback

After query is executed callback function will be called (if is defined). You can catch possible Cassandra errors here, or see result of operation.

default undefined

transform

Each time when new log is received, log will be passed as argument to transform function, so you will have opportunity to modify raw log data, for example add or remove some fields from log.

transform: function (row) {
  // let we say we want date with specific format
  row.date = moment(row.time).format('YYYY-MM-DD');
  return row;
}