winston-redis-stream

0.0.3 • Public • Published

winston-redis-stream

A capped Redis transport for winston.

About

This package is a clone of winston-redis by Charlie Robbins, modified to use the new Streams feature of Redis included from version 5. It uses ioRedis1 rather than node-redis as the underlying client library because that supports the new stream commands. Otherwise the API is very similar.

The basic log method works.

The query method returns entries from the log selected by time:

const options = {
  from: new Date() - (24 * 60 * 60 * 1000),  // last 24 hrs
  until: new Date(),
  limit: 20,                                 // but only most recent 20 entries
  order: 'asc',                              // order earliest to most recent
  fields: ['level', 'message', 'timestamp']  // fields required
};

The "level" and "message" fields are always present. "timestamp" is a meta field and other meta fields which exist can also be selected.

logger.query(options, function (err, results) {
  if (err) {
    /* TODO: handle me */
    throw err;
  }
  results.redis.map(log => {
    console.log(`${log.timestamp} ${log.level}: ${log.message}` )
  })
});

The stream method has not been tried or tested and may well not work.

An example script "logreader.js" can be used to tail the log stream.

Requirements

  • NodeJS 8.11.x
  • Winston 3.x
  • Redis server version > 5.0.0

Usage

  const winston = require('winston');
  const redisTransport = require('winston-redis-stream');
 
  const logger = winston.createLogger({
    level: 'info',
    transports: [
      new redisTransport()
    ]
  });
 
  logger.log({
    level: "info",
    message: "redis is awesome",
    reason: "it's fast" // this will get stored as meta data
  });

This transport accepts the options accepted by the ioredis client:

  • host: (Default localhost) Remote host of the Redis server
  • port: (Default 6379) Port the Redis server is running on.
  • auth: (Default None) Password set on the Redis server

In addition to these, the Redis transport also accepts the following options.

  • redis: Either the redis client or the options for the redis client
  • length: (Default 200) Number of log messages to store.
  • container: (Default winston) Name of the Redis container you wish your logs to be in.
  • channel: (Default None) Name of the Redis channel to stream logs from.
  • meta: (Default {}) Custom fields to add to each log.
  • flatMeta: (Default false) Store meta data at the top level of the object rather than within a child meta object.

Metadata: Logged as JSON literal in Redis

Installation

Installing npm (node package manager)

  $ curl http://npmjs.org/install.sh | sh

Installing winston-redis

  $ npm install winston
  $ npm install winston-redis-stream

Run Tests

Winston-redis tests are written in mocha, using Abstract Winston Transport and designed to be run with npm.

  npm test

Author: Dick Middleton

 Originator: [Charlie Robbins](http://github.com/indexzero)

Package Sidebar

Install

npm i winston-redis-stream

Weekly Downloads

118

Version

0.0.3

License

MIT

Unpacked Size

16.3 kB

Total Files

8

Last publish

Collaborators

  • nottledim