Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzle/guzzle/src/Guzzle/Plugin/Log
File: LogPlugin.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Guzzle\Plugin\Log;
[2] Fix | Delete
[3] Fix | Delete
use Guzzle\Common\Event;
[4] Fix | Delete
use Guzzle\Log\LogAdapterInterface;
[5] Fix | Delete
use Guzzle\Log\MessageFormatter;
[6] Fix | Delete
use Guzzle\Log\ClosureLogAdapter;
[7] Fix | Delete
use Guzzle\Http\EntityBody;
[8] Fix | Delete
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
[9] Fix | Delete
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Plugin class that will add request and response logging to an HTTP request.
[13] Fix | Delete
*
[14] Fix | Delete
* The log plugin uses a message formatter that allows custom messages via template variable substitution.
[15] Fix | Delete
*
[16] Fix | Delete
* @see MessageLogger for a list of available log template variable substitutions
[17] Fix | Delete
*/
[18] Fix | Delete
class LogPlugin implements EventSubscriberInterface
[19] Fix | Delete
{
[20] Fix | Delete
/** @var LogAdapterInterface Adapter responsible for writing log data */
[21] Fix | Delete
protected $logAdapter;
[22] Fix | Delete
[23] Fix | Delete
/** @var MessageFormatter Formatter used to format messages before logging */
[24] Fix | Delete
protected $formatter;
[25] Fix | Delete
[26] Fix | Delete
/** @var bool Whether or not to wire request and response bodies */
[27] Fix | Delete
protected $wireBodies;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* @param LogAdapterInterface $logAdapter Adapter object used to log message
[31] Fix | Delete
* @param string|MessageFormatter $formatter Formatter used to format log messages or the formatter template
[32] Fix | Delete
* @param bool $wireBodies Set to true to track request and response bodies using a temporary
[33] Fix | Delete
* buffer if the bodies are not repeatable.
[34] Fix | Delete
*/
[35] Fix | Delete
public function __construct(
[36] Fix | Delete
LogAdapterInterface $logAdapter,
[37] Fix | Delete
$formatter = null,
[38] Fix | Delete
$wireBodies = false
[39] Fix | Delete
) {
[40] Fix | Delete
$this->logAdapter = $logAdapter;
[41] Fix | Delete
$this->formatter = $formatter instanceof MessageFormatter ? $formatter : new MessageFormatter($formatter);
[42] Fix | Delete
$this->wireBodies = $wireBodies;
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Get a log plugin that outputs full request, response, and curl error information to stderr
[47] Fix | Delete
*
[48] Fix | Delete
* @param bool $wireBodies Set to false to disable request/response body output when they use are not repeatable
[49] Fix | Delete
* @param resource $stream Stream to write to when logging. Defaults to STDERR when it is available
[50] Fix | Delete
*
[51] Fix | Delete
* @return self
[52] Fix | Delete
*/
[53] Fix | Delete
public static function getDebugPlugin($wireBodies = true, $stream = null)
[54] Fix | Delete
{
[55] Fix | Delete
if ($stream === null) {
[56] Fix | Delete
if (defined('STDERR')) {
[57] Fix | Delete
$stream = STDERR;
[58] Fix | Delete
} else {
[59] Fix | Delete
$stream = fopen('php://output', 'w');
[60] Fix | Delete
}
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
return new self(new ClosureLogAdapter(function ($m) use ($stream) {
[64] Fix | Delete
fwrite($stream, $m . PHP_EOL);
[65] Fix | Delete
}), "# Request:\n{request}\n\n# Response:\n{response}\n\n# Errors: {curl_code} {curl_error}", $wireBodies);
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
public static function getSubscribedEvents()
[69] Fix | Delete
{
[70] Fix | Delete
return array(
[71] Fix | Delete
'curl.callback.write' => array('onCurlWrite', 255),
[72] Fix | Delete
'curl.callback.read' => array('onCurlRead', 255),
[73] Fix | Delete
'request.before_send' => array('onRequestBeforeSend', 255),
[74] Fix | Delete
'request.sent' => array('onRequestSent', 255)
[75] Fix | Delete
);
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Event triggered when curl data is read from a request
[80] Fix | Delete
*
[81] Fix | Delete
* @param Event $event
[82] Fix | Delete
*/
[83] Fix | Delete
public function onCurlRead(Event $event)
[84] Fix | Delete
{
[85] Fix | Delete
// Stream the request body to the log if the body is not repeatable
[86] Fix | Delete
if ($wire = $event['request']->getParams()->get('request_wire')) {
[87] Fix | Delete
$wire->write($event['read']);
[88] Fix | Delete
}
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Event triggered when curl data is written to a response
[93] Fix | Delete
*
[94] Fix | Delete
* @param Event $event
[95] Fix | Delete
*/
[96] Fix | Delete
public function onCurlWrite(Event $event)
[97] Fix | Delete
{
[98] Fix | Delete
// Stream the response body to the log if the body is not repeatable
[99] Fix | Delete
if ($wire = $event['request']->getParams()->get('response_wire')) {
[100] Fix | Delete
$wire->write($event['write']);
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Called before a request is sent
[106] Fix | Delete
*
[107] Fix | Delete
* @param Event $event
[108] Fix | Delete
*/
[109] Fix | Delete
public function onRequestBeforeSend(Event $event)
[110] Fix | Delete
{
[111] Fix | Delete
if ($this->wireBodies) {
[112] Fix | Delete
$request = $event['request'];
[113] Fix | Delete
// Ensure that curl IO events are emitted
[114] Fix | Delete
$request->getCurlOptions()->set('emit_io', true);
[115] Fix | Delete
// We need to make special handling for content wiring and non-repeatable streams.
[116] Fix | Delete
if ($request instanceof EntityEnclosingRequestInterface && $request->getBody()
[117] Fix | Delete
&& (!$request->getBody()->isSeekable() || !$request->getBody()->isReadable())
[118] Fix | Delete
) {
[119] Fix | Delete
// The body of the request cannot be recalled so logging the body will require us to buffer it
[120] Fix | Delete
$request->getParams()->set('request_wire', EntityBody::factory());
[121] Fix | Delete
}
[122] Fix | Delete
if (!$request->getResponseBody()->isRepeatable()) {
[123] Fix | Delete
// The body of the response cannot be recalled so logging the body will require us to buffer it
[124] Fix | Delete
$request->getParams()->set('response_wire', EntityBody::factory());
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
/**
[130] Fix | Delete
* Triggers the actual log write when a request completes
[131] Fix | Delete
*
[132] Fix | Delete
* @param Event $event
[133] Fix | Delete
*/
[134] Fix | Delete
public function onRequestSent(Event $event)
[135] Fix | Delete
{
[136] Fix | Delete
$request = $event['request'];
[137] Fix | Delete
$response = $event['response'];
[138] Fix | Delete
$handle = $event['handle'];
[139] Fix | Delete
[140] Fix | Delete
if ($wire = $request->getParams()->get('request_wire')) {
[141] Fix | Delete
$request = clone $request;
[142] Fix | Delete
$request->setBody($wire);
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
if ($wire = $request->getParams()->get('response_wire')) {
[146] Fix | Delete
$response = clone $response;
[147] Fix | Delete
$response->setBody($wire);
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
// Send the log message to the adapter, adding a category and host
[151] Fix | Delete
$priority = $response && $response->isError() ? LOG_ERR : LOG_DEBUG;
[152] Fix | Delete
$message = $this->formatter->format($request, $response, $handle);
[153] Fix | Delete
$this->logAdapter->log($message, $priority, array(
[154] Fix | Delete
'request' => $request,
[155] Fix | Delete
'response' => $response,
[156] Fix | Delete
'handle' => $handle
[157] Fix | Delete
));
[158] Fix | Delete
}
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function