GuzzleHttpMessageFormatter

Formats log messages using variable substitutions for requests, responses, and other transactional data.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/MessageFormatter.php  
  1. class MessageFormatter 
  2. /** 
  3. * Apache Common Log Format. 
  4. * @link http://httpd.apache.org/docs/2.4/logs.html#common 
  5. * @var string 
  6. */ 
  7. const CLF = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}"; 
  8. const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"; 
  9. const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; 
  10.  
  11. /** @var string Template used to format log messages */ 
  12. private $template; 
  13.  
  14. /** 
  15. * @param string $template Log message template 
  16. */ 
  17. public function __construct($template = self::CLF) 
  18. $this->template = $template ?: self::CLF; 
  19.  
  20. /** 
  21. * Returns a formatted message string. 
  22. * @param RequestInterface $request Request that was sent 
  23. * @param ResponseInterface $response Response that was received 
  24. * @param \Exception $error Exception that was received 
  25. * @return string 
  26. */ 
  27. public function format( 
  28. RequestInterface $request,  
  29. ResponseInterface $response = null,  
  30. \Exception $error = null 
  31. ) { 
  32. $cache = []; 
  33.  
  34. return preg_replace_callback( 
  35. '/{\s*([A-Za-z_\-\.0-9]+)\s*}/',  
  36. function (array $matches) use ($request, $response, $error, &$cache) { 
  37.  
  38. if (isset($cache[$matches[1]])) { 
  39. return $cache[$matches[1]]; 
  40.  
  41. $result = ''; 
  42. switch ($matches[1]) { 
  43. case 'request': 
  44. $result = Psr7\str($request); 
  45. break; 
  46. case 'response': 
  47. $result = $response ? Psr7\str($response) : ''; 
  48. break; 
  49. case 'req_headers': 
  50. $result = trim($request->getMethod() 
  51. . ' ' . $request->getRequestTarget()) 
  52. . ' HTTP/' . $request->getProtocolVersion() . "\r\n" 
  53. . $this->headers($request); 
  54. break; 
  55. case 'res_headers': 
  56. $result = $response ? 
  57. sprintf( 
  58. 'HTTP/%s %d %s',  
  59. $response->getProtocolVersion(),  
  60. $response->getStatusCode(),  
  61. $response->getReasonPhrase() 
  62. ) . "\r\n" . $this->headers($response) 
  63. : 'NULL'; 
  64. break; 
  65. case 'req_body': 
  66. $result = $request->getBody(); 
  67. break; 
  68. case 'res_body': 
  69. $result = $response ? $response->getBody() : 'NULL'; 
  70. break; 
  71. case 'ts': 
  72. case 'date_iso_8601': 
  73. $result = gmdate('c'); 
  74. break; 
  75. case 'date_common_log': 
  76. $result = date('d/M/Y:H:i:s O'); 
  77. break; 
  78. case 'method': 
  79. $result = $request->getMethod(); 
  80. break; 
  81. case 'version': 
  82. $result = $request->getProtocolVersion(); 
  83. break; 
  84. case 'uri': 
  85. case 'url': 
  86. $result = $request->getUri(); 
  87. break; 
  88. case 'target': 
  89. $result = $request->getRequestTarget(); 
  90. break; 
  91. case 'req_version': 
  92. $result = $request->getProtocolVersion(); 
  93. break; 
  94. case 'res_version': 
  95. $result = $response 
  96. ? $response->getProtocolVersion() 
  97. : 'NULL'; 
  98. break; 
  99. case 'host': 
  100. $result = $request->getHeaderLine('Host'); 
  101. break; 
  102. case 'hostname': 
  103. $result = gethostname(); 
  104. break; 
  105. case 'code': 
  106. $result = $response ? $response->getStatusCode() : 'NULL'; 
  107. break; 
  108. case 'phrase': 
  109. $result = $response ? $response->getReasonPhrase() : 'NULL'; 
  110. break; 
  111. case 'error': 
  112. $result = $error ? $error->getMessage() : 'NULL'; 
  113. break; 
  114. default: 
  115. // handle prefixed dynamic headers 
  116. if (strpos($matches[1], 'req_header_') === 0) { 
  117. $result = $request->getHeaderLine(substr($matches[1], 11)); 
  118. } elseif (strpos($matches[1], 'res_header_') === 0) { 
  119. $result = $response 
  120. ? $response->getHeaderLine(substr($matches[1], 11)) 
  121. : 'NULL'; 
  122.  
  123. $cache[$matches[1]] = $result; 
  124. return $result; 
  125. },  
  126. $this->template 
  127. ); 
  128.  
  129. private function headers(MessageInterface $message) 
  130. $result = ''; 
  131. foreach ($message->getHeaders() as $name => $values) { 
  132. $result .= $name . ': ' . implode(', ', $values) . "\r\n"; 
  133.  
  134. return trim($result);