Universal winston SQL transport.
Supports:
- MySQL
- PostgreSQL
- SQL Server
via knex library.
$ npm install winston
$ npm install winston-sql-transport
and then install the appropriate database library: mysql2 for MySQL or MariaDB, pg for PostgreSQL, or tedious for MSSQL.
// CommonJS
const winston = require('winston');
const { SqlTransport } = require('winston-sql-transport');
// ES Modules
import winston from 'winston';
import { SqlTransport } from 'winston-sql-transport';
`level` = 'info', Level at which to log the message.
`name` = 'SqlTransport', Name for transport
`silent` = false, Suppress logs.
`tableName` = 'winston_logs', Name for database table
`defaultMeta` (optional), Will be added by default to meta for all logs;
const const transportConfig = {
client: 'mssql',
connection: {
user: MSSQL_USER,
password: MSSQL_PASSWORD,
server: MSSQL_HOST,
database: MSSQL_DB,
},
}
const transportConfig = {
client: 'mysql2',
connection: {
host: MYSQL_HOST,
port: MYSQL_PORT,
user: MYSQL_USER,
password: MYSQL_PASSWORD,
database: MYSQL_DATABASE,
},
};
const transportConfig = {
client: 'pg',
connection: `postgres://${PGUSER}\
:${PGPASSWORD}\
@${PGHOST}\
:${PGPORT}\
/${PGDATABASE}`,
};
const transportConfig = {
client: 'mssql',
connection: {
user: MSSQL_USER,
password: MSSQL_PASSWORD,
server: MSSQL_HOST,
database: MSSQL_DB,
},
defaultMeta: { example_winston_logs: true },
name: 'ExampleSqlTransport',
tableName: 'winston_logs',
};
const logger = winston.createLogger({
format: winston.format.json(),
transports: [new SqlTransport(transportConfig)],
});
logger.log({
level: 'info',
message: 'Hello there.',
});
(async () => {
const transport = new SqlTransport(transportConfig);
await transport.init();
})();
This transport supports querying of logs with Loggly-like options.
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);
});
The tests are written in jest, and designed to be run with npm.
$ npm test