From 1c6dca3733909a797b2ce31bae7a7c3541a81f5d Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Wed, 7 Sep 2016 15:54:42 +0200 Subject: [PATCH 1/9] adding Json formatter as suggested by #13 --- src/Formatter/JsonFormatter.php | 128 ++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/Formatter/JsonFormatter.php diff --git a/src/Formatter/JsonFormatter.php b/src/Formatter/JsonFormatter.php new file mode 100644 index 00000000..d4f9542b --- /dev/null +++ b/src/Formatter/JsonFormatter.php @@ -0,0 +1,128 @@ +format($this->getDateTimeFormat()); + } + + return @json_encode($event, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION); + + } + + /** + * {@inheritDoc} + */ + public function getDateTimeFormat() + { + return $this->dateTimeFormat; + } + + /** + * {@inheritDoc} + */ + public function setDateTimeFormat($dateTimeFormat) + { + $this->dateTimeFormat = (string) $dateTimeFormat; + return $this; + } + + /** + * Normalizes given $data. + * + * @param mixed $data + * + * @return mixed + */ + protected function normalize($data, $depth = 0) + { + if ($depth > 9) { + return 'Over 9 levels deep, aborting normalization'; + } + if (is_array($data) || $data instanceof \Traversable) { + $normalized = []; + $count = 1; + foreach ($data as $key => $value) { + if ($count++ >= 1000) { + $normalized['...'] = 'Over 1000 items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value, $depth + 1); + } + return $normalized; + } + if ($data instanceof Exception) { + return $this->normalizeException($data); + } + return $data; + } + + /** + * Normalizes given exception with its own stack trace + * + * @param \Throwable $e + * + * @return array + */ + protected function normalizeException(\Throwable $e) + { + $data = [ + 'class' => get_class($e), + 'message' => $e->getMessage(), + 'code' => $e->getCode(), + 'file' => $e->getFile().':'.$e->getLine(), + ]; + $trace = $e->getTrace(); + foreach ($trace as $frame) { + if (isset($frame['file'])) { + $data['trace'][] = $frame['file'].':'.$frame['line']; + } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { + // We should again normalize the frames, because it might contain invalid items + $data['trace'][] = $frame['function']; + } else { + // We should again normalize the frames, because it might contain invalid items + $data['trace'][] = $this->normalize($frame); + } + } + if ($previous = $e->getPrevious()) { + $data['previous'] = $this->normalizeException($previous); + } + return $data; + } +} From 9c28b10ed3a4b5806d7f42b9cb345c7de599a2cf Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Wed, 7 Sep 2016 15:59:23 +0200 Subject: [PATCH 2/9] adding Json formatter as suggested by #13 --- doc/book/formatters.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/book/formatters.md b/doc/book/formatters.md index 5d987a01..e0d9dae3 100644 --- a/doc/book/formatters.md +++ b/doc/book/formatters.md @@ -38,6 +38,24 @@ format string. This string contains keys surrounded by percent signs (e.g. You can retrieve the default keys by using the `DEFAULT_FORMAT` constant from `Zend\Log\Formatter\Simple`. +## Formatting to Json + +`Zend\Log\Formatter\JsonFormatter` is the Json formatter. By default, it +automatically logs all items as Json: + +```php +$writer = new Zend\Log\Writer\Stream('php://output'); +$formatter = new Zend\Log\Formatter\JsonFormatter(); +$writer->setFormatter($formatter); + +$logger = new Zend\Log\Logger(); +$logger->addWriter($writer); + +$logger->info('there'); + +// outputs "{"timestamp":"2016-09-07T13:58:01+00:00","priority":6,"priorityName":"INFO","message":"there","extra":[]}" +``` + ## Formatting to XML `Zend\Log\Formatter\Xml` formats log data into XML strings. By default, it From df7162d61be48aedaf7f3b5c179f91411fa43699 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Wed, 7 Sep 2016 15:59:37 +0200 Subject: [PATCH 3/9] adding Json formatter as suggested by #13 --- test/Formatter/JsonFormatterTest.php | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/Formatter/JsonFormatterTest.php diff --git a/test/Formatter/JsonFormatterTest.php b/test/Formatter/JsonFormatterTest.php new file mode 100644 index 00000000..612ad68c --- /dev/null +++ b/test/Formatter/JsonFormatterTest.php @@ -0,0 +1,59 @@ +format(['timestamp' => $date, 'message' => 'foo', 'priority' => 42]); + $json = json_decode($line); + + + $this->assertEqual($date->format('c'), $json->timestamp); + $this->assertEqual('foo', $json->message); + $this->assertEqual((string)42, $json->priority); + } + + + + /** + * @dataProvider provideDateTimeFormats + */ + public function testSetDateTimeFormat($dateTimeFormat) + { + $date = new DateTime(); + $f = new JsonFormatter(); + $f->setDateTimeFormat($dateTimeFormat); + + $line = $f->format(['timestamp' => $date]); + $json = json_decode($line); + + $this->assertContains($date->format($dateTimeFormat), $json->timestamp); + } + + + public function provideDateTimeFormats() + { + return [ + ['r'], + ['U'], + ]; + } + +} From b2702c4cfb47801db94b8e81f53f856a9930a8db Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 10:57:30 +0200 Subject: [PATCH 4/9] Rename the formatter --- doc/book/formatters.md | 4 +- src/Formatter/Json.php | 127 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 src/Formatter/Json.php diff --git a/doc/book/formatters.md b/doc/book/formatters.md index e0d9dae3..d8d0579e 100644 --- a/doc/book/formatters.md +++ b/doc/book/formatters.md @@ -40,12 +40,12 @@ You can retrieve the default keys by using the `DEFAULT_FORMAT` constant from ## Formatting to Json -`Zend\Log\Formatter\JsonFormatter` is the Json formatter. By default, it +`Zend\Log\Formatter\Json` is the Json formatter. By default, it automatically logs all items as Json: ```php $writer = new Zend\Log\Writer\Stream('php://output'); -$formatter = new Zend\Log\Formatter\JsonFormatter(); +$formatter = new Zend\Log\Formatter\Json(); $writer->setFormatter($formatter); $logger = new Zend\Log\Logger(); diff --git a/src/Formatter/Json.php b/src/Formatter/Json.php new file mode 100644 index 00000000..044c7ad6 --- /dev/null +++ b/src/Formatter/Json.php @@ -0,0 +1,127 @@ +format($this->getDateTimeFormat()); + } + + return @json_encode($event, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION); + + } + + /** + * {@inheritDoc} + */ + public function getDateTimeFormat() + { + return $this->dateTimeFormat; + } + + /** + * {@inheritDoc} + */ + public function setDateTimeFormat($dateTimeFormat) + { + $this->dateTimeFormat = (string) $dateTimeFormat; + return $this; + } + + /** + * Normalizes given $data. + * + * @param mixed $data + * + * @return mixed + */ + protected function normalize($data, $depth = 0) + { + if ($depth > 9) { + return 'Over 9 levels deep, aborting normalization'; + } + if (is_array($data) || $data instanceof \Traversable) { + $normalized = []; + $count = 1; + foreach ($data as $key => $value) { + if ($count++ >= 1000) { + $normalized['...'] = 'Over 1000 items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value, $depth + 1); + } + return $normalized; + } + if ($data instanceof Exception) { + return $this->normalizeException($data); + } + return $data; + } + + /** + * Normalizes given exception with its own stack trace + * + * @param \Throwable $e + * + * @return array + */ + protected function normalizeException(\Throwable $e) + { + $data = [ + 'class' => get_class($e), + 'message' => $e->getMessage(), + 'code' => $e->getCode(), + 'file' => $e->getFile().':'.$e->getLine(), + ]; + $trace = $e->getTrace(); + foreach ($trace as $frame) { + if (isset($frame['file'])) { + $data['trace'][] = $frame['file'].':'.$frame['line']; + } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { + // We should again normalize the frames, because it might contain invalid items + $data['trace'][] = $frame['function']; + } else { + // We should again normalize the frames, because it might contain invalid items + $data['trace'][] = $this->normalize($frame); + } + } + if ($previous = $e->getPrevious()) { + $data['previous'] = $this->normalizeException($previous); + } + return $data; + } +} From b90aad0d7fc648a307f2deef0f312b91b0394b92 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 10:57:47 +0200 Subject: [PATCH 5/9] Typo in test class --- test/Formatter/JsonFormatterTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Formatter/JsonFormatterTest.php b/test/Formatter/JsonFormatterTest.php index 612ad68c..be19ed83 100644 --- a/test/Formatter/JsonFormatterTest.php +++ b/test/Formatter/JsonFormatterTest.php @@ -10,17 +10,17 @@ namespace ZendTest\Log\Formatter; use DateTime; -use Zend\Log\Formatter\JsonFormatter; +use Zend\Log\Formatter\Json; /** * @group Zend_Log */ -class XmlTest extends \PHPUnit_Framework_TestCase +class JsonTest extends \PHPUnit_Framework_TestCase { public function testDefaultFormat() { $date = new DateTime(); - $f = new JsonFormatter(); + $f = new Json(); $line = $f->format(['timestamp' => $date, 'message' => 'foo', 'priority' => 42]); $json = json_decode($line); @@ -38,7 +38,7 @@ public function testDefaultFormat() public function testSetDateTimeFormat($dateTimeFormat) { $date = new DateTime(); - $f = new JsonFormatter(); + $f = new Json(); $f->setDateTimeFormat($dateTimeFormat); $line = $f->format(['timestamp' => $date]); From ee513b5e9ae49f88ba2261db0a5b8054ba2e89c1 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 10:58:12 +0200 Subject: [PATCH 6/9] remove --- src/Formatter/JsonFormatter.php | 128 -------------------------------- 1 file changed, 128 deletions(-) delete mode 100644 src/Formatter/JsonFormatter.php diff --git a/src/Formatter/JsonFormatter.php b/src/Formatter/JsonFormatter.php deleted file mode 100644 index d4f9542b..00000000 --- a/src/Formatter/JsonFormatter.php +++ /dev/null @@ -1,128 +0,0 @@ -format($this->getDateTimeFormat()); - } - - return @json_encode($event, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION); - - } - - /** - * {@inheritDoc} - */ - public function getDateTimeFormat() - { - return $this->dateTimeFormat; - } - - /** - * {@inheritDoc} - */ - public function setDateTimeFormat($dateTimeFormat) - { - $this->dateTimeFormat = (string) $dateTimeFormat; - return $this; - } - - /** - * Normalizes given $data. - * - * @param mixed $data - * - * @return mixed - */ - protected function normalize($data, $depth = 0) - { - if ($depth > 9) { - return 'Over 9 levels deep, aborting normalization'; - } - if (is_array($data) || $data instanceof \Traversable) { - $normalized = []; - $count = 1; - foreach ($data as $key => $value) { - if ($count++ >= 1000) { - $normalized['...'] = 'Over 1000 items, aborting normalization'; - break; - } - $normalized[$key] = $this->normalize($value, $depth + 1); - } - return $normalized; - } - if ($data instanceof Exception) { - return $this->normalizeException($data); - } - return $data; - } - - /** - * Normalizes given exception with its own stack trace - * - * @param \Throwable $e - * - * @return array - */ - protected function normalizeException(\Throwable $e) - { - $data = [ - 'class' => get_class($e), - 'message' => $e->getMessage(), - 'code' => $e->getCode(), - 'file' => $e->getFile().':'.$e->getLine(), - ]; - $trace = $e->getTrace(); - foreach ($trace as $frame) { - if (isset($frame['file'])) { - $data['trace'][] = $frame['file'].':'.$frame['line']; - } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $frame['function']; - } else { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $this->normalize($frame); - } - } - if ($previous = $e->getPrevious()) { - $data['previous'] = $this->normalizeException($previous); - } - return $data; - } -} From fc13edf84060df3570efaa9d633cad5099952fd8 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 11:33:51 +0200 Subject: [PATCH 7/9] removing last typos --- test/Formatter/{JsonFormatterTest.php => Json.php} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename test/Formatter/{JsonFormatterTest.php => Json.php} (87%) diff --git a/test/Formatter/JsonFormatterTest.php b/test/Formatter/Json.php similarity index 87% rename from test/Formatter/JsonFormatterTest.php rename to test/Formatter/Json.php index be19ed83..5f6e6e21 100644 --- a/test/Formatter/JsonFormatterTest.php +++ b/test/Formatter/Json.php @@ -25,9 +25,9 @@ public function testDefaultFormat() $json = json_decode($line); - $this->assertEqual($date->format('c'), $json->timestamp); - $this->assertEqual('foo', $json->message); - $this->assertEqual((string)42, $json->priority); + $this->assertEquals($date->format('c'), $json->timestamp); + $this->assertEquals('foo', $json->message); + $this->assertEquals((string)42, $json->priority); } From 078d15754dc96917bcc2cb04b7a7409d71328017 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 14:19:31 +0200 Subject: [PATCH 8/9] Add tests --- src/Formatter/Json.php | 13 ++++--------- test/Formatter/{Json.php => JsonTest.php} | 4 +--- 2 files changed, 5 insertions(+), 12 deletions(-) rename test/Formatter/{Json.php => JsonTest.php} (94%) diff --git a/src/Formatter/Json.php b/src/Formatter/Json.php index 044c7ad6..41a9562a 100644 --- a/src/Formatter/Json.php +++ b/src/Formatter/Json.php @@ -9,15 +9,13 @@ namespace Zend\Log\Formatter; - -use Exception; use DateTime; +use Exception; class Json implements FormatterInterface { - /** * Format specifier for DateTime objects in event data (default: ISO 8601) * @@ -27,7 +25,6 @@ class Json implements FormatterInterface protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT; - /** * Formats data into a single line to be written by the writer. * @@ -36,13 +33,11 @@ class Json implements FormatterInterface */ public function format($event) { - if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) { $event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat()); } return @json_encode($event, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION); - } /** @@ -58,7 +53,7 @@ public function getDateTimeFormat() */ public function setDateTimeFormat($dateTimeFormat) { - $this->dateTimeFormat = (string) $dateTimeFormat; + $this->dateTimeFormat = (string)$dateTimeFormat; return $this; } @@ -105,12 +100,12 @@ protected function normalizeException(\Throwable $e) 'class' => get_class($e), 'message' => $e->getMessage(), 'code' => $e->getCode(), - 'file' => $e->getFile().':'.$e->getLine(), + 'file' => $e->getFile() . ':' . $e->getLine(), ]; $trace = $e->getTrace(); foreach ($trace as $frame) { if (isset($frame['file'])) { - $data['trace'][] = $frame['file'].':'.$frame['line']; + $data['trace'][] = $frame['file'] . ':' . $frame['line']; } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { // We should again normalize the frames, because it might contain invalid items $data['trace'][] = $frame['function']; diff --git a/test/Formatter/Json.php b/test/Formatter/JsonTest.php similarity index 94% rename from test/Formatter/Json.php rename to test/Formatter/JsonTest.php index 5f6e6e21..37a4df33 100644 --- a/test/Formatter/Json.php +++ b/test/Formatter/JsonTest.php @@ -31,7 +31,6 @@ public function testDefaultFormat() } - /** * @dataProvider provideDateTimeFormats */ @@ -44,7 +43,7 @@ public function testSetDateTimeFormat($dateTimeFormat) $line = $f->format(['timestamp' => $date]); $json = json_decode($line); - $this->assertContains($date->format($dateTimeFormat), $json->timestamp); + $this->assertEquals($date->format($dateTimeFormat), $json->timestamp); } @@ -55,5 +54,4 @@ public function provideDateTimeFormats() ['U'], ]; } - } From 9f96fa3717752dd356b2b3b9292f0918c7c0e949 Mon Sep 17 00:00:00 2001 From: Guillaume Polaert Date: Thu, 8 Sep 2016 14:38:47 +0200 Subject: [PATCH 9/9] remove unsed method --- src/Formatter/Json.php | 64 ------------------------------------------ 1 file changed, 64 deletions(-) diff --git a/src/Formatter/Json.php b/src/Formatter/Json.php index 41a9562a..291dc4a1 100644 --- a/src/Formatter/Json.php +++ b/src/Formatter/Json.php @@ -10,7 +10,6 @@ namespace Zend\Log\Formatter; use DateTime; -use Exception; class Json implements FormatterInterface { @@ -56,67 +55,4 @@ public function setDateTimeFormat($dateTimeFormat) $this->dateTimeFormat = (string)$dateTimeFormat; return $this; } - - /** - * Normalizes given $data. - * - * @param mixed $data - * - * @return mixed - */ - protected function normalize($data, $depth = 0) - { - if ($depth > 9) { - return 'Over 9 levels deep, aborting normalization'; - } - if (is_array($data) || $data instanceof \Traversable) { - $normalized = []; - $count = 1; - foreach ($data as $key => $value) { - if ($count++ >= 1000) { - $normalized['...'] = 'Over 1000 items, aborting normalization'; - break; - } - $normalized[$key] = $this->normalize($value, $depth + 1); - } - return $normalized; - } - if ($data instanceof Exception) { - return $this->normalizeException($data); - } - return $data; - } - - /** - * Normalizes given exception with its own stack trace - * - * @param \Throwable $e - * - * @return array - */ - protected function normalizeException(\Throwable $e) - { - $data = [ - 'class' => get_class($e), - 'message' => $e->getMessage(), - 'code' => $e->getCode(), - 'file' => $e->getFile() . ':' . $e->getLine(), - ]; - $trace = $e->getTrace(); - foreach ($trace as $frame) { - if (isset($frame['file'])) { - $data['trace'][] = $frame['file'] . ':' . $frame['line']; - } elseif (isset($frame['function']) && $frame['function'] === '{closure}') { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $frame['function']; - } else { - // We should again normalize the frames, because it might contain invalid items - $data['trace'][] = $this->normalize($frame); - } - } - if ($previous = $e->getPrevious()) { - $data['previous'] = $this->normalizeException($previous); - } - return $data; - } }