1.0.4 • Published 2 years ago

winston-better-sqlite3 v1.0.4

Weekly downloads
6
License
CC0-1.0
Repository
github
Last release
2 years ago

winston-better-sqlite3

aka wbs

sqite3 Transport for Winston

Install

$ npm install winston-better-sqlite3

Use

const winston = require('winston');

const wbs = require('winston-better-sqlite3');
const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    transports: [
        new wbs({

            // 'db' is required, and should include the full 
            // path to the sqlite3 database file on the disk
            db: '<name of sqlite3 database file>',

            // The name of the table to use in the database.
            // Defaults to 'log'
            table: 'log',

            // A list of the log params to log. Defaults to 
            // ['level, 'message']. These params are used as 
            // columns in the sqlite3 table
            params: ['level', 'resource', 'query', 'message']
        })
    ]
});

There are a couple of sqlite3 transports for Winston out there, but this one is different. For one, it uses Josuha Wise's most excellent better-sqlite3 package. Second, in my biased opinion, wbs is also better because it uses no ORMs or any such middleware. It is just a plain, no-nonsense transport that writes the logs to a sqlite3 database. You have to provide the name of (and path to) the database file and optionally the table name (defaults to log), but the module creates the table (if it doesn't already exist) with the following four columns by default

CREATE TABLE IF NOT EXISTS log (
    id INTEGER PRIMARY KEY,
    timestamp INTEGER DEFAULT (strftime('%s','now')),
    level TEXT,
    message TEXT
);

Later on, in your program where you want to log something

// the following two messages will be logged
logger.log({
    level: 'info',
    resource: 'collaborators',
    query: 'q=punkish',
    message: 'searching for folks'
});

logger.log({
    level: 'error',
    resource: 'collaboratoradoras',
    query: 'q=normal',
    message: 'searching for state'
});

// the following message will not be logged because 
// its level is less important than the log level
logger.log({
    level: 'verbose',
    resource: 'funding',
    query: 'q=normal&something',
    message: 'searching for habbine'
});

Note: The user has to provide only the name of the sqlite3 database file. The module will create a database if it doesn't already exist (as sqlite3 always does), and will create a table of a given name (log by default) inside the database if the table doesn't already exist.

The id and timestamp columns are automatically inserted by sqlite3. The user can provide whatever others columns needed for tracking logging info. For example, I use level, resource, query and message in one of my projects (as shown in the example above). All logging params are stored in columns of datatype TEXT.