A Winston transport for PostgreSQL. Based on on Postgres fully featured, lightweight PostgreSQL client for Node.js
$ 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(),
)
- 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',
};
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;
logger.log({
level: 'info',
message: 'Hello there.',
});
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);
});
The tests are written in jest, and designed to be run with npm.
$ npm test