Python v4 SDKs now log HTTP requests and responses through Python's standard logging module. Nothing is printed until you turn the SDK's logger on, the same way you would for any library, or set one environment variable.
Details
The SDK writes under a logger named after its package and configures nothing itself: no handler, no level, and never the root logger. At INFO it writes one line as each request is sent and one when its response arrives, with the status and duration. WARNING covers responses outside 2xx and each retry, and ERROR a call that ends in an exception. For a run you cannot edit, an environment variable named after the package, such as PAYMENTS_LOG=info or debug, sets the level and adds a stderr handler when none is listening.
The logging_options keyword on the client, a dict or a LoggingOptions, turns on the DEBUG dumps of the request headers, the request body and the response headers. Header, query and form values print REDACTED unless they are known to be safe, so Authorization, cookies and API keys never reach the log, and a response body is never logged.
The per-call request_options accept a log_level floor that quietens one call. It never turns on a record the client's options leave off.
Usage Example
Raise the SDK's logger, choose the dumps on the client, and quieten a single call:
import logging
from payments import PaymentsClient
logging.basicConfig()
logging.getLogger("payments").setLevel(logging.DEBUG)
client = PaymentsClient(
bearer_auth="YOUR_ACCESS_TOKEN",
logging_options={"log_request_headers": True, "log_response_headers": True},
)
# The request and response lines, then both header dumps with Authorization masked.
client.payments.get_payment("pay_123")
# Nothing below WARNING is written for this call.
client.payments.get_payment("pay_456", request_options={"log_level": logging.WARNING})
log_request_body prints a JSON body as sent, so turn it on only where that content may be logged.
This feature is available in the Python v4 SDK. Generate it with version 2.x of the APIMatic CLI:
apimatic sdk generate --language=python
For the full set of flags and options, see the APIMatic CLI documentation.