docs: Add a DEBUG.md
bshaffer opened this issue · 0 comments
bshaffer commented
See https://github.com/googleapis/google-cloud-go/blob/main/debug.md
Similary, PHP could have one. Here's an example of how to add debugging to bigquery
use Google\Cloud\BigQuery\BigQueryClient;
use GuzzleHttp\HandlerStack;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Handler\StreamHandler;
$logger = new Logger('Logger');
$logger->pushHandler(new StreamHandler('php://stdout', Level::Debug)); // <<< uses a stream
$stack = HandlerStack::create();
$stack->push(
Middleware::log(
$logger,
new MessageFormatter('{req_body} - {res_body}')
)
);
$client = new \GuzzleHttp\Client([
'handler' => $stack,
]);
$httpHandler = HttpHandlerFactory::build($client);
$bigQuery = new BigQueryClient([
'httpHandler' => $httpHandler,
'projectId' => $projectId,
]);
$datasets = $bigQuery->datasets();
foreach ($datasets as $dataset) {
print($dataset->id() . PHP_EOL);
}
This would be similar for any GAPIC client as well, although you'd need to supply httpHandler
to the transport config, and it would only work for REST requests
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\ListInfoTypesRequest;
use GuzzleHttp\HandlerStack;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Handler\StreamHandler;
$logger = new Logger('Logger');
$logger->pushHandler(new StreamHandler('php://stdout', Level::Debug)); // <<< uses a stream
$stack = HandlerStack::create();
$stack->push(
Middleware::log(
$logger,
new MessageFormatter('{req_body} - {res_body}')
)
);
$client = new \GuzzleHttp\Client([
'handler' => $stack,
]);
$httpHandler = [HttpHandlerFactory::build($client), 'async'];
// Instantiate a client.
$dlp = new DlpServiceClient([
'transportConfig' => ['rest' => ['httpHandler' => $httpHandler]],
]);
// Run request
$listInfoTypesRequest = (new ListInfoTypesRequest())
->setLanguageCode($languageCode)
->setFilter($filter);
$response = $dlp->listInfoTypes($listInfoTypesRequest);
// Print the results
print('Info Types:' . PHP_EOL);
foreach ($response->getInfoTypes() as $infoType) {
printf(
' %s (%s)' . PHP_EOL,
$infoType->getDisplayName(),
$infoType->getName()
);
}