A Winston 3.X compatible transport for PostgreSQL. Uses high performance of native bindings via libpq.
$ npm install winston
$ npm install @pauleliet/winston-pg-native
You must have a table in your PostgreSQL database, for example:
CREATE TABLE winston_logs
(
timestamp timestamp without time zone DEFAULT now(),
level character varying,
message character varying,
meta json
)
- connectionString: The PostgreSQL connection string. Required.
- level: The winston's log level. Optional, default: info
- poolConfig: Pool specific configuration parameters. Optional.
- tableName: PostgreSQL table name definition. Optional.
See the default values used:
const options = {
connectionString: 'postgres://username:password@localhost:5432/database',
level: 'info',
poolConfig: {
// number of milliseconds to wait before timing out when connecting a new client
// by default this is 0 which means no timeout
connectionTimeoutMillis: 0,
// number of milliseconds a client must sit idle in the pool and not be checked out
// before it is disconnected from the backend and discarded
// default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients
idleTimeoutMillis: 10000,
// maximum number of clients the pool should contain
// by default this is set to 10.
max: 10
},
tableName: 'winston_logs'
};
const { Logger } = require('winston');
const Postgres = require('winston-pg-native');
const logger = new Logger({
transports: [
new Postgres({
connectionString,
level: 'info',
poolConfig: {
connectionTimeoutMillis: 0,
idleTimeoutMillis: 0,
max: 10
},
tableName: 'winston_logs'
})
]
});
module.exports = logger;
logger.log('info', 'message', {});
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(),
start: 0,
limit: 10,
order: 'desc'
};
//
// Find items logged between today and yesterday.
//
logger.query(options, (err, results) => {
if (err) {
throw err;
}
console.log(results);
});
Streaming allows you to stream your logs back
//
// Start at the end.
//
logger.stream({ start: -1 }).on('log', log => {
console.log(log);
});
The tests are written in vows, and designed to be run with npm.
$ npm test