This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from logmatic/JSON
Json
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zend-log for the canonical source repository | ||
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Log\Formatter; | ||
|
||
use DateTime; | ||
|
||
class Json implements FormatterInterface | ||
{ | ||
|
||
|
||
/** | ||
* Format specifier for DateTime objects in event data (default: ISO 8601) | ||
* | ||
* @see http://php.net/manual/en/function.date.php | ||
* @var string | ||
*/ | ||
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT; | ||
|
||
|
||
/** | ||
* Formats data into a single line to be written by the writer. | ||
* | ||
* @param array $event event data | ||
* @return string formatted line to write to the log | ||
*/ | ||
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); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getDateTimeFormat() | ||
{ | ||
return $this->dateTimeFormat; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setDateTimeFormat($dateTimeFormat) | ||
{ | ||
$this->dateTimeFormat = (string)$dateTimeFormat; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Log\Formatter; | ||
|
||
use DateTime; | ||
use Zend\Log\Formatter\Json; | ||
|
||
/** | ||
* @group Zend_Log | ||
*/ | ||
class JsonTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testDefaultFormat() | ||
{ | ||
$date = new DateTime(); | ||
$f = new Json(); | ||
$line = $f->format(['timestamp' => $date, 'message' => 'foo', 'priority' => 42]); | ||
$json = json_decode($line); | ||
|
||
|
||
$this->assertEquals($date->format('c'), $json->timestamp); | ||
$this->assertEquals('foo', $json->message); | ||
$this->assertEquals((string)42, $json->priority); | ||
} | ||
|
||
|
||
/** | ||
* @dataProvider provideDateTimeFormats | ||
*/ | ||
public function testSetDateTimeFormat($dateTimeFormat) | ||
{ | ||
$date = new DateTime(); | ||
$f = new Json(); | ||
$f->setDateTimeFormat($dateTimeFormat); | ||
|
||
$line = $f->format(['timestamp' => $date]); | ||
$json = json_decode($line); | ||
|
||
$this->assertEquals($date->format($dateTimeFormat), $json->timestamp); | ||
} | ||
|
||
|
||
public function provideDateTimeFormats() | ||
{ | ||
return [ | ||
['r'], | ||
['U'], | ||
]; | ||
} | ||
} |