log-plugin.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ==========
  2. Log plugin
  3. ==========
  4. Use the ``Guzzle\Plugin\Log\LogPlugin`` to view all data sent over the wire, including entity bodies and redirects.
  5. .. code-block:: php
  6. use Guzzle\Http\Client;
  7. use Guzzle\Log\Zf1LogAdapter;
  8. use Guzzle\Plugin\Log\LogPlugin;
  9. use Guzzle\Log\MessageFormatter;
  10. $client = new Client('http://www.test.com/');
  11. $adapter = new Zf1LogAdapter(
  12. new \Zend_Log(new \Zend_Log_Writer_Stream('php://output'))
  13. );
  14. $logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT);
  15. // Attach the plugin to the client, which will in turn be attached to all
  16. // requests generated by the client
  17. $client->addSubscriber($logPlugin);
  18. $response = $client->get('http://google.com')->send();
  19. The code sample above wraps a ``Zend_Log`` object using a ``Guzzle\Log\Zf1LogAdapter``. After attaching the plugin to
  20. the client, all data sent over the wire will be logged to stdout.
  21. The first argument of the LogPlugin's constructor accepts a ``Guzzle\Log\LogAdapterInterface`` object. This object is
  22. an adapter that allows you to use the logging capabilities of your favorite log implementation. The second argument of
  23. the constructor accepts a ``Guzzle\Log\MessageFormatter`` or a log messaged format string. The format string uses
  24. variable substitution and allows you to define the log data that is important to your application. The different
  25. variables that can be injected are as follows:
  26. ================== ====================================================================================
  27. Variable Substitution
  28. ================== ====================================================================================
  29. {request} Full HTTP request message
  30. {response} Full HTTP response message
  31. {ts} Timestamp
  32. {host} Host of the request
  33. {method} Method of the request
  34. {url} URL of the request
  35. {host} Host of the request
  36. {protocol} Request protocol
  37. {version} Protocol version
  38. {resource} Resource of the request (path + query + fragment)
  39. {port} Port of the request
  40. {hostname} Hostname of the machine that sent the request
  41. {code} Status code of the response (if available)
  42. {phrase} Reason phrase of the response (if available)
  43. {curl_error} Curl error message (if available)
  44. {curl_code} Curl error code (if available)
  45. {curl_stderr} Curl standard error (if available)
  46. {connect_time} Time in seconds it took to establish the connection (if available)
  47. {total_time} Total transaction time in seconds for last transfer (if available)
  48. {req_header_*} Replace `*` with the lowercased name of a request header to add to the message
  49. {res_header_*} Replace `*` with the lowercased name of a response header to add to the message
  50. {req_body} Request body
  51. {res_body} Response body
  52. ================== ====================================================================================
  53. The LogPlugin has a helper method that can be used when debugging that will output the full HTTP request and
  54. response of a transaction:
  55. .. code-block:: php
  56. $client->addSubscriber(LogPlugin::getDebugPlugin());