bunyan-sql-stream

Stream your Bunyan logs to your favorite SQL database

Usage no npm install needed!

<script type="module">
  import bunyanSqlStream from 'https://cdn.skypack.dev/bunyan-sql-stream';
</script>

README

bunyan-sql-stream

Build Status NPM Version Coverage

This package creates a Bunyan stream that can write to a number of different SQL databases.

Currently, it supports:

  • PostgreSQL
  • MSSQL
  • MySQL
  • MariaDB
  • SQLite3
  • Oracle

The stream maps the default Bunyan log fields to table columns and also stores the entire log message. Depending on your database's capabilities, it will attempt to store the message in a json, jsonb, or text column type.

Installation

npm install --save bunyan-sql-stream

Usage

First, create the table you want to store your logs in:

create table if not exists "public"."logs" (
  "id" serial primary key,
  "name" text,
  "level" integer,
  "hostname" text,
  "msg" text,
  "pid" integer,
  "time" timestamptz,
  "content" jsonb
)

Then, use the package as a Bunyan stream:

const logger = bunyan.createLogger({
  name: 'sql stream',
  level: 'info',
  stream: bunyanSqlStream({
    client: 'pg',
    connection: {
      host: 'localhost',
      user: 'postgres',
      password: 'password',
      database: 'db'
    },
    tableName: 'logs'
  })
})

This package uses the same client and connection options as Knex.js.

Database Clients

You'll also need to install a database client for each type of database you'll be logging to.

For example, if you're using PostgreSQL:

npm install --save pg

The package should issue an error message that will tell you which module to install once you set the client option.

Additional Info

This package depends on Knex.js. If want to minimize dependencies, you might prefer another module:

Contributing

Pull requests welcome.

License

MIT © Forrest Desjardins