Skip to content

Commit

Permalink
Merge 0dc477a into b3a218d
Browse files Browse the repository at this point in the history
  • Loading branch information
yuric18 committed May 21, 2019
2 parents b3a218d + 0dc477a commit f7d4fdd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -865,6 +865,7 @@ The first argument can be either a `url` or an `options` object. The only requir
- `firstByte`: Duration of HTTP server response (`timings.response` - `timings.connect`)
- `download`: Duration of HTTP download (`timings.end` - `timings.response`)
- `total`: Duration entire HTTP round-trip (`timings.end`)
- `logger` - All requests made from the server will be logget at `info` level, that supports the standard loggin API (like `console` or [Winston](https://github.com/winstonjs/winston))

- `har` - a [HAR 1.2 Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), will be processed from HAR format into options overwriting matching values *(see the [HAR 1.2 section](#support-for-har-12) for details)*
- `callback` - alternatively pass the request's callback in the options object
Expand Down Expand Up @@ -898,9 +899,10 @@ instead, it **returns a wrapper** that has your default settings applied to it.

For example:
```js
//requests using baseRequest() will set the 'x-token' header
//requests using baseRequest() will set the 'x-token' header and log its requests on console
const baseRequest = request.defaults({
headers: {'x-token': 'my-token'}
headers: {'x-token': 'my-token'},
logger: console
})

//requests using specialRequest() will include the 'x-token' header set in
Expand Down
8 changes: 8 additions & 0 deletions request.js
Expand Up @@ -710,6 +710,10 @@ Request.prototype.start = function () {
// this is usually called on the first write(), end() or on nextTick()
var self = this

if (self.logger) {
self.timing = true
}

if (self.timing) {
// All timings will be relative to this request's startTime. In order to do this,
// we need to capture the wall-clock start time (via Date), immediately followed
Expand Down Expand Up @@ -916,6 +920,10 @@ Request.prototype.onRequestResponse = function (response) {
// timings is just for the final fetch
response.timings = self.timings

if (self.logger) {
self.logger.info(`----> ${response.request.method} ${response.request.href} - ${response.statusCode} - ${response.timings.response.toFixed(2)}ms`)
}

// pre-calculate phase timings as well
response.timingPhases = {
wait: self.timings.socket,
Expand Down
52 changes: 52 additions & 0 deletions tests/test-logger.js
@@ -0,0 +1,52 @@
var fs = require('fs')
var path = require('path')
var http = require('http')
var tape = require('tape')
var request = require('../')
var server

tape('before', function (t) {
server = http.createServer()
server.on('request', function (req, res) {
req.pipe(res)
})
server.listen(0, function () {
server.url = 'http://localhost:' + this.address().port
t.end()
})
})

tape('request with logger option', function (t) {
var fpath = path.join(__dirname, 'unicycle.jpg')
var input = fs.createReadStream(fpath, {highWaterMark: 1000})
request({
uri: server.url,
method: 'POST',
body: input,
encoding: null,
logger: console
}, function (err, res, body) {
t.error(err)
t.equal(body.length, fs.statSync(fpath).size)
t.end()
})
})

tape('request without logger option', function (t) {
var fpath = path.join(__dirname, 'unicycle.jpg')
var input = fs.createReadStream(fpath, {highWaterMark: 1000})
request({
uri: server.url,
method: 'POST',
body: input,
encoding: null
}, function (err, res, body) {
t.error(err)
t.equal(body.length, fs.statSync(fpath).size)
t.end()
})
})

tape('after', function (t) {
server.close(t.end)
})

0 comments on commit f7d4fdd

Please sign in to comment.