Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add http logging for Node JS #618

Merged
merged 27 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3e1945e
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
5cb3410
Add helper function to match CLI output
JenniferMah Oct 13, 2020
4025e46
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
5fb1088
Add helper function to match CLI output
JenniferMah Oct 13, 2020
4d0d88d
Merge branch 'DI947' of github.com:twilio/twilio-node into DI947
JenniferMah Oct 14, 2020
8c67b21
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
a6f3d07
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
e515020
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
f12dbe6
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
69bca0c
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
8372078
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
0c9252f
Merge branch 'DI947' of github.com:twilio/twilio-node into DI947
JenniferMah Oct 15, 2020
a41ec86
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
a601238
Add helper function to match CLI output
JenniferMah Oct 13, 2020
6fe65cc
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
fd835e0
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
c224fed
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
5b3e16e
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
5a1ae55
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
317fc7a
feat: add http logging for Node JS
JenniferMah Oct 13, 2020
e1fc887
Use logLevel to trigger debug logging and log the request and response
JenniferMah Oct 14, 2020
937ee70
Merge branch 'DI947' of github.com:twilio/twilio-node into DI947
JenniferMah Oct 15, 2020
8d3b0f2
Merge branch 'main' into DI947
JenniferMah Oct 15, 2020
b497a50
Merge branch 'DI947' of github.com:twilio/twilio-node into DI947
JenniferMah Oct 15, 2020
6fb28df
Move request log to before request is being made and fix response.dat…
JenniferMah Oct 15, 2020
7583c9d
allow logLevel to be a request-level parameter
JenniferMah Oct 16, 2020
276c4be
opts.logLevel takes precedence
JenniferMah Oct 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
JenniferMah marked this conversation as resolved.
Show resolved Hide resolved
*/
/* 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