A PostgreSQL Transport for Winston Logger (v3.x).
Uses the node-postgres package, and does
not require native binding (libpq
) to be installed on the host
machine.
Inspired by winston-pg-native.
You must have a table that matches the following description:
CREATE TABLE logs (
timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(),
level VARCHAR,
message VARCHAR,
meta JSON
);
npm i winston-transports-pg
To create an instance of the PostgresTransport, we need two things:
- A
pg
Pool instance - An IPostgresTransportOptions object
Below is the interface for the IPostgresTransportOptions
object.
interface IPostgresTransportOptions {
level?: string;
tableName: string;
}
import { Pool } from 'pg';
import { createLogger, format } from 'winston';
// First create a Pool instance.
const pool = new Pool('postgres://username:password@hostname:5432/database');
// Set up our options.
const opts = {
level: 'info',
tableName: 'logs',
};
// Create the instance.
const postgresTransport = new PostgresTransport(pool, opts);
// Add Transport to Logger
const logger = createLogger({
format: format.json(),
transports: [ postgresTransport ]
});