Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

winston-mssql-transport

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

winston-mssql-transport

A slick and simple MS SQL Server transport for Winston Logger

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

winston-mssql-transport

A slick, simple and practical MS SQL Server transport for Winston Logger.

Objective

Winston (https://www.npmjs.com/package/winston) is one of the most popular library around there. It offers some commonly used destinations (transports) such as File, HTTP etc.

And, of course, many of us want the logs to be transported in a database like MySQL, MS-SQL, Oracle and so on.

While there are some packages available for Winston MySQL transport (for example, https://github.com/charles-zh/winston-mysql), no reliable and supported package found for MS SQL Server. One such package winston-sql-transport (https://www.npmjs.com/package/winston-sql-transport) unfortunately is deprecated.

Thus the need of a simple and supported plug-in for Winston MS SQL Server transport.

Supports

  • Winston 3.x
  • MS SQL 2018 onwards

Setup

Before using the transport, the target database and table must be created.

Assuming your log table name is winston_logs, here is the DDL statement.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[winston_logs](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [level] [nvarchar](16) NOT NULL,
    [message] [nvarchar](max) NOT NULL,
    [meta] [nvarchar](max) NOT NULL,
    [timestamp] [datetime] NOT NULL,
CONSTRAINT [PK_winston_logs] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[winston_logs] ADD  CONSTRAINT [DF_winston_logs_timestamp]  DEFAULT (getdate()) FOR [timestamp]
GO

Installation

// CommonJS

npm i winston --save
npm i winston-mssql-transport --save

Usage

const winston = require('winston');
const format = require('winston').format;
const wstnMsSql = require('winston-mssql-transport');

const options = {
    user: 'my-db-user-name',
    password: 'my-db-password',
    server: 'my-db-server',
    database: 'my-db-name',	
    table: 'my-table-name',
};

const sqlTransport = new wstnMsSql(options);
const sqlLogger = winston.createLogger({
    transports: [ sqlTransport ],
    format: format.combine(
	    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
	    format.errors({ stack: true }),
	    format.json()
    ),
    level: 'silly'
});

sqlLogger.debug('my message', { data: 'my-meta-data' });

Advanced Options

You can configure the SQL Server connection pool settings.

const options = {
    ...
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 30000
    }
};

In case you want logging status in console.

const options = {
    ...
    console: true   // default false
}

Querying Logs

You can query the stored logs as standard winston query format. options is optional, default values as mentioned below would be used. This will return SQL server recordset.

const options = {
    limit: 10,              // optional, default 100
    order: 'ASC',           // optional, default DESC
    from: '2024-04-04',     // optional
    until: '2024-06-04',    // optional
    fields: [ 'message', 'meta' ]   // optional, default both `message` and `meta`
};

logger.query(options, (err, recs) => {
    console.error(err);
    console.log(recs);
});  

License

MIT License.

Keywords

FAQs

Package last updated on 27 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc