GithubHelp home page GithubHelp logo

alexvictoor / winston-koa-sse Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 9 KB

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

HTML 15.96% JavaScript 84.04%

winston-koa-sse's Introduction

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

winston-koa-sse's People

Contributors

alexvictoor avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ooga

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.