node-middleware-log
v1.0.0
Published
Ndjson logging middleware
Downloads
2
Readme
node-middleware-log
Ndjson logging middleware.
Installation
$ npm install node-middleware-log
Usage
const logMiddleware = require('middleware-log')
const http = require('http')
const logOpts = { requestBody: true }
const log = httpLogger(logOpts, process.stdout)
http.createServer(function (req, res) {
log(req, res, function (err, res) {
// logic
})
}).listen()
express + bole
const logMiddleware = require('middleware-log')
const boleStream = require('bole-stream')
const express = require('express')
const bole = require('bole')
bole.output({ stream: process.stdout })
const app = express()
app.use(logMiddleware(boleStream({ level: 'info' })))
app.listen()
restify + bunyan
const logMiddleware = require('middleware-log')
const bunyanStream = require('bunyan-stream')
const restify = require('restify')
const bunyan = require('bunyan')
const logger = bunyan.createLogger({ name: 'myApp' })
const server = restify.createServer()
server.use(logMiddleware(bunyanStream({ level: 'info' }, logger)))
API
log = logMiddleware(opts, outStream)
Create logging middleware. Options can contain the following values:
- requestBody: log the requests body. Requires
req.body
to exist, which is often patched on by frameworks such asexpress
orrestify
.
log(req, res, cb(err, res))
Log a request and response, calls a callback after it's finished initializing.
Limitations
In order to be compatible with popular frameworks such as express
and
restify
, middleware-log
has had to make tradeoffs. To get properties such
as response size, Node's native properties are being patched on every request.
Also, to log the request size this package also relies on a non-standard
req.body
value to be present. If you'd like to roll your own middleware-log
with different tradeoffs take a look at size-stream
and http-ndjson
.