1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00

Fixing common log format. Adding date_* format patterns. Closes #1122

This commit is contained in:
Michael Dowling 2015-06-13 17:00:31 -07:00
parent e812f17245
commit 861d2da555
2 changed files with 41 additions and 24 deletions

View File

@ -11,25 +11,27 @@ use Psr\Http\Message\ResponseInterface;
* *
* The following variable substitutions are supported: * The following variable substitutions are supported:
* *
* - {request}: Full HTTP request message * - {request}: Full HTTP request message
* - {response}: Full HTTP response message * - {response}: Full HTTP response message
* - {ts}: Timestamp * - {ts}: ISO 8601 date in GMT
* - {host}: Host of the request * - {date_iso_8601} ISO 8601 date in GMT
* - {method}: Method of the request * - {date_common_log} Apache common log date using the configured timezone.
* - {uri}: URI of the request * - {host}: Host of the request
* - {host}: Host of the request * - {method}: Method of the request
* - {version}: Protocol version * - {uri}: URI of the request
* - {target}: Request target of the request (path + query + fragment) * - {host}: Host of the request
* - {hostname}: Hostname of the machine that sent the request * - {version}: Protocol version
* - {code}: Status code of the response (if available) * - {target}: Request target of the request (path + query + fragment)
* - {phrase}: Reason phrase of the response (if available) * - {hostname}: Hostname of the machine that sent the request
* - {error}: Any error messages (if available) * - {code}: Status code of the response (if available)
* - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message * - {phrase}: Reason phrase of the response (if available)
* - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message * - {error}: Any error messages (if available)
* - {req_headers}: Request headers * - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message
* - {res_headers}: Response headers * - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message
* - {req_body}: Request body * - {req_headers}: Request headers
* - {res_body}: Response body * - {res_headers}: Response headers
* - {req_body}: Request body
* - {res_body}: Response body
*/ */
class MessageFormatter class MessageFormatter
{ {
@ -38,7 +40,7 @@ class MessageFormatter
* @link http://httpd.apache.org/docs/1.3/logs.html#common * @link http://httpd.apache.org/docs/1.3/logs.html#common
* @var string * @var string
*/ */
const CLF = "{hostname} {req_header_User-Agent} - [{ts}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}"; const CLF = "{hostname} {req_header_User-Agent} - [{date_common_log}] \"{method} {target} HTTP/{version}\" {code} {res_header_Content-Length}";
const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"; const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}";
const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}';
@ -108,8 +110,12 @@ class MessageFormatter
$result = $response ? $response->getBody() : 'NULL'; $result = $response ? $response->getBody() : 'NULL';
break; break;
case 'ts': case 'ts':
case 'date_iso_8601':
$result = gmdate('c'); $result = gmdate('c');
break; break;
case 'date_common_log':
$result = date('d/M/Y:H:i:s O');
break;
case 'method': case 'method':
$result = $request->getMethod(); $result = $request->getMethod();
break; break;

View File

@ -20,13 +20,24 @@ class MessageFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(MessageFormatter::CLF, $this->readAttribute($f, 'template')); $this->assertEquals(MessageFormatter::CLF, $this->readAttribute($f, 'template'));
} }
public function testFormatsTimestamps() public function dateProvider()
{ {
$f = new MessageFormatter('{ts}'); return [
['{ts}', '/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/'],
['{date_iso_8601}', '/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/'],
['{date_common_log}', '/^\d\d\/[A-Z][a-z]{2}\/[0-9]{4}:\d\d:\d\d:\d\d \-[\d]{4}/']
];
}
/**
* @dataProvider dateProvider
*/
public function testFormatsTimestamps($format, $pattern)
{
$f = new MessageFormatter($format);
$request = new Request('GET', '/'); $request = new Request('GET', '/');
$result = $f->format($request); $result = $f->format($request);
// Ensure it matches this format: '2014-03-02T00:18:41+00:00'; $this->assertEquals(1, preg_match($pattern, $result));
$this->assertEquals(1, preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/', $result));
} }
public function formatProvider() public function formatProvider()