winston-koa-sse
v1.0.4
Published
winston koa transport based on sse
Downloads
9
Readme
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
- For obvious security concerns, do not activate it in production!
- This is a first basic not opimized implementation, no batching of log messages, etc