https://github.com/fdesjardins/bunyan-postgres-stream
A bunyan stream for PostgreSQL
https://github.com/fdesjardins/bunyan-postgres-stream
bunyan bunyan-stream logging nodejs postgresql
Last synced: about 1 year ago
JSON representation
A bunyan stream for PostgreSQL
- Host: GitHub
- URL: https://github.com/fdesjardins/bunyan-postgres-stream
- Owner: fdesjardins
- License: mit
- Created: 2016-08-05T03:59:00.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T02:35:16.000Z (over 3 years ago)
- Last Synced: 2025-03-22T22:04:47.419Z (over 1 year ago)
- Topics: bunyan, bunyan-stream, logging, nodejs, postgresql
- Language: JavaScript
- Size: 1.29 MB
- Stars: 3
- Watchers: 2
- Forks: 9
- Open Issues: 18
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# bunyan-postgres-stream
[](https://travis-ci.org/fdesjardins/bunyan-postgres-stream)
[](https://www.npmjs.org/package/bunyan-postgres-stream)
[](https://coveralls.io/github/fdesjardins/bunyan-postgres-stream?branch=master)
Store your Bunyan logs in PostgreSQL.
This module creates a Bunyan stream that maps the default log fields to table columns and also records the entire log message in a `jsonb` column to support your custom fields.
Requires PostgreSQL 9.4 or above for use with `jsonb` column types.
## Install
```
$ npm install --save bunyan-postgres-stream
```
## Usage
First, create the table you want to store your logs in:
```sql
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:
```js
const bunyan = require('bunyan')
const bunyanPostgresStream = require('./')
const stream = bunyanPostgresStream({
connection: {
host: 'localhost',
user: 'postgres',
password: 'password',
database: 'db'
},
tableName: 'logs'
})
const log = bunyan.createLogger({
name: 'postgres stream',
level: 'info',
stream
})
log.info('something happened')
// explicity dispose of the database connection pool
stream.end()
```
## API
### bunyanPostgresStream(options)
#### options
##### connection
Type: `object`
One of the following:
- a valid node-postgres connection options object
- an initialized knex.js instance (see [examples/knex.js](./examples/knex.js))
##### tableName
Type: `string`
The name of the table that will contain the logs.
## Contributing
Pull requests welcome.
## License
MIT © [Forrest Desjardins](https://github.com/fdesjardins)