TypeScript v4 SDKs now log HTTP requests, responses and retries. Logging is off by default and is switched on through the logging client option, or from the environment without a code change.
Details
The logger writes the request line, the response line with its status and duration, a line before each retry, and a line for a call that ends without a response. Request headers, response headers and the request body are off by default and switched on with logRequestHeaders, logResponseHeaders and logRequestBody. Lines go to console unless you pass your own logger, which can be any object with error, warn, info and debug methods.
Query values and headers are redacted by allow-list, so credentials, cookies and API keys are masked. redactedKeys, redactedHeaders and unmaskHeaders adjust what is masked. A JSON request body is logged as it is, so leave logRequestBody off on endpoints that carry personal or payment data.
When the code sets neither a logger nor a level, the client reads an environment variable named after the client class in upper snake case, such as PAYMENTS_CLIENT_LOG for PaymentsClient. Set it to error, warn, info or debug to turn logging on. A call's second argument can set the level for that call alone.
Usage Example
Send the SDK's lines to your application's logger, and turn on full detail for one call:
import { PaymentsClient } from "payments";
const client = new PaymentsClient({
logging: {
logger: appLogger, // any object with error, warn, info and debug methods
level: "info",
logRequestHeaders: true,
logResponseHeaders: true,
},
});
// This call alone logs at debug, request body included.
await client.payments.get({ paymentId: "pay_123" }, { logging: { level: "debug" } });
To switch logging on in a deployed application without a code change, run it with PAYMENTS_CLIENT_LOG=info.
This feature is available in the TypeScript v4 SDK. Generate it with version 2.x of the APIMatic CLI:
apimatic sdk generate --language=typescript
For the full set of flags and options, see the APIMatic CLI documentation.