From 861d2da555ebe00a036235700291793d6d554b79 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Sat, 13 Jun 2015 17:00:31 -0700 Subject: [PATCH] Fixing common log format. Adding date_* format patterns. Closes #1122 --- src/MessageFormatter.php | 46 +++++++++++++++++++--------------- tests/MessageFormatterTest.php | 19 +++++++++++--- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/MessageFormatter.php b/src/MessageFormatter.php index 80876cb3..5f0cb6f7 100644 --- a/src/MessageFormatter.php +++ b/src/MessageFormatter.php @@ -11,25 +11,27 @@ use Psr\Http\Message\ResponseInterface; * * The following variable substitutions are supported: * - * - {request}: Full HTTP request message - * - {response}: Full HTTP response message - * - {ts}: Timestamp - * - {host}: Host of the request - * - {method}: Method of the request - * - {uri}: URI of the request - * - {host}: Host of the request - * - {version}: Protocol version - * - {target}: Request target of the request (path + query + fragment) - * - {hostname}: Hostname of the machine that sent the request - * - {code}: Status code of the response (if available) - * - {phrase}: Reason phrase of the response (if available) - * - {error}: Any error messages (if available) - * - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message - * - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message - * - {req_headers}: Request headers - * - {res_headers}: Response headers - * - {req_body}: Request body - * - {res_body}: Response body + * - {request}: Full HTTP request message + * - {response}: Full HTTP response message + * - {ts}: ISO 8601 date in GMT + * - {date_iso_8601} ISO 8601 date in GMT + * - {date_common_log} Apache common log date using the configured timezone. + * - {host}: Host of the request + * - {method}: Method of the request + * - {uri}: URI of the request + * - {host}: Host of the request + * - {version}: Protocol version + * - {target}: Request target of the request (path + query + fragment) + * - {hostname}: Hostname of the machine that sent the request + * - {code}: Status code of the response (if available) + * - {phrase}: Reason phrase of the response (if available) + * - {error}: Any error messages (if available) + * - {req_header_*}: Replace `*` with the lowercased name of a request header to add to the message + * - {res_header_*}: Replace `*` with the lowercased name of a response header to add to the message + * - {req_headers}: Request headers + * - {res_headers}: Response headers + * - {req_body}: Request body + * - {res_body}: Response body */ class MessageFormatter { @@ -38,7 +40,7 @@ class MessageFormatter * @link http://httpd.apache.org/docs/1.3/logs.html#common * @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 SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; @@ -108,8 +110,12 @@ class MessageFormatter $result = $response ? $response->getBody() : 'NULL'; break; case 'ts': + case 'date_iso_8601': $result = gmdate('c'); break; + case 'date_common_log': + $result = date('d/M/Y:H:i:s O'); + break; case 'method': $result = $request->getMethod(); break; diff --git a/tests/MessageFormatterTest.php b/tests/MessageFormatterTest.php index 2f5dd286..2ce347bb 100644 --- a/tests/MessageFormatterTest.php +++ b/tests/MessageFormatterTest.php @@ -20,13 +20,24 @@ class MessageFormatterTest extends \PHPUnit_Framework_TestCase $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', '/'); $result = $f->format($request); - // Ensure it matches this format: '2014-03-02T00:18:41+00:00'; - $this->assertEquals(1, preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}/', $result)); + $this->assertEquals(1, preg_match($pattern, $result)); } public function formatProvider()