2.0.0-alpha2 • Published 5 years ago

winstonjs-transport-http-headers v2.0.0-alpha2

Weekly downloads
1,246
License
MIT
Repository
github
Last release
5 years ago

WinstonJS Transport: HTTP Headers

A WinstonJS transport that logs to HTTP headers, useful for sending profiling data to the browser that doesn't muddle the response body.

v2 Supports Node 8, 10 and Winston v3

Master Build Status: Build Status Coverage Status

This project sponsored by:

AerisWeather - Empowering the next generation, aerisweather.com

Installation

This project is available on npm:

npm install --save winstonjs-transport-http-headers

Example

Note: A full example can be found in example.js which integrates an ExpressJS HTTP Server

The logger should be added to the response, probably in middleware:

// Middleware example to setup our logger.
app.use(function(req, res, next) {
    res.logger = new (winston.Logger)({
        transports: [
            // Here is our new logger
            new HttpHeaderTransport({
                setHeader: res.set.bind(res),
                level: 'debug'
            }),
            // We can use the HTTP Header logger in combination with other loggers too.
            new (winston.transports.Console)({
                level: 'warn'
            })
        ],
        //Setup Levels for this logger
        levels:     {
            warn: 4,
            info: 6,
            debug: 7
        }
    });
    next();
});

Then in an action, we can log something to the response:

// Will log to a header: `x-logger-0-debug`
res.logger.log('debug', 'Basic Log Message');

// Will log to both a header and the console (as defined above)
res.logger.log('warn', 'Uh oh');

Docs

new HttpHeaders(options)

ParamTypeDefaultDescription
setHeaderfunctionA callable (key, value) that will set the header to the request.
silentbooleanfalseIf enabled, logger will not log to HTTP Header, will not emit events.
levellevel"debug"What level this logger should respond to, see Winston docs for more info.
headerPrefixstring"X-Logger-"A callable that provides a string that is prepended to each header.
getHeaderIdfunction(default)A callable that gets a header ID based on passed options.
cleanIdfunction(default)Cleans a string into a nice HTTP friendly header ID.