diff --git a/README.md b/README.md index 3c50a1cd70..cbd27f9f85 100644 --- a/README.md +++ b/README.md @@ -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 }); ``` @@ -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', }); @@ -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). diff --git a/lib/base/RequestClient.js b/lib/base/RequestClient.js index b8fd57f614..4355c17605 100644 --- a/lib/base/RequestClient.js +++ b/lib/base/RequestClient.js @@ -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 || {}; @@ -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, @@ -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; diff --git a/lib/rest/Twilio.d.ts b/lib/rest/Twilio.d.ts index c0c290743f..211a9034fa 100644 --- a/lib/rest/Twilio.d.ts +++ b/lib/rest/Twilio.d.ts @@ -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 @@ -134,6 +135,7 @@ declare namespace Twilio { allowRedirects?: boolean; data?: object; headers?: object; + logLevel?: string; method: string; params?: object; password?: string; @@ -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 { @@ -158,6 +161,7 @@ declare namespace Twilio { env?: object; httpClient?: RequestClient; lazyLoading?: boolean; + logLevel?: string; region?: string; } } diff --git a/lib/rest/Twilio.js b/lib/rest/Twilio.js index d19def1b72..4113219f4f 100644 --- a/lib/rest/Twilio.js +++ b/lib/rest/Twilio.js @@ -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 */ @@ -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'); @@ -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) { @@ -265,6 +268,7 @@ Twilio.prototype.request = function request(opts) { data: opts.data, timeout: opts.timeout, allowRedirects: opts.allowRedirects, + logLevel: opts.logLevel || this.logLevel }); };