Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

aldy505/winston-knex

Repository files navigation

Winston Transport for Knex

npm npm Codecov GitHub Release Date GitHub code size in bytes GitHub

This module is deprecated and no longer maintained. Please switch to other module or use Knex natively.

  • Supports Winston 3.x
  • Typescript typings built-in
  • Pure Knex

Installation

$ npm install winston-knex
# OR IF YOU PREFER YARN
$ yarn add winston-knex

Please also add your working database as dependency as described by Knex's documentation:

$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install oracledb
$ npm install mssql

Usage

const winston = require('winston')
const KnexTransport = require('winston-knex')

const logger = winston.createLogger({
  level: 'info',
  transports: [
    new KnexTransport({
      client: 'mysql',
      connection: 'mysql://user:password@host:port/dbname',
      tableName: 'anyname' // defaults to `logs`
      // this config also accepts any knex configuration key/params
    })
  ]
})

logger.error('This should inserted into the database')
logger.log({
  level: 'info',
  message: 'This should do as well'
})

Other methods such as stream is not written yet. You can only do query function with limited possibility.

TODO

  • Streaming logs (stream function)
  • Querying logs (query function) - ⚠ Need tests
  • Close connection (close function)

To get the logs values, please use knex for now: (there are more way of doing this, mine is just an example)

const knex = require('knex')({ ...config })

async function getLogsValue() {
  try {
    const trxProvider = knex.transactionProvider();
    const trx = await trxProvider();
    const logs = await trx('tableName').select();
    return logs;
  } catch (error) {
    throw new Error(error)
  }
}

Contributing

I will put this simply: Go ahead 😀

License

MIT