├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | # after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # log-http [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Log HTTP requests. Works well with [pino][pino] and [pino-colada][pino-colada]. 6 | 7 | ## Usage 8 | ```js 9 | var logHttp = require('log-http') 10 | var http = require('http') 11 | var pino = require('pino') 12 | 13 | var log = pino('http') 14 | 15 | var server = http.createServer(function (req, res) { 16 | res.end('hello filthy planet') 17 | }) 18 | 19 | var stats = logHttp(server) 20 | stats.on('data', function (level, data) { 21 | log[level](data) 22 | }) 23 | 24 | server.listen(8080) 25 | ``` 26 | 27 | ## Output 28 | ```txt 29 | {"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003344167,"url":"/","method":"GET","message":"request","contentLength":89,"v":1} 30 | {"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003344170,"url":"/","method":"GET","statusCode":200,"message":"response","elapsed":4,"contentLength":119,"v":1} 31 | {"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003352734,"url":"/","method":"GET","message":"request","contentLength":89,"v":1} 32 | {"pid":42140,"hostname":"anons-MacBook.local","level":30,"time":1492003352734,"url":"/","method":"GET","statusCode":200,"message":"response","elapsed":1,"contentLength":119,"v":1} 33 | ``` 34 | 35 | ## API 36 | ### `stats = logHttp(server)` 37 | Create a new stats emitter from a server instance. 38 | 39 | ### `stats.on('data', function(level, data))` 40 | Listen to a new incoming or outgoing request. Logs out the expected log level 41 | and corresponding data. Should be passed to a logger. 42 | 43 | ## License 44 | [MIT](https://tldrlegal.com/license/mit-license) 45 | 46 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 47 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 48 | [2]: https://img.shields.io/npm/v/log-http.svg?style=flat-square 49 | [3]: https://npmjs.org/package/log-http 50 | [4]: https://img.shields.io/travis/yoshuawuyts/log-http/master.svg?style=flat-square 51 | [5]: https://travis-ci.org/yoshuawuyts/log-http 52 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/log-http/master.svg?style=flat-square 53 | [7]: https://codecov.io/github/yoshuawuyts/log-http 54 | [8]: http://img.shields.io/npm/dm/log-http.svg?style=flat-square 55 | [9]: https://npmjs.org/package/log-http 56 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 57 | [11]: https://github.com/feross/standard 58 | [pino-colada]: https://github.com/lrlna/pino-colada 59 | [pino]: https://github.com/pinojs/pino 60 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var logHttp = require('./') 2 | var http = require('http') 3 | var pino = require('pino') 4 | 5 | var log = pino('http') 6 | 7 | var server = http.createServer(function (req, res) { 8 | res.end('hello filthy planet') 9 | }) 10 | 11 | var stats = logHttp(server) 12 | stats.on('data', function (level, data) { 13 | log[level](data) 14 | }) 15 | 16 | server.listen(8080) 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var EventEmitter = require('events').EventEmitter 2 | var requestStats = require('request-stats') 3 | 4 | module.exports = logHttp 5 | 6 | function logHttp (server) { 7 | var emitter = new EventEmitter() 8 | var stats = requestStats(server) 9 | stats.on('complete', fmtStats) 10 | return emitter 11 | 12 | function fmtStats (stats) { 13 | var req = stats.req 14 | var res = stats.res 15 | 16 | var resLevel = res.status >= 400 ? 'warn' : 'info' 17 | 18 | emitter.emit('data', 'info', { 19 | url: req.path, 20 | method: req.method, 21 | message: 'request', 22 | contentLength: req.bytes 23 | }) 24 | 25 | emitter.emit('data', resLevel, { 26 | url: req.path, 27 | method: req.method, 28 | statusCode: res.status, 29 | message: 'response', 30 | elapsed: stats.time, 31 | contentLength: res.bytes 32 | }) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "log-http", 3 | "description": "Log HTTP requests", 4 | "repository": "yoshuawuyts/log-http", 5 | "version": "1.0.0", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node ./example.js", 9 | "test": "standard && npm run deps", 10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov" 11 | }, 12 | "dependencies": { 13 | "request-stats": "^2.0.1" 14 | }, 15 | "devDependencies": { 16 | "dependency-check": "^2.8.0", 17 | "nyc": "^10.2.0", 18 | "pino": "^4.2.4", 19 | "standard": "^10.0.1", 20 | "tape": "^4.6.3" 21 | }, 22 | "keywords": [ 23 | "log", 24 | "http", 25 | "server" 26 | ], 27 | "license": "MIT" 28 | } 29 | --------------------------------------------------------------------------------