Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #58 from logmatic/JSON
Browse files Browse the repository at this point in the history
Json
  • Loading branch information
weierophinney committed Apr 9, 2018
2 parents fe91d51 + 9f96fa3 commit fba29a8
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/book/formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\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\Json();
$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
Expand Down
58 changes: 58 additions & 0 deletions src/Formatter/Json.php
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;
}
}
57 changes: 57 additions & 0 deletions test/Formatter/JsonTest.php
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'],
];
}
}

0 comments on commit fba29a8

Please sign in to comment.