DebugCallGraphTrait.php in Varnish purger 8.2
File
src/DebugCallGraphTrait.php
View source
<?php
namespace Drupal\varnish_purger;
use Drupal\purge\Logger\PurgeLoggerAwareTrait;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
trait DebugCallGraphTrait {
use PurgeLoggerAwareTrait;
protected $debug = [];
protected function getClassName($class) {
if (is_object($class)) {
$class = get_class($class);
}
if ($pos = strrpos($class, '\\')) {
$class = substr($class, $pos + 1);
}
return $class;
}
protected function debug($caller) {
$logger = $this
->logger();
if (!$logger || !$logger
->isDebuggingEnabled()) {
return;
}
$caller = str_replace($this
->getClassName(__CLASS__), '', $this
->getClassName($caller));
$log = function ($output) use ($logger) {
$space = str_repeat(' ', count($this->debug));
$logger
->debug($space . $output);
};
if (!in_array($caller, $this->debug)) {
$this->debug[] = $caller;
$log("--> {$caller}():");
}
else {
unset($this->debug[array_search($caller, $this->debug)]);
$log(" (finished)");
}
}
protected function debugInfoForRequest(RequestInterface $r) {
$info = [];
$info['req http'] = $r
->getProtocolVersion();
$info['req uri'] = $r
->getUri()
->__toString();
$info['req method'] = $r
->getMethod();
$info['req headers'] = [];
foreach ($r
->getHeaders() as $h => $v) {
$info['req headers'][] = $h . ': ' . $r
->getHeaderLine($h);
}
return $info;
}
protected function debugInfoForResponse(ResponseInterface $r, RequestException $e = NULL) {
$info = [];
$info['rsp http'] = $r
->getProtocolVersion();
$info['rsp status'] = $r
->getStatusCode();
$info['rsp reason'] = $r
->getReasonPhrase();
if (!is_null($e)) {
$info['rsp summary'] = json_encode($e
->getResponseBodySummary($r));
}
$info['rsp headers'] = [];
foreach ($r
->getHeaders() as $h => $v) {
$info['rsp headers'][] = $h . ': ' . $r
->getHeaderLine($h);
}
return $info;
}
protected function logDebugTable(array $table, $left = 15) {
$longest_key = max(array_map('strlen', array_keys($table)));
$logger = $this
->logger();
if ($longest_key > $left) {
$left = $longest_key;
}
foreach ($table as $title => $value) {
$spacing = str_repeat(' ', $left - strlen($title));
$title = strtoupper($title) . $spacing . ' | ';
if (is_array($value)) {
foreach ($value as $repeated_value) {
$logger
->debug($title . $repeated_value);
}
}
else {
$logger
->debug($title . $value);
}
}
}
protected function logFailedRequest($caller, \Exception $e) {
$msg = "::@caller() -> @class:";
$vars = [
'@caller' => $caller,
'@class' => $this
->getClassName($e),
'@msg' => $e
->getMessage(),
];
if ($e instanceof ConnectException) {
$vars['@msg'] = str_replace('(see http://curl.haxx.se/libcurl/c/libcurl-errors.html)', '', $e
->getMessage());
$vars['@msg'] .= '; This is allowed to happen accidentally when load' . ' balancers are slow. However, if all cache invalidations fail, your' . ' queue may stall and you should investigate with your hosting' . ' provider!';
}
elseif ($e instanceof RequestException) {
$req = $e
->getRequest();
$msg .= " HTTP @status; @method @uri;";
$vars['@uri'] = $req
->getUri();
$vars['@method'] = $req
->getMethod();
$vars['@status'] = $e
->hasResponse() ? $e
->getResponse()
->getStatusCode() : '???';
}
$logger = $this
->logger();
$logger
->emergency("{$msg} @msg", $vars);
if ($logger
->isDebuggingEnabled()) {
$table = [
'exception' => get_class($e),
];
if ($e instanceof RequestException) {
$table = array_merge($table, $this
->debugInfoForRequest($e
->getRequest()));
$table['rsp'] = ($has_rsp = $e
->hasResponse()) ? 'YES' : 'No response';
if ($has_rsp && ($rsp = $e
->getResponse())) {
$table = array_merge($table, $this
->debugInfoForResponse($rsp, $e));
}
}
$this
->logDebugTable($table);
}
}
}