npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

winston-express-sse

v1.0.0

Published

>

Downloads

5

Readme

winston-express-sse

Installation

$ npm install --save winston-express-sse

Usage

On the server-side

This module provides a Winston's Transport class that you must create and add to your winston configuration:

var winston = require('winston');
var ExpressSSETransportClass = require('winston-express-sse');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new ExpressSSETransportClass({level: 'error', timestamp: function(){return (new Date()).toISOString();})
  ]
});

The module also provides the Express's middleware as a static function called 'middleware'. Add these line in your express configuration code:

var winstonSse = require('winston-express-sse').middleware;

// Add the middleware function to the chain 
app.use(winstonSse);

// Be aware not to call the middleware. This module doesn't work in this way. Don't do this:
// /* wrong */ app.use(winstonSse());

The middleware adds a new method called 'enableLogEventStream' to the request object. So you can enable any route to receive SSE events from winston logs:

var router = express.Router();

/* SSE: Log */
router.get('/logstream', function (req, res, next) {
  res.enableLogEventStream();
});

On the browser-side

On the browser-side you need to build an EventSource pointing to the route on the server where you set up the SSE Stream:

  // Close previous connections
  if (logEventSource) {
    logEventSource.close();
  }
      
  // Connect to SSE with minimumLevel
  logEventSource = new EventSource('http://localhost:4000/logstream');

And subscribe for each log level event you want to receive:

  logEventSource.addEventListener('error', logEventHandler, false);
  logEventSource.addEventListener('warning', logEventHandler, false);
  logEventSource.addEventListener('info', logEventHandler, false);
  logEventSource.addEventListener('trace', logEventHandler, false);
  
  logEventHandler = function(e) {
    // e parameter contains the log level event in the property 'type' and the contents in the property 'data'    
    var message = (e.type || 'Unknown type') + ' - ' + (e.data || 'Unknown log');
    
    console.log(message);
  }

Transport configuration

The transport constructor takes these options:

  • level: Minimum level of messages that this transport will log (default 'info').
  • timestamp: Boolean flag indicating if we should prepend output with timestamps (default false). If function is specified, its return value will be used instead of timestamps.

Remarks

  • You can add your authentication logic in your Express route before calling to 'res.enableLogStream()';

  • The server will emit only SSE events with the minimum level specified in the 'level' property of the transport options

  • The client must subscribe to specific levels adding listeners to event names matching the winston level name. logEventSource.addEventListener('error', ...);

  • This module was tested with winston v.2.x.x and it doesn't use the node.js streams.

License

MIT © Raul