log-http
Version:
Log HTTP requests
60 lines (48 loc) • 2.43 kB
Markdown
[![npm version][2]][3] [![build status][4]][5]
[![downloads][8]][9] [![js-standard-style][10]][11]
Log HTTP requests. Works well with [pino][pino] and [pino-colada][pino-colada].
```js
var logHttp = require('log-http')
var http = require('http')
var pino = require('pino')
var log = pino('http')
var server = http.createServer(function (req, res) {
res.end('hello filthy planet')
})
var stats = logHttp(server)
stats.on('data', function (level, data) {
log[level](data)
})
server.listen(8080)
```
```txt
{"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003344167,"url":"/","method":"GET","message":"request","contentLength":89,"v":1}
{"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003344170,"url":"/","method":"GET","statusCode":200,"message":"response","elapsed":4,"contentLength":119,"v":1}
{"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003352734,"url":"/","method":"GET","message":"request","contentLength":89,"v":1}
{"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003352734,"url":"/","method":"GET","statusCode":200,"message":"response","elapsed":1,"contentLength":119,"v":1}
```
Create a new stats emitter from a server instance.
Listen to a new incoming or outgoing request. Logs out the expected log level
and corresponding data. Should be passed to a logger.
[](https://tldrlegal.com/license/mit-license)
[]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[]: https://nodejs.org/api/documentation.html#documentation_stability_index
[]: https://img.shields.io/npm/v/log-http.svg?style=flat-square
[]: https://npmjs.org/package/log-http
[]: https://img.shields.io/travis/yoshuawuyts/log-http/master.svg?style=flat-square
[]: https://travis-ci.org/yoshuawuyts/log-http
[]: https://img.shields.io/codecov/c/github/yoshuawuyts/log-http/master.svg?style=flat-square
[]: https://codecov.io/github/yoshuawuyts/log-http
[]: http://img.shields.io/npm/dm/log-http.svg?style=flat-square
[]: https://npmjs.org/package/log-http
[]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[]: https://github.com/feross/standard
[]: https://github.com/lrlna/pino-colada
[]: https://github.com/pinojs/pino