1.2.0 • Published 3 years ago

winston-postgres-transport v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

winston-postgres-transport

CircleCI NPM version Dependency Status NPM Downloads

A Winston transport for PostgreSQL. Based on on Postgres fully featured, lightweight PostgreSQL client for Node.js

Installation

  $ npm install winston
  $ npm install winston-postgres-transport

You must have a table in your PostgreSQL database, for example:

CREATE TABLE winston_logs
(
  level character varying,
  message character varying,
  meta json,
  timestamp timestamp without time zone DEFAULT now(),
)

Options

  • level: The winston's log level. Optional, default: info
  • name: The winston's transport name. Optional, default: Postgres
  • postgresOptions: Postgres specific connection options. Optional.
  • postgresUrl: The PostgreSQL connection string. Required.
  • tableName: PostgreSQL table name definition. Optional, default winston_logs

See the default values used:

const options = {
  level: 'info',
  name: 'Postgres',
  postgresOptions: {
    // Is called with (connection, query, params)
    debug: console.log,
  },
  postgresUrl: 'postgres://username:password@localhost:5432/database',
  tableName: 'winston_logs',
};

Usage

const winston = require('winston');
const PostgresTransport = require('winston-postgres-transport');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new PostgresTransport({
      postgresUrl,
    }),
  ],
});

module.exports = logger;

Logging

logger.log({
  level: 'info',
  message: 'Hello there.',
});

Querying Logs

This transport supports querying of logs with Loggly-like options. See Loggly Search API

const options = {
  fields: ['message'],
  from: new Date() - 24 * 60 * 60 * 1000,
  until: new Date(),
  limit: 10,
  order: 'ASC',
};

//
// Find items logged between today and yesterday.
//
logger.query(options, (err, results) => {
  if (err) {
    throw err;
  }

  console.log(results);
});

Run Tests

The tests are written in jest, and designed to be run with npm.

  $ npm test

LICENSE

MIT License