https://github.com/snigbhaumik/winston-mssql-transport
A slick and simple MS SQL Server transport for Winston Logger
https://github.com/snigbhaumik/winston-mssql-transport
Last synced: about 2 months ago
JSON representation
A slick and simple MS SQL Server transport for Winston Logger
- Host: GitHub
- URL: https://github.com/snigbhaumik/winston-mssql-transport
- Owner: SnigBhaumik
- License: mit
- Created: 2024-08-26T14:02:25.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-08-27T14:05:02.000Z (10 months ago)
- Last Synced: 2025-04-19T14:59:56.183Z (2 months ago)
- Language: JavaScript
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
GOSET QUOTED_IDENTIFIER ON
GOCREATE 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]
GOALTER 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](http://en.wikipedia.org/wiki/MIT_License).