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