Skip to content

alexvictoor/winston-koa-sse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

winston-koa-sse

A winston transport for KOA applications that streams logs to your browser leveraging SSE

Installation

  $ npm install winston-koa-sse

  or

  $ yarn add winston-koa-sse

Usage

On the server-side:

  import winston from 'winston';
  import Koa from 'koa';
  import KoaRouter from 'koa-router';
  import { KoaSseMiddleware, KoaSseTransport } from 'winston-koa-sse';

  // Adds the transport to winston
  winston.add(KoaSseTransport);

  // Plug logs stream on route /logs
  const router = new KoaRouter();
  router.get('/logs', KoaSseMiddleware);

On the browser-side you need to consume logs using an EventSource object. Below a script that you can include in your page, assuming your node server is reachable at URl http://localhost:3000 :

    const streamUrl = 'http://localhost:3000';
    const source = new EventSource(streamUrl); 
    
    const levels = ['debug', 'info', 'warn', 'error', 'fatal'];
    levels.forEach(level => {
        source.addEventListener(level, function(event) {
            console[level](event.data);
        });
    });

You can also use a bookmarklet to execute this script. Below the same script in a bookmarklet flavor:

javascript: (function(){const streamUrl = 'http://localhost:3000'; const source = new EventSource(streamUrl); const levels = ['debug', 'info', 'warn', 'error', 'fatal']; levels.forEach(level => { source.addEventListener(level, function(event) { console[level](event.data); }); });})();

Disclaimer

  1. For obvious security concerns, do not activate it in production!
  2. This is a first basic not opimized implementation, no batching of log messages, etc

About

Winston transport leveraging on Server Sent Events (SSE) to stream NodeJS logs to your browser console

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors