Skip to content

Commit

Permalink
feat: add http logging for Node JS (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
JenniferMah authored Oct 16, 2020
1 parent 8ada194 commit efa0a94
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ If your environment requires SSL decryption, you can set the path to CA bundle i
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console

const client = require('twilio')(accountSid, authToken, {
lazyLoading: true
const client = require('twilio')(accountSid, authToken, {
lazyLoading: true
});
```

Expand All @@ -59,7 +59,7 @@ To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/doc
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console

const client = require('twilio')(accountSid, authToken, {
const client = require('twilio')(accountSid, authToken, {
region: 'au1',
edge: 'sydney',
});
Expand All @@ -75,6 +75,22 @@ client.edge = 'sydney';

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.

### Enable Debug Logging
There are two ways to enable debug logging in the default HTTP client. You can create an environment variable called `TWILIO_LOG_LEVEL` and set it to `debug` or you can set the logLevel variable on the client as debug:
```javascript
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console

const client = require('twilio')(accountSid, authToken, {
logLevel: 'debug'
});
```
You can also set the logLevel variable on the client after constructing the Twilio client:
```javascript
const client = require('twilio')(accountSid, authToken);
client.logLevel = 'debug';
```

## Handling Exceptions

For an example on how to handle exceptions in this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/node/usage-guide#exceptions).
Expand Down
32 changes: 32 additions & 0 deletions lib/base/RequestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var RequestClient = function() {};
* @param {int} [opts.timeout=30000] - The request timeout in milliseconds
* @param {boolean} [opts.allowRedirects] - Should the client follow redirects
* @param {boolean} [opts.forever] - Set to true to use the forever-agent
* @param {string} [opts.logLevel] - Show debug logs
*/
RequestClient.prototype.request = function(opts) {
opts = opts || {};
Expand Down Expand Up @@ -89,11 +90,20 @@ RequestClient.prototype.request = function(opts) {
ca: options.ca
};

if (opts.logLevel === 'debug') {
logRequest(options)
}

var _this = this;
this.lastResponse = undefined;
this.lastRequest = new Request(optionsRequest);

axios(options).then((response) => {
if (opts.logLevel === 'debug') {
console.log(`response.statusCode: ${response.status}`)
console.log(`response.headers: ${JSON.stringify(response.headers)}`)
console.log(`response.data: ${JSON.stringify(response.data)}`)
}
_this.lastResponse = new Response(response.status, response.data, response.headers);
deferred.resolve({
statusCode: response.status,
Expand All @@ -107,4 +117,26 @@ RequestClient.prototype.request = function(opts) {
return deferred.promise;
};

function logRequest(options) {
console.log('-- BEGIN Twilio API Request --');
console.log(`${options.method} ${options.url}`);

if (options.data) {
console.log('Form data:');
console.log(options.data);
}

if (options.params) {
console.log('Querystring:');
console.log(options.params);
}

if (options.headers) {
console.log('Headers:');
console.log(options.headers)
}

console.log('-- END Twilio API Request --');
}

module.exports = RequestClient;
4 changes: 4 additions & 0 deletions lib/rest/Twilio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ declare namespace Twilio {
* @property allowRedirects - Should the client follow redirects
* @property data - The request data
* @property headers - The request headers
* @property logLevel - Show debug logs
* @property method - The http method
* @property params - The request params
* @property password - The password used for auth
Expand All @@ -134,6 +135,7 @@ declare namespace Twilio {
allowRedirects?: boolean;
data?: object;
headers?: object;
logLevel?: string;
method: string;
params?: object;
password?: string;
Expand All @@ -150,6 +152,7 @@ declare namespace Twilio {
* @property env - The environment object. Defaults to process.env
* @property httpClient - The client used for http requests. Defaults to RequestClient
* @property lazyLoading - Enable lazy loading, loading time will decrease if enabled
* @property logLevel - Debug logs will be shown. Defaults to none
* @property region - Twilio region to use. Defaults to us1 if edge defined
*/
export interface TwilioClientOptions {
Expand All @@ -158,6 +161,7 @@ declare namespace Twilio {
env?: object;
httpClient?: RequestClient;
lazyLoading?: boolean;
logLevel?: string;
region?: string;
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/rest/Twilio.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ var RestException = require('../base/RestException'); /* jshint ignore:line */
* Twilio region to use. Defaults to us1 if edge defined
* @param {boolean} [opts.lazyLoading] -
* Enable lazy loading, loading time will decrease if enabled
* @param {string} [opts.logLevel] - Debug logs will be shown. Defaults to none
*
* @returns {Twilio} A new instance of Twilio client
*/
Expand All @@ -125,6 +126,7 @@ function Twilio(username, password, opts) {
}
this.edge = opts.edge || env.TWILIO_EDGE;
this.region = opts.region || env.TWILIO_REGION;
this.logLevel = opts.logLevel || env.TWILIO_LOG_LEVEL;

if (!this.username) {
throw new Error('username is required');
Expand Down Expand Up @@ -220,6 +222,7 @@ function Twilio(username, password, opts) {
* @param {object} [opts.data] - The request data
* @param {int} [opts.timeout] - The request timeout in milliseconds
* @param {boolean} [opts.allowRedirects] - Should the client follow redirects
* @param {string} [opts.logLevel] - Show debug logs
*/
/* jshint ignore:end */
Twilio.prototype.request = function request(opts) {
Expand Down Expand Up @@ -265,6 +268,7 @@ Twilio.prototype.request = function request(opts) {
data: opts.data,
timeout: opts.timeout,
allowRedirects: opts.allowRedirects,
logLevel: opts.logLevel || this.logLevel
});
};

Expand Down

0 comments on commit efa0a94

Please sign in to comment.